From 1ac6046553caf29b1e636690086b6d0daabbc134 Mon Sep 17 00:00:00 2001 From: iglocska Date: Tue, 10 Dec 2019 10:48:15 +0100 Subject: [PATCH] chg: [UI] refactor of the genericForm/submitbutton to support ajax --- app/View/Attributes/add.ctp | 254 +++++++----------- app/View/Attributes/test_add.ctp | 128 --------- .../genericElements/Form/genericForm.ctp | 16 +- .../genericElements/Form/submitButton.ctp | 40 ++- app/webroot/css/main.css | 12 +- app/webroot/js/misp.js | 2 +- 6 files changed, 152 insertions(+), 300 deletions(-) delete mode 100644 app/View/Attributes/test_add.ctp diff --git a/app/View/Attributes/add.ctp b/app/View/Attributes/add.ctp index 89bf37355..91216f37a 100644 --- a/app/View/Attributes/add.ctp +++ b/app/View/Attributes/add.ctp @@ -1,166 +1,117 @@ -
Form->create('Attribute', array('id', 'url' => '/attributes/' . $url_params)); -?> -
- -
- -
- Form->hidden('event_id'); - echo $this->Form->input('category', array( - 'empty' => __('(choose one)'), - 'label' => __('Category ') . $this->element('formInfo', array('type' => 'category')), - )); - echo $this->Form->input('type', array( - 'empty' => __('(first choose category)'), - 'label' => __('Type ') . $this->element('formInfo', array('type' => 'type')), - )); - - $initialDistribution = 5; - if (Configure::read('MISP.default_attribute_distribution') != null) { - if (Configure::read('MISP.default_attribute_distribution') === 'event') { - $initialDistribution = 5; - } else { - $initialDistribution = Configure::read('MISP.default_attribute_distribution'); - } - } - - ?> -
- array($distributionLevels), - 'label' => __('Distribution ') . $this->element('formInfo', array('type' => 'distribution')), - ); - - if ($action == 'add') { - $distArray['selected'] = $initialDistribution; - } - - echo $this->Form->input('distribution', $distArray); - ?> - - Form->input('value', array( - 'type' => 'textarea', - 'error' => array('escape' => false), - 'div' => 'input clear', - 'class' => 'input-xxlarge' - )); - ?> -
- Form->input('comment', array( + $modelForForm = 'Attribute'; + echo $this->element('genericElements/Form/genericForm', array( + 'form' => $this->Form, + 'data' => array( + 'title' => $action === 'add' ? __('Add Attribute') : __('Edit Attribute'), + 'model' => $modelForForm, + 'fields' => array( + array( + 'field' => 'event_id', + 'class' => 'org-id-picker-hidden-field', 'type' => 'text', - 'label' => __('Contextual Comment'), - 'error' => array('escape' => false), + 'hidden' => true + ), + array( + 'field' => 'category', + 'class' => 'input', + 'empty' => __('(choose one)'), + 'options' => $categories, + 'stayInLine' => 1 + ), + array( + 'field' => 'type', + 'class' => 'input', + 'empty' => __('(choose category first)'), + 'options' => $types + ), + array( + 'field' => 'distribution', + 'class' => 'input', + 'options' => $distributionLevels, + 'default' => isset($attribute['Attribute']['distribution']) ? $attribute['Attribute']['distribution'] : $initialDistribution, + 'stayInLine' => 1 + ), + array( + 'field' => 'sharing_group_id', + 'class' => 'input', + 'options' => $sharingGroups, + 'label' => __("Sharing Group") + ), + array( + 'field'=> 'value', + 'type' => 'textarea', + 'class' => 'input span6', + 'div' => 'input clear' + ), + array( + 'field' => 'comment', + 'type' => 'text', + 'class' => 'input span6', 'div' => 'input clear', - 'class' => 'input-xxlarge' - )); - ?> -
- Form->input('to_ids', array( - 'label' => __('for Intrusion Detection System'), - 'type' => 'checkbox' - )); - echo $this->Form->input('batch_import', array( + 'label' => __("Contextual Comment") + ), + array( + 'field' => 'to_ids', + 'type' => 'checkbox', + 'label' => __("for Intrusion Detection System"), + //'stayInLine' => 1 + ), + array( + 'field' => 'batch_import', 'type' => 'checkbox' - )); - echo '
'; - echo $this->Form->input('disable_correlation', array( + ), + array( + 'field' => 'disable_correlation', 'type' => 'checkbox' - )); - ?> -
-
- - -
- - -
- - Form->button('Submit', array('class' => 'btn btn-primary')); - endif; - echo $this->Form->end(); - ?> -
-' + ), + 'submit' => array( + 'action' => $action, + 'ajaxSubmit' => sprintf( + 'submitPopoverForm(%s, %s)', + "'" . ($action == 'add' ? h($event_id) : h($attribute['Attribute']['id'])) . "'", + "'" . h($action) . "'" + ) + ) + ) + )); if (!$ajax) { - $event['Event']['id'] = $event_id; - $event['Event']['published'] = $published; - echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'event', 'menuItem' => 'addAttribute', 'event' => $event)); - } -?> - Js->writeBuffer(); // Write cached scripts diff --git a/app/View/Attributes/test_add.ctp b/app/View/Attributes/test_add.ctp deleted file mode 100644 index 1b4f0d810..000000000 --- a/app/View/Attributes/test_add.ctp +++ /dev/null @@ -1,128 +0,0 @@ -element('genericElements/Form/genericForm', array( - 'form' => $this->Form, - 'data' => array( - 'title' => $action === 'add' ? __('Add Attribute') : __('Edit Attribute'), - 'model' => $modelForForm, - 'fields' => array( - array( - 'field' => 'event_id', - 'class' => 'org-id-picker-hidden-field', - 'type' => 'text', - 'hidden' => true - ), - array( - 'field' => 'category', - 'class' => 'input', - 'empty' => __('(choose one)'), - 'options' => $categories, - 'stayInLine' => 1 - ), - array( - 'field' => 'type', - 'class' => 'input', - 'empty' => __('(choose category first)'), - 'options' => $types - ), - array( - 'field' => 'distribution', - 'class' => 'input', - 'options' => $distributionLevels, - 'default' => isset($attribute['Attribute']['distribution']) ? $attribute['Attribute']['distribution'] : $initialDistribution, - 'stayInLine' => 1 - ), - array( - 'field' => 'sharing_group_id', - 'class' => 'input', - 'options' => $sharingGroups, - 'label' => __("Sharing Group") - ), - array( - 'field'=> 'value', - 'type' => 'textarea', - 'class' => 'input span6', - 'div' => 'input clear' - ), - array( - 'field' => 'comment', - 'type' => 'text', - 'class' => 'input span6', - 'div' => 'input clear', - 'label' => __("Contextual Comment") - ), - array( - 'field' => 'to_ids', - 'type' => 'checkbox', - 'label' => __("for Intrusion Detection System"), - 'stayInLine' => 1 - ), - array( - 'field' => 'batch_import', - 'type' => 'checkbox' - ), - array( - 'field' => 'disable_correlation', - 'type' => 'checkbox' - ), - '
' - ), - 'submit' => array( - 'action' => $action - ) - ) - )); - if (!$ajax) { - echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'event-collection', 'menuItem' => $this->action === 'add' ? 'add' : 'editEvent')); - } -?> - - -Js->writeBuffer(); // Write cached scripts diff --git a/app/View/Elements/genericElements/Form/genericForm.ctp b/app/View/Elements/genericElements/Form/genericForm.ctp index 61be61b06..00637b0fe 100644 --- a/app/View/Elements/genericElements/Form/genericForm.ctp +++ b/app/View/Elements/genericElements/Form/genericForm.ctp @@ -73,12 +73,26 @@ if (!empty($data['submit'])) { $submitButtonData = array_merge($submitButtonData, $data['submit']); } + if (!empty($data['ajaxSubmit'])) { + $submitButtonData['ajaxSubmit'] = $ajaxSubmit; + } + $ajaxFlashMessage = ''; + if ($ajax) { + $ajaxFlashMessage = sprintf( + '
%s
', + $this->Flash->render() + ); + } $formEnd = $this->Form->end(); echo sprintf( - '
%s
%s%s
%s%s%s
', + '
%s
%s%s
%s
%s%s%s
', + $ajax ? 'ajax' : 'form', $formCreate, empty($data['title']) ? h(Inflector::humanize($this->request->params['action'])) . ' ' . $modelForForm : h($data['title']), + $ajaxFlashMessage, + empty($ajax) ? '' : 'ajax_fieldset', $fieldsString, + empty($ajax) ? '' : 'ajax_fieldset', $formEnd, $metaFieldString, $this->element('genericElements/Form/submitButton', $submitButtonData) diff --git a/app/View/Elements/genericElements/Form/submitButton.ctp b/app/View/Elements/genericElements/Form/submitButton.ctp index faa46e69e..c6f6f5e1c 100644 --- a/app/View/Elements/genericElements/Form/submitButton.ctp +++ b/app/View/Elements/genericElements/Form/submitButton.ctp @@ -1,12 +1,32 @@ %s', - sprintf( - "$('#%s%sForm').submit();", - h($model), - h(Inflector::classify($action)) - ), - empty($type) ? 'primary' : h($type), - empty($text) ? __('Submit') : h($text) - ); + if ($ajax) { + echo sprintf( + '%s%s', + sprintf( + '%s', + __('Submit'), + __('Submit'), + $ajaxSubmit, + __('Submit') + ), + sprintf( + '%s', + __('Cancel'), + __('Cancel'), + 'cancelPopoverForm();', + __('Cancel') + ) + ); + } else { + echo sprintf( + '', + sprintf( + "$('#%s%sForm').submit();", + h($model), + h(Inflector::classify($action)) + ), + empty($type) ? 'primary' : h($type), + empty($text) ? __('Submit') : h($text) + ); + } ?> diff --git a/app/webroot/css/main.css b/app/webroot/css/main.css index 5ea24498e..46321bc56 100644 --- a/app/webroot/css/main.css +++ b/app/webroot/css/main.css @@ -882,9 +882,10 @@ a.proposal_link_red:hover { .ajax_popover_form { display:none; - width: 700px; + max-width: 700px; top:15%; - left:calc(50% - 350px); + margin-left:auto; + margin-right:auto; position: fixed; background-color:#f4f4f4; border-radius: 11px 11px 10px 10px; @@ -942,7 +943,7 @@ a.proposal_link_red:hover { border-radius: 10px 10px 0px 0px; padding-left:10px; margin-bottom:0px; - width:690px; + width:calc(100% - 10px); background-color:black; color:white; font-size: 21px; @@ -976,8 +977,9 @@ a.proposal_link_red:hover { height:120px !important; } -.ajax_popover_form .add_attribute_fields { +.ajax_fieldset, .ajax_popover_form .add_attribute_fields { padding-left:10px; + padding-right:10px; } .hover_enrichment_text { @@ -2431,4 +2433,4 @@ table tr:hover .down-expand-button { background-color: #3b3b3b; color: white; border-radius: 3px; -} \ No newline at end of file +} diff --git a/app/webroot/js/misp.js b/app/webroot/js/misp.js index 1f6cf07ad..9bc88bee0 100644 --- a/app/webroot/js/misp.js +++ b/app/webroot/js/misp.js @@ -1187,7 +1187,7 @@ function submitPopoverForm(context_id, referer, update_context_id) { break; } if (url !== null) { - url = baseurl + url; + url = baseurl + $("#submitButton").closest("form").attr('action'); $.ajax({ beforeSend: function (XMLHttpRequest) { $(".loading").show();