Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (function () {
  3.     "use strict";
  4.     var formId = '#matchingConditionSettingsForm';
  5.     var switchConditionsBtnId = '#switchConditions';
  6.     var saveSourceBtnId = '#saveSourceBtn';
  7.     var getSourceBtnId = '#getSourceBtn';
  8.     var saveDestinationBtnId = '#saveDestinationBtn';
  9.     var getDestinationBtnId = '#getDestinationBtn';
  10.     var saveMatchingBtnId = '#saveMatchingBtn';
  11.     var getMatchingBtnId = '#getMatchingBtn';
  12.     var submitFormBtnId = '#submitFormBtn';
  13.     var extractSourceBtnId = '#extractSourceBtn';
  14.     var extractDestinationBtnId = '#extractDestinationBtn';
  15.     var matchingWordsAreaId = '#matchingWordsArea';
  16.     var checkDomainInPartnerGroupId = "#check-domain-in-group";
  17.  
  18.     var sourceBuilderId = '#source-builder';
  19.     var destinationBuilderId = '#destination-builder';
  20.     var matchingBuilderId = '#matching-builder';
  21.  
  22.     var removeDatalistItemBtnId = "#dataRemoveItem";
  23.  
  24.     var sourceListKey = "/user/matchingSettings/listSourceKey";
  25.     var sourcePrefixUrlKey = "/user/matchingSettings/source";
  26.     var destinationListKey = "/user/matchingSettings/listDestinationKey";
  27.     var destinationPrefixUrlKey = "/user/matchingSettings/destination";
  28.     var matchingListKey = "/user/matchingSettings/listMatchingKey";
  29.     var matchingPrefixUrlKey = "/user/matchingSettings/matching";
  30.  
  31.     var collapsedPrefixKey = "/user/matchingSettings/collapsed";
  32.  
  33.     var collapseViewPostfix = "-collapse-view";
  34.  
  35.     var checkDomainInPartnerGroupKey = "/user/matchingSettings/checkDomainInPartnerGroup";
  36.  
  37.     var sourceConditionNameId = "#source-condition-name";
  38.     var destinationConditionNameId = "#destination-condition-name";
  39.     var matchingConditionNameId = "#matching-condition-name";
  40.  
  41.     var sourceNotificationId = "source-notification";
  42.     var sourceNotificationNewId = "source-notification-new";
  43.     var sourceNotificationAccountId = "source-notification-account";
  44.     var sourceNotificationSentBtnId = "#source-notification-sent";
  45.  
  46.     var destinationNotificationId = "destination-notification";
  47.     var destinationNotificationNewId = "destination-notification-new";
  48.     var destinationNotificationAccountId = "destination-notification-account";
  49.     var destinationNotificationSentBtnId = "#destination-notification-sent";
  50.  
  51.     var matchingNotificationId = "matching-notification";
  52.     var matchingNotificationNewId = "matching-notification-new";
  53.     var matchingNotificationAccountId = "matching-notification-account";
  54.     var matchingNotificationSentBtnId = "#matching-notification-sent";
  55.  
  56.     var SOURCE_CONDITION = 0;
  57.     var DESTINATION_CONDITION = 1;
  58.     var MATCHING_CONDITION = 2;
  59.  
  60.     var NOTIFICATION_NEW = 0;
  61.     var NOTIFICATION_ACCEPT = 1;
  62.     var NOTIFICATION_REJECT = 2;
  63.  
  64.     var sourceNotificationList = [];
  65.     var destinationNotificationList = [];
  66.     var matchingNotificationList = [];
  67.  
  68.     var default_source_configs = {};
  69.     var default_destination_configs = {};
  70.     var default_matching_configs = {};
  71.  
  72.     var ruleInvalidateIds = [];
  73.  
  74.     var RULE_NUMBER_ID = 4;
  75.     var RULE_NUMBER_UP_RATE_ID = 5;
  76.     var RULE_NUMBER_DOWN_RATE_ID = 6;
  77.  
  78.     var ruleNumberId = "ruleNumber";
  79.     var ruleNumberUpRateId = "ruleNumberUpRate";
  80.     var ruleNumberDownRateId = "ruleNumberDownRate";
  81.  
  82.     var ruleNumberDownRateName = "";
  83.     var ruleNumberUpRateName = "";
  84.     var ruleNumberName = "";
  85.  
  86.     var default_source_rules = {
  87.         condition: "AND",
  88.         rules: [
  89.             {
  90.                 id: "7",
  91.                 input: "ratio",
  92.                 type: "integer",
  93.                 value: 1
  94.             },
  95.             {
  96.                 id: "8",
  97.                 operator: "greater_or_equal",
  98.                 type:  "string",
  99.                 value: "-7"
  100.             },
  101.             {
  102.                 id: "2",
  103.                 operator: "not_contains",
  104.                 type:  "string",
  105.                 value: "Re:"
  106.             },
  107.             {
  108.                 id: "0",
  109.                 operator: "not_contains",
  110.                 type:  "string",
  111.                 value: "@world-link-system.com"
  112.             }
  113.         ]
  114.     };
  115.  
  116.     var default_destination_rules = {
  117.         condition: "AND",
  118.         rules: [
  119.             {
  120.                 id: "7",
  121.                 input: "ratio",
  122.                 type: "integer",
  123.                 value: 0
  124.             },
  125.             {
  126.                 id: "8",
  127.                 operator: "greater_or_equal",
  128.                 type:  "string",
  129.                 value: "-7"
  130.             },
  131.             {
  132.                 id: "2",
  133.                 operator: "not_contains",
  134.                 type:  "string",
  135.                 value: "Re:"
  136.             },
  137.             {
  138.                 id: "0",
  139.                 operator: "not_contains",
  140.                 type:  "string",
  141.                 value: "@world-link-system.com"
  142.             }
  143.         ]
  144.     };
  145.  
  146.     function fullWidthNumConvert(fullWidthNum){
  147.         return fullWidthNum.replace(/[\uFF10-\uFF19]/g, function(m) {
  148.             return String.fromCharCode(m.charCodeAt(0) - 0xfee0);
  149.         });
  150.     }
  151.  
  152.     function numberValidator(value, rule) {
  153.         if (!value || value.trim().length === 0) {
  154.             return "Value can not be empty!";
  155.         } else if (rule.operator.type !== 'in') {
  156.             value = fullWidthNumConvert(value);
  157.             value = value.replace(/,/g, ",");
  158.             var pattern = /^\d+(,\d{3})*(\.\d+)?$/;
  159.             var match = pattern.test(value);
  160.             if(!match){
  161.                 return "Value must be a number greater than or equal to 0";
  162.             }
  163.         }
  164.         return true;
  165.     }
  166.  
  167.     function matchingMumberValidator(value, rule) {
  168.         if (!value || value.trim().length === 0) {
  169.             return "Value can not be empty!";
  170.         } else if (rule.operator.type !== 'in') {
  171.             if(value === ruleNumberName || value === ruleNumberUpRateName || value === ruleNumberDownRateName){
  172.                 return true;
  173.             }
  174.             value = fullWidthNumConvert(value);
  175.             value = value.replace(/,/g, ",");
  176.             var pattern = /^\d+(,\d{3})*(\.\d+)?$/;
  177.             var match = pattern.test(value);
  178.             if(!match){
  179.                 return "Value must be a number greater than or equal to 0";
  180.             }
  181.         }
  182.         return true;
  183.     }
  184.  
  185.     $(function () {
  186.         var default_plugins = [
  187.             'sortable',
  188.             'filter-description',
  189.             'unique-filter',
  190.             'bt-tooltip-errors',
  191.             'bt-selectpicker',
  192.             'bt-checkbox',
  193.             'invert',
  194.         ];
  195.  
  196.         var default_filters = [{
  197.             id: '0',
  198.             label: '送信者',
  199.             type: 'string',
  200.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  201.         }, {
  202.             id: '1',
  203.             label: '受信者',
  204.             type: 'string',
  205.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  206.         }, {
  207.             id: '9',
  208.             label: 'CC',
  209.             type: 'string',
  210.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  211.         }, {
  212.             id: '10',
  213.             label: 'BCC',
  214.             type: 'string',
  215.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  216.         }, {
  217.             id: '11',
  218.             label: '全て(受信者・CC・BCC)',
  219.             type: 'string',
  220.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  221.         }, {
  222.             id: '12',
  223.             label: 'いずれか(受信者・CC・BCC)',
  224.             type: 'string',
  225.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  226.         }, {
  227.             id: '2',
  228.             label: '件名',
  229.             type: 'string',
  230.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  231.         }, {
  232.             id: '3',
  233.             label: '本文',
  234.             type: 'string',
  235.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  236.         }, {
  237.             id: '13',
  238.             label: '全て(件名・本文)',
  239.             type: 'string',
  240.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  241.         }, {
  242.             id: '14',
  243.             label: 'いずれか(件名・本文)',
  244.             type: 'string',
  245.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  246.         }, {
  247.             id: '7',
  248.             label: '添付ファイル',
  249.             type: 'integer',
  250.             input: 'radio',
  251.             values: {
  252.                 1: '有り',
  253.                 0: '無し'
  254.             },
  255.             colors: {
  256.                 1: 'success',
  257.                 0: 'danger'
  258.             },
  259.             operators: ['equal']
  260.         }, {
  261.             id: '8',
  262.             label: '受信日',
  263.             type: 'string',
  264.             operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less']
  265.         }, {
  266.             id: '15',
  267.             label: 'マーク',
  268.             type: 'string',
  269.             operators: ['equal', 'not_equal']
  270.         }];
  271.  
  272.         var default_filters_matching = [{
  273.             id: '0',
  274.             label: '送信者',
  275.             type: 'string',
  276.             input: function(rule, inputName) {
  277.                 return '<label>比較先項目または値:&nbsp;</label>' +
  278.                     '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist" placeholder=""/>';
  279.             },
  280.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  281.         }, {
  282.             id: '1',
  283.             label: '受信者',
  284.             type: 'string',
  285.             input: function(rule, inputName) {
  286.                 return '<label>比較先項目または値:&nbsp;</label>' +
  287.                     '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist" placeholder=""/>';
  288.             },
  289.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  290.         }, {
  291.             id: '2',
  292.             label: '件名',
  293.             type: 'string',
  294.             input: function(rule, inputName) {
  295.                 return '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist" placeholder=""/>';
  296.             },
  297.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  298.         }, {
  299.             id: '3',
  300.             label: '本文',
  301.             type: 'string',
  302.             input: function(rule, inputName) {
  303.                 return '<label>比較先項目または値:&nbsp;</label>' +
  304.                     '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist" placeholder=""/>';
  305.             },
  306.             operators: ['contains', 'not_contains', 'equal', 'not_equal']
  307.         }];
  308.  
  309.         ruleNumberDownRateName = $('#'+ruleNumberDownRateId).text();
  310.         if(!ruleNumberDownRateName || ruleNumberDownRateName==null){
  311.             ruleInvalidateIds.push(RULE_NUMBER_DOWN_RATE_ID);
  312.         }else{
  313.             default_filters.splice(10,0,{
  314.                 id: RULE_NUMBER_DOWN_RATE_ID,
  315.                 label: ruleNumberDownRateName,
  316.                 type: 'string',
  317.                 operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less', 'in'],
  318.                 validation: {
  319.                     callback: numberValidator
  320.                 },
  321.             })
  322.  
  323.             default_filters_matching.splice(4,0,{
  324.                 id: RULE_NUMBER_DOWN_RATE_ID,
  325.                 label: ruleNumberDownRateName,
  326.                 type: 'string',
  327.                 input: function(rule, inputName) {
  328.                     return '<label>比較先項目または値:&nbsp;</label>' +
  329.                         '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist2" placeholder=""/>';
  330.                 },
  331.                 operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less', 'in'],
  332.                 validation: {
  333.                     callback: matchingMumberValidator
  334.                 },
  335.             })
  336.         }
  337.  
  338.         ruleNumberUpRateName = $('#'+ruleNumberUpRateId).text();
  339.         if(!ruleNumberUpRateName || ruleNumberUpRateName==null){
  340.             ruleInvalidateIds.push(RULE_NUMBER_UP_RATE_ID);
  341.         }else{
  342.             default_filters.splice(10,0,{
  343.                 id: RULE_NUMBER_UP_RATE_ID,
  344.                 label: ruleNumberUpRateName,
  345.                 type: 'string',
  346.                 operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less', 'in'],
  347.                 validation: {
  348.                     callback: numberValidator
  349.                 },
  350.             })
  351.  
  352.             default_filters_matching.splice(4,0,{
  353.                 id: RULE_NUMBER_UP_RATE_ID,
  354.                 label: ruleNumberUpRateName,
  355.                 type: 'string',
  356.                 input: function(rule, inputName) {
  357.                     return '<label>比較先項目または値:&nbsp;</label>' +
  358.                         '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist2" placeholder=""/>';
  359.                 },
  360.                 operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less', 'in'],
  361.                 validation: {
  362.                     callback: matchingMumberValidator
  363.                 },
  364.             })
  365.         }
  366.  
  367.         ruleNumberName = $('#'+ruleNumberId).text();
  368.         if(!ruleNumberName || ruleNumberName==null){
  369.             ruleInvalidateIds.push(RULE_NUMBER_ID);
  370.         }else{
  371.             default_filters.splice(10,0,{
  372.                 id: RULE_NUMBER_ID,
  373.                 label: ruleNumberName,
  374.                 type: 'string',
  375.                 operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less', 'in'],
  376.                 validation: {
  377.                     callback: numberValidator
  378.                 },
  379.             })
  380.  
  381.             default_filters_matching.splice(4,0,{
  382.                 id: RULE_NUMBER_ID,
  383.                 label: ruleNumberName,
  384.                 type: 'string',
  385.                 input: function(rule, inputName) {
  386.                     return '<label>比較先項目または値:&nbsp;</label>' +
  387.                         '<input class="matchingValue black-down-triangle" type="text" name="' + inputName + '" list="itemlist2" placeholder=""/>';
  388.                 },
  389.                 operators: ['equal', 'not_equal', 'greater_or_equal', 'greater', 'less_or_equal', 'less', 'in'],
  390.                 validation: {
  391.                     callback: matchingMumberValidator
  392.                 },
  393.             })
  394.         }
  395.  
  396.         default_source_configs = {
  397.             plugins: default_plugins,
  398.             allow_empty: true,
  399.             filters: default_filters,
  400.             rules: null,
  401.             lang: globalConfig.default_lang,
  402.         };
  403.  
  404.         default_destination_configs = {
  405.             plugins: default_plugins,
  406.             allow_empty: true,
  407.             filters: default_filters,
  408.             rules: null,
  409.             lang: globalConfig.default_lang,
  410.         };
  411.  
  412.         default_matching_configs = {
  413.             plugins: default_plugins,
  414.             allow_empty: true,
  415.             filters: default_filters_matching,
  416.             rules: null,
  417.             lang: globalConfig.default_lang,
  418.         };
  419.  
  420.         $(sourceBuilderId).queryBuilder(default_source_configs);
  421.         $(destinationBuilderId).queryBuilder(default_destination_configs);
  422.         $(matchingBuilderId).queryBuilder(default_matching_configs);
  423.         var group = $(matchingBuilderId)[0].queryBuilder.model.root;
  424.         if(group){
  425.             group.empty();
  426.         }
  427.  
  428.         $(matchingBuilderId).on('afterUpdateRuleFilter.queryBuilder', function(e, group) {
  429.             setInputAutoComplete("matchingValue");
  430.         });
  431.  
  432.         setButtonClickListenerByName("builder-ec", onExpandCollapseBuilder);
  433.         setButtonClickListenter(switchConditionsBtnId, switchConditions);
  434.         setButtonClickListenter(saveSourceBtnId, saveSourceListData);
  435.         setButtonClickListenter(getSourceBtnId, getSourceListData);
  436.         setButtonClickListenter(saveDestinationBtnId, saveDestinationListData);
  437.         setButtonClickListenter(getDestinationBtnId, getDestinationListData);
  438.         setButtonClickListenter(saveMatchingBtnId, saveMatchingListData);
  439.         setButtonClickListenter(getMatchingBtnId, getMatchingListData);
  440.         setButtonClickListenter(submitFormBtnId, submit);
  441.         setButtonClickListenter(extractSourceBtnId, extractSource);
  442.         setButtonClickListenter(extractDestinationBtnId, extractDestination);
  443.         setButtonClickListenter(sourceNotificationSentBtnId, sendSourceConditions);
  444.         setButtonClickListenter(destinationNotificationSentBtnId, sendDestinationConditions);
  445.         setButtonClickListenter(matchingNotificationSentBtnId, sendMatchingConditions);
  446.         initDuplicateHandle();
  447.         initSameDomainHandle();
  448.         initcheckDomainInPartnerGroup();
  449.         loadDefaultSettings();
  450.         loadConditionNotification();
  451.         $(window).on('beforeunload', saveDefaultSettings);
  452.         $(document).on("keydown", keydownHandler);
  453.     });
  454.  
  455.     function loadConditionNotification(){
  456.         $('#'+sourceNotificationId).off('click');
  457.         $('#'+destinationNotificationId).off('click');
  458.         $('#'+matchingNotificationId).off('click');
  459.         function onSuccess(response) {
  460.             if(response && response.status) {
  461.                 var sourceNotification = response.sourceNotification;
  462.                 var destinationNotification = response.destinationNotification;
  463.                 var matchingNotification = response.matchingNotification;
  464.                 sourceNotificationList = response.sourceNotificationList;
  465.                 destinationNotificationList = response.destinationNotificationList;
  466.                 matchingNotificationList = response.matchingNotificationList;
  467.  
  468.                 updateNotification(sourceNotification, SOURCE_CONDITION);
  469.                 updateNotification(destinationNotification, DESTINATION_CONDITION);
  470.                 updateNotification(matchingNotification, MATCHING_CONDITION);
  471.  
  472.                 $('#'+sourceNotificationId).click(function () {
  473.                     showNotificationModal("メール元抽条件通知", sourceNotificationList);
  474.                 });
  475.  
  476.                 $('#'+destinationNotificationId).click(function () {
  477.                     showNotificationModal("メール先抽出条件通知", destinationNotificationList);
  478.                 });
  479.  
  480.                 $('#'+matchingNotificationId).click(function () {
  481.                     showNotificationModal("マッチング条件通知", matchingNotificationList);
  482.                 });
  483.             }
  484.         }
  485.         function onError() {
  486.             alert('マッチング条件ロードに失敗しました。');
  487.         }
  488.  
  489.         getMatchingConditionNotification(onSuccess, onError);
  490.     }
  491.  
  492.     function updateNotification(notificationNumber, notificationType) {
  493.         var notification = null;
  494.         switch (notificationType) {
  495.             case SOURCE_CONDITION:
  496.                 notification = $('#' + sourceNotificationNewId);
  497.                 break;
  498.             case DESTINATION_CONDITION:
  499.                 notification = $('#' + destinationNotificationNewId);
  500.                 break;
  501.             case MATCHING_CONDITION:
  502.                 notification = $('#' + matchingNotificationNewId);
  503.                 break;
  504.         }
  505.         if(notificationNumber>0){
  506.             notificationNumber = notificationNumber>99? 99 : notificationNumber;
  507.             notification.text(notificationNumber);
  508.             notification.removeClass('hidden');
  509.         }else{
  510.             notification.addClass('hidden');
  511.         }
  512.     }
  513.  
  514.     function sendSourceConditions() {
  515.         var toAccount = $('#' + sourceNotificationAccountId).val();
  516.         if(!toAccount || toAccount==null){
  517.             $.alert("アカウントを最初に選択してください。");
  518.             return;
  519.         }
  520.         var sourceConditions = $(sourceBuilderId).queryBuilder('getRules');
  521.         if(!sourceConditions) return;
  522.         sendConditions(toAccount, sourceConditions, SOURCE_CONDITION);
  523.     }
  524.  
  525.     function sendDestinationConditions() {
  526.         var toAccount = $('#' + destinationNotificationAccountId).val();
  527.         if(!toAccount || toAccount==null){
  528.             $.alert("アカウントを最初に選択してください。");
  529.             return;
  530.         }
  531.         var destinationConditions = $(destinationBuilderId).queryBuilder('getRules');
  532.         if(!destinationConditions) return;
  533.         sendConditions(toAccount, destinationConditions, DESTINATION_CONDITION);
  534.     }
  535.  
  536.     function sendMatchingConditions() {
  537.         var toAccount = $('#' + matchingNotificationAccountId).val();
  538.         if(!toAccount || toAccount==null){
  539.             $.alert("アカウントを最初に選択してください。");
  540.             return;
  541.         }
  542.         var matchingConditions = $(matchingBuilderId).queryBuilder('getRules');
  543.         if(!matchingConditions) return;
  544.         sendConditions(toAccount, matchingConditions, MATCHING_CONDITION);
  545.     }
  546.  
  547.  
  548.     function sendConditions(toAccount, conditions, conditionType) {
  549.         var condition = JSON.stringify(conditions);
  550.         var sendBtn = null;
  551.         switch (conditionType) {
  552.             case SOURCE_CONDITION:
  553.                 sendBtn = $(sourceNotificationSentBtnId);
  554.                 break;
  555.             case DESTINATION_CONDITION:
  556.                 sendBtn = $(destinationNotificationSentBtnId);
  557.                 break;
  558.             case MATCHING_CONDITION:
  559.                 sendBtn = $(matchingNotificationSentBtnId);
  560.                 break;
  561.         }
  562.         sendBtn.button('loading');
  563.         function onSuccess(response) {
  564.             if(response && response.status) {
  565.                 $.alert("条件送信に成功しました。");
  566.             } else {
  567.                 $.alert("条件送信に失敗しました。");
  568.             }
  569.             sendBtn.button('reset');
  570.         }
  571.  
  572.         function onError(response) {
  573.             $.alert("条件送信に失敗しました。");
  574.             sendBtn.button('reset');
  575.         }
  576.  
  577.         addConditionNotification({
  578.             toAccountId: toAccount,
  579.             condition: condition,
  580.             conditionType: conditionType,
  581.         }, onSuccess, onError)
  582.     }
  583.  
  584.     function onExpandCollapseBuilder() {
  585.         var builderId = this.getAttribute("data");
  586.         if(builderId) {
  587.             var builder = $(builderId);
  588.             $(this).html(builder.is(":visible") ? "+" : "ー");
  589.             if(builder.is(":visible")){
  590.                 $(builderId).slideToggle(200, function () {
  591.                     $(builderId + collapseViewPostfix).slideToggle(200, function () {
  592.  
  593.                     })
  594.                 })
  595.             } else {
  596.                 $(builderId + collapseViewPostfix).slideToggle(200, function () {
  597.                     $(builderId).slideToggle(200, function () {
  598.  
  599.                     })
  600.                 });
  601.             }
  602.         }
  603.     }
  604.  
  605.     function switchConditions() {
  606.         var sourceConditions = $(sourceBuilderId).queryBuilder('getRules');
  607.         var destinationConditions = $(destinationBuilderId).queryBuilder('getRules');
  608.         if ($.isEmptyObject(sourceConditions)) return;
  609.         if ($.isEmptyObject(destinationConditions)) return;
  610.         replaceCondition(destinationConditions);
  611.         $(sourceBuilderId).queryBuilder('setRules', destinationConditions);
  612.         replaceCondition(sourceConditions);
  613.         $(destinationBuilderId).queryBuilder('setRules', sourceConditions);
  614.         var sourceConditionName = getInputValue(sourceConditionNameId);
  615.         var destinationConditionName = getInputValue(destinationConditionNameId);
  616.         setInputValue(sourceConditionNameId, destinationConditionName);
  617.         setInputValue(destinationConditionNameId, sourceConditionName);
  618.     }
  619.  
  620.     function keydownHandler(e) {
  621.         if((e.which || e.keyCode) == 114) {
  622.             e.preventDefault();
  623.             $(extractSourceBtnId).click();
  624.         } else if((e.which || e.keyCode) == 115) {
  625.             e.preventDefault();
  626.             $(extractDestinationBtnId).click();
  627.         } else if((e.which || e.keyCode) == 116) {
  628.             e.preventDefault();
  629.             $(submitFormBtnId).click();
  630.         }
  631.     }
  632.  
  633.     function saveDefaultSettings() {
  634.         var sourceConditions = $(sourceBuilderId).queryBuilder('getRules');
  635.         localStorage.setItem("sourceConditions", JSON.stringify(sourceConditions));
  636.         var destinationConditions = $(destinationBuilderId).queryBuilder('getRules');
  637.         localStorage.setItem("destinationConditions", JSON.stringify(destinationConditions));
  638.         var matchingConditions = $(matchingBuilderId).queryBuilder('getRules');
  639.         localStorage.setItem("matchingConditions", JSON.stringify(matchingConditions));
  640.         // var spaceEffective = $('input[name=spaceEffective]:checked', formId).val() === "true";
  641.         // var distinguish = $('input[name=distinguish]:checked', formId).val() === "true";
  642.         var spaceEffective = false;
  643.         var distinguish = false;
  644.         localStorage.setItem("spaceEffective", spaceEffective);
  645.         localStorage.setItem("distinguish", distinguish);
  646.         var matchingWords = $(matchingWordsAreaId).val();
  647.         matchingWords = matchingWords.toLocaleLowerCase();
  648.         matchingWords = matchingWords.trim();
  649.         localStorage.setItem("matchingWords", matchingWords);
  650.         localStorage.setItem(getCollapseKey(sourceBuilderId), $(sourceBuilderId).is(":hidden"));
  651.         localStorage.setItem(getCollapseKey(destinationBuilderId), $(destinationBuilderId).is(":hidden"));
  652.         localStorage.setItem(getCollapseKey(matchingBuilderId), $(matchingBuilderId).is(":hidden"));
  653.         var sourceConditionName = getInputValue(sourceConditionNameId);
  654.         var destinationConditionName = getInputValue(destinationConditionNameId);
  655.         var matchingConditionName = getInputValue(matchingConditionNameId);
  656.         localStorage.setItem("sourceConditionName", sourceConditionName);
  657.         localStorage.setItem("destinationConditionName", destinationConditionName);
  658.         localStorage.setItem("matchingConditionName", matchingConditionName);
  659.     }
  660.  
  661.     function getCollapseKey(builderId) {
  662.         return collapsedPrefixKey + "-" + builderId;
  663.     }
  664.  
  665.     function loadDefaultSettings() {
  666.         loadExpandCollapseSetting(sourceBuilderId);
  667.         loadExpandCollapseSetting(destinationBuilderId);
  668.         loadExpandCollapseSetting(matchingBuilderId);
  669.         var sourceConditionsStr = localStorage.getItem("sourceConditions");
  670.         var sourceConditions = sourceConditionsStr == null || JSON.parse(sourceConditionsStr) == null ? default_source_rules : JSON.parse(sourceConditionsStr);
  671.         replaceCondition(sourceConditions);
  672.         $(sourceBuilderId).queryBuilder('setRules', sourceConditions);
  673.         var destinationConditionsStr = localStorage.getItem("destinationConditions");
  674.         var destinationConditions = destinationConditionsStr == null || JSON.parse(destinationConditionsStr) == null ? default_destination_rules : JSON.parse(destinationConditionsStr);
  675.         replaceCondition(destinationConditions);
  676.         $(destinationBuilderId).queryBuilder('setRules', destinationConditions);
  677.         var matchingConditionsStr = localStorage.getItem("matchingConditions");
  678.         var matchingConditions = matchingConditionsStr == null || JSON.parse(matchingConditionsStr) == null ? {condition: "AND", rules: []} : JSON.parse(matchingConditionsStr);
  679.         replaceCondition(matchingConditions);
  680.         $(matchingBuilderId).queryBuilder('setRules', matchingConditions);
  681.         // var spaceEffective = localStorage.getItem("spaceEffective");
  682.         // spaceEffective = spaceEffective == "true" ? true : false;
  683.         // $("#spaceEffective1").prop("checked", !spaceEffective);
  684.         // $("#spaceEffective2").prop("checked", spaceEffective);
  685.         // var distinguish = localStorage.getItem("distinguish");
  686.         // distinguish = distinguish == "true" ? true : false;
  687.         // $("#distinguish1").prop("checked", !distinguish);
  688.         // $("#distinguish2").prop("checked", distinguish);
  689.         var sourceConditionName = localStorage.getItem("sourceConditionName") || "未登録の条件";
  690.         var destinationConditionName = localStorage.getItem("destinationConditionName") || "未登録の条件";
  691.         var matchingConditionName = localStorage.getItem("matchingConditionName") || "未登録の条件";
  692.         setInputValue(sourceConditionNameId, sourceConditionName);
  693.         setInputValue(destinationConditionNameId, destinationConditionName);
  694.         setInputValue(matchingConditionNameId, matchingConditionName);
  695.         var matchingWords = localStorage.getItem("matchingWords");
  696.         $(matchingWordsAreaId).val(matchingWords);
  697.     }
  698.  
  699.     function loadExpandCollapseSetting(builderId) {
  700.         var isHidden = localStorage.getItem(getCollapseKey(builderId)) === "true";
  701.         var $builder = $(builderId);
  702.         var $collapseView = $(builderId + collapseViewPostfix);
  703.         var $button = $builder.parent().parent().find("button[name='builder-ec']");
  704.         if(isHidden) {
  705.             $button.html("+");
  706.             $builder.hide();
  707.             $collapseView.show();
  708.         } else {
  709.             $button.html("ー");
  710.             $collapseView.hide();
  711.             $builder.show();
  712.         }
  713.     }
  714.  
  715.     function setButtonClickListenter(id, callback) {
  716.         $(id).off('click');
  717.         $(id).click(function () {
  718.             if(typeof callback === "function"){
  719.                 callback();
  720.             }
  721.         });
  722.     }
  723.  
  724.     function setButtonClickListenerByName(name, callback) {
  725.         $("button[name='"+name+"']").off('click');
  726.         $("button[name='"+name+"']").click(function () {
  727.             if(typeof callback == "function"){
  728.                 callback.apply(this);
  729.             }
  730.         })
  731.     }
  732.  
  733.     var SOURCE_CONDITION;
  734.     var DESTINATION_CONDITION;
  735.     var MATCHING_CONDITION;
  736.  
  737.     function saveSourceListData(){
  738.         SOURCE_CONDITION = 1;
  739.         var result = $(sourceBuilderId).queryBuilder('getRules');
  740.         if ($.isEmptyObject(result)) return;
  741.         var datalistStr = localStorage.getItem(sourceListKey);
  742.         var datalist = JSON.parse(datalistStr);
  743.         datalist = datalist || [];
  744.         var defaultPromptName = getInputValue(sourceConditionNameId);
  745.         showNamePrompt(datalist, sourceListKey, sourcePrefixUrlKey, defaultPromptName, function (name) {
  746.             if (name != null && name.length > 0) {
  747.                 if(datalist.indexOf(name) < 0){
  748.                     datalist.push(name);
  749.                 }
  750.                 var data = {
  751.                     conditionName : name,
  752.                     condition: JSON.stringify(result),
  753.                     conditionType: SOURCE_CONDITION
  754.                 }
  755.                 function onSuccess(response) {
  756.                     if(response && response.status) {
  757.  
  758.                     }
  759.                 }
  760.  
  761.                 function onError(response) {
  762.                     $.alert("add condition fail2");
  763.                 }
  764.                 console.log(data)
  765.                 console.log(result)
  766.                 addConditionSaved(data, onSuccess, onError);
  767.             }
  768.         })
  769.  
  770.     }
  771.  
  772.     function showNamePrompt(datalist, listKey, prefixUrlKey, defaultName, callback) {
  773.         $('#dataModal').modal();
  774.         $( '#dataModalName').val(defaultName);
  775.         updateKeyList(datalist);
  776.         $("#dataModalName").off("change paste keyup");
  777.         $("#dataModalName").on("change paste keyup", disableRemoveDatalistItem);
  778.         setInputAutoComplete("dataModalName");
  779.         $(removeDatalistItemBtnId).off('click');
  780.         $(removeDatalistItemBtnId).click(function () {
  781.             var name = $( '#dataModalName').val();
  782.             removeDatalistItem(listKey, prefixUrlKey, name);
  783.         });
  784.         $('#dataModalOk').off('click');
  785.         $("#dataModalOk").click(function () {
  786.             var name = $( '#dataModalName').val();
  787.             $('#dataModal').modal('hide');
  788.             if(typeof callback === "function"){
  789.                 callback(name);
  790.             }
  791.         });
  792.         $('#dataModalCancel').off('click');
  793.         $("#dataModalCancel").click(function () {
  794.             $('#dataModal').modal('hide');
  795.             if(typeof callback === "function"){
  796.                 callback();
  797.             }
  798.         });
  799.     }
  800.  
  801.     function removeDatalistItem(listKey, prefixUrlKey, name){
  802.         var datalistStr = localStorage.getItem(listKey);
  803.         var datalist = JSON.parse(datalistStr);
  804.         var index = datalist.indexOf(name);
  805.         if (index > -1) {
  806.             datalist.splice(index, 1);
  807.         }
  808.         localStorage.setItem(listKey, JSON.stringify(datalist));
  809.         localStorage.removeItem(prefixUrlKey + "@" + name);
  810.         $( '#dataModalName').val('');
  811.         updateKeyList(datalist);
  812.     }
  813.  
  814.     function updateKeyList(datalist) {
  815.         datalist = datalist || [];
  816.         $('#keylist').html('');
  817.         for(var i = 0; i < datalist.length; i++){
  818.             $('#keylist').append("<option value='" + datalist[i] + "'>");
  819.         }
  820.     }
  821.  
  822.     function disableRemoveDatalistItem() {
  823.         var name = $( '#dataModalName').val();
  824.         if(!name || name.trim().length === 0){
  825.             disableButton(removeDatalistItemBtnId, true);
  826.         } else {
  827.             disableButton(removeDatalistItemBtnId, false);
  828.         }
  829.     }
  830.  
  831.     function disableButton(buttonId, disabled) {
  832.         if(buttonId && buttonId.length > 0){
  833.             $(buttonId).prop("disabled", disabled);
  834.         }
  835.     }
  836.  
  837.     function saveDestinationListData(){
  838.         var result = $(destinationBuilderId).queryBuilder('getRules');
  839.         if ($.isEmptyObject(result)) return;
  840.         var datalistStr = localStorage.getItem(destinationListKey);
  841.         var datalist = JSON.parse(datalistStr);
  842.         datalist = datalist || [];
  843.         var defaultPromptName = getInputValue(destinationConditionNameId);
  844.         showNamePrompt(datalist, destinationListKey, destinationPrefixUrlKey, defaultPromptName, function (name) {
  845.             if (name != null && name.length > 0) {
  846.                 if(datalist.indexOf(name) < 0){
  847.                     datalist.push(name);
  848.                 }
  849.                 localStorage.setItem(destinationListKey, JSON.stringify(datalist));
  850.                 saveListData(
  851.                     destinationPrefixUrlKey,
  852.                     name,
  853.                     result
  854.                 )
  855.             }
  856.         })
  857.     }
  858.  
  859.     function saveMatchingListData(){
  860.         var result = $(matchingBuilderId).queryBuilder('getRules');
  861.         if ($.isEmptyObject(result)) return;
  862.         var datalistStr = localStorage.getItem(matchingListKey);
  863.         var datalist = JSON.parse(datalistStr);
  864.         datalist = datalist || [];
  865.         var defaultPromptName = getInputValue(matchingConditionNameId);
  866.         showNamePrompt(datalist, matchingListKey, matchingPrefixUrlKey, defaultPromptName, function (name) {
  867.             if (name != null && name.length > 0) {
  868.                 if(datalist.indexOf(name) < 0){
  869.                     datalist.push(name);
  870.                 }
  871.                 localStorage.setItem(matchingListKey, JSON.stringify(datalist));
  872.                 saveListData(
  873.                     matchingPrefixUrlKey,
  874.                     name,
  875.                     result
  876.                 )
  877.             }
  878.         })
  879.     }
  880.  
  881.     function saveListData(url, name,  data) {
  882.         var key = url + "@" + name;
  883.         var inputId = getInputIdFromUrl(url);
  884.         setInputValue(inputId, name);
  885.         localStorage.setItem(key, JSON.stringify(data));
  886.     }
  887.  
  888.     function setInputAutoComplete(className) {
  889.         $( "." + className ).off('click');
  890.         $( "." + className ).off('mouseleave');
  891.         $( "." + className ).on('click', function() {
  892.             $(this).attr('placeholder',$(this).val());
  893.             $(this).val('');
  894.             disableRemoveDatalistItem();
  895.         });
  896.         $( "." + className ).on('mouseleave', function() {
  897.             if ($(this).val() == '') {
  898.                 $(this).val($(this).attr('placeholder'));
  899.             }
  900.         });
  901.     }
  902.  
  903.     var conditionSavedArr = [];
  904.  
  905.     function getSourceListData() {
  906.         getAllConditionSaved()
  907.         var datalistStr = localStorage.getItem(sourceListKey);
  908.         var datalist = JSON.parse(datalistStr);
  909.         var datalist = JSON.parse(datalistStr);
  910.         datalist = datalist || [];
  911.         console.log(datalist)
  912.         showNamePrompt(datalist, sourceListKey, sourcePrefixUrlKey, "", function (name) {
  913.             console.log(sourceBuilderId)
  914.             if (name != null && name.length > 0) {
  915.                 getListData(sourcePrefixUrlKey, name, sourceBuilderId);
  916.  
  917.             }
  918.         })
  919.     }
  920.  
  921.  
  922.     function getDestinationListData(skip) {
  923.         var datalistStr = localStorage.getItem(destinationListKey);
  924.         var datalist = JSON.parse(datalistStr);
  925.         datalist = datalist || [];
  926.         showNamePrompt(datalist, destinationListKey, destinationPrefixUrlKey, "", function (name) {
  927.             if (name != null && name.length > 0) {
  928.                 getListData(destinationPrefixUrlKey, name, destinationBuilderId);
  929.             }
  930.         })
  931.     }
  932.  
  933.     function getMatchingListData(skip) {
  934.         var datalistStr = localStorage.getItem(matchingListKey);
  935.         var datalist = JSON.parse(datalistStr);
  936.         datalist = datalist || [];
  937.         showNamePrompt(datalist, matchingListKey, matchingPrefixUrlKey, "", function (name) {
  938.             if (name != null && name.length > 0) {
  939.                 getListData(matchingPrefixUrlKey, name, matchingBuilderId, true);
  940.             }
  941.         })
  942.     }
  943.  
  944.     async function getListData(url, name, builderId, skipAddDefaultRow) {
  945.         var data = null;
  946.         if(name && name.length > 0){
  947.             function onSuccess(response) {
  948.                 if(response && response.status) {
  949.                     $.alert({
  950.                         title: "",
  951.                         content: "add condition success",
  952.                         onClose: function () {
  953.                             loadConditionSaved();
  954.                         }
  955.                     });
  956.                 } else {
  957.                     $.alert("add condition fail");
  958.                 }
  959.             }
  960.  
  961.             function onError(response) {
  962.                 $.alert("add condition fail");
  963.             }
  964.             data =  await getSourceConditionSaved(name, 1, onSuccess, onError);
  965.             data = JSON.parse(data);
  966.  
  967.             console.log(data)
  968.         }
  969.         if(data != null){
  970.             // var enableAddDefaultRow = !skipAddDefaultRow;
  971.             // if(enableAddDefaultRow) {
  972.             //     data = addDefaultReceiveDateRow(data);
  973.             // }
  974.             var inputId = getInputIdFromUrl(url);
  975.             setInputValue(inputId, name);
  976.             replaceCondition(data);
  977.             $(builderId).queryBuilder('setRules', data);
  978.         } else {
  979.             alert("見つけませんでした。");
  980.         }
  981.     }
  982.  
  983.     function addDefaultReceiveDateRow(data) {
  984.         var receivedDateCondition = null;
  985.         if(data.rules) {
  986.             for(var i = 0; i < data.rules.length; i ++){
  987.                 var condition = data.rules[i];
  988.                 if(condition.id == "8"){
  989.                     receivedDateCondition = condition;
  990.                 }
  991.             }
  992.         }
  993.         if(receivedDateCondition == null){
  994.             receivedDateCondition = {
  995.                 id: "8",
  996.                 operator: "greater_or_equal",
  997.                 type:  "string",
  998.                 value: "-7"
  999.             }
  1000.             data.rules = data.rules ? data.rules : [];
  1001.             data.rules.push(receivedDateCondition);
  1002.         }
  1003.         return data;
  1004.     }
  1005.  
  1006.     function buildDataFromBuilder(builderId) {
  1007.         var result = $(builderId).queryBuilder('getRules');
  1008.         if ($.isEmptyObject(result)) return;
  1009.         return buildGroupDataFromRaw(result);
  1010.     }
  1011.  
  1012.     function buildGroupDataFromRaw(data){
  1013.         var result = {
  1014.             condition: data.condition,
  1015.             rules: buildRulesDataFromRaw(data)
  1016.         }
  1017.         return result;
  1018.     }
  1019.  
  1020.     function buildRulesDataFromRaw(data) {
  1021.         var result = [];
  1022.         for(var i = 0; i < data.rules.length; i++){
  1023.             var rawRule = data.rules[i];
  1024.             if(rawRule.id){
  1025.                 var rule = {
  1026.                     id: rawRule.id,
  1027.                     operator: rawRule.operator,
  1028.                     type: rawRule.type,
  1029.                     value: rawRule.value
  1030.                 }
  1031.                 result.push(rule);
  1032.             } else if (rawRule.condition) {
  1033.                 var rule = buildGroupDataFromRaw(rawRule);
  1034.                 result.push(rule);
  1035.             }
  1036.         }
  1037.         return result;
  1038.     }
  1039.  
  1040.     function extractSource() {
  1041.         var sourceConditionData = buildDataFromBuilder(sourceBuilderId);
  1042.         if(!sourceConditionData) return;
  1043.         var duplicateSettingData = getCachedDuplicationSettingData();
  1044.         var data = {
  1045.             "conditionData" : sourceConditionData,
  1046.             // "distinguish": $('input[name=distinguish]:checked', formId).val() === "true",
  1047.             // "spaceEffective": $('input[name=spaceEffective]:checked', formId).val() === "true",
  1048.             "distinguish": false,
  1049.             "spaceEffective": false,
  1050.             "handleDuplicateSender": duplicateSettingData.handleDuplicateSender,
  1051.             "handleDuplicateSubject": duplicateSettingData.handleDuplicateSubject,
  1052.             "type": 1,
  1053.         };
  1054.         sessionStorage.setItem("extractSourceData", JSON.stringify(data));
  1055.         saveDefaultSettings();
  1056.         var win = window.open('/user/extractSource', '_blank');
  1057.         if (win) {
  1058.             //Browser has allowed it to be opened
  1059.             win.focus();
  1060.         } else {
  1061.             //Browser has blocked it
  1062.             alert('このサイトのポップアップを許可してください');
  1063.         }
  1064.     }
  1065.  
  1066.     function extractDestination() {
  1067.         var destinationConditionData = buildDataFromBuilder(destinationBuilderId);
  1068.         if(!destinationConditionData) return;
  1069.         var duplicateSettingData = getCachedDuplicationSettingData();
  1070.         var data = {
  1071.             "conditionData" : destinationConditionData,
  1072.             // "distinguish": $('input[name=distinguish]:checked', formId).val() === "true",
  1073.             // "spaceEffective": $('input[name=spaceEffective]:checked', formId).val() === "true",
  1074.             "distinguish": false,
  1075.             "Please allow popups for this websitespaceEffective": false,
  1076.             "handleDuplicateSender": duplicateSettingData.handleDuplicateSender,
  1077.             "handleDuplicateSubject": duplicateSettingData.handleDuplicateSubject,
  1078.             "type": 2,
  1079.         };
  1080.         sessionStorage.setItem("extractDestinationData", JSON.stringify(data));
  1081.         saveDefaultSettings();
  1082.         var win = window.open('/user/extractDestination', '_blank');
  1083.         if (win) {
  1084.             //Browser has allowed it to be opened
  1085.             win.focus();
  1086.         } else {
  1087.             //Browser has blocked it
  1088.             alert('このサイトのポップアップを許可してください');
  1089.         }
  1090.     }
  1091.  
  1092.     function submit() {
  1093.         var sourceConditionData = buildDataFromBuilder(sourceBuilderId);
  1094.         if(!sourceConditionData) return;
  1095.         var destinationConditionData = buildDataFromBuilder(destinationBuilderId);
  1096.         if(!destinationConditionData) return;
  1097.         var matchingConditionData = buildDataFromBuilder(matchingBuilderId);
  1098.         if(!matchingConditionData) return;
  1099.         var matchingWords = $(matchingWordsAreaId).val();
  1100.         matchingWords = matchingWords.toLocaleLowerCase();
  1101.         matchingWords = matchingWords.trim();
  1102.         // var spaceEffective = $('input[name=spaceEffective]:checked', formId).val() === "true";
  1103.         // var distinguish = $('input[name=distinguish]:checked', formId).val() === "true";
  1104.         var spaceEffective = false;
  1105.         var distinguish = false;
  1106.         var duplicateSettingData = getCachedDuplicationSettingData();
  1107.         var form = {
  1108.             "sourceConditionData" : sourceConditionData,
  1109.             "destinationConditionData" : destinationConditionData,
  1110.             "matchingConditionData" : matchingConditionData,
  1111.             "matchingWords": matchingWords,
  1112.             "distinguish": distinguish,
  1113.             "spaceEffective": spaceEffective,
  1114.             "handleDuplicateSender": duplicateSettingData.handleDuplicateSender,
  1115.             "handleDuplicateSubject": duplicateSettingData.handleDuplicateSubject,
  1116.             "handleSameDomain": getCachedSameDomainSettingData(),
  1117.             "checkDomainInPartnerGroup": getCachedCheckDomainInPartnerGroupSettingData(),
  1118.         };
  1119.         sessionStorage.setItem("distinguish", distinguish);
  1120.         sessionStorage.setItem("spaceEffective", spaceEffective);
  1121.         sessionStorage.setItem("matchingConditionData", JSON.stringify(form));
  1122.         // window.location = '/user/matchingResult';
  1123.         saveDefaultSettings();
  1124.         var win = window.open('/user/matchingResult', '_blank');
  1125.         if (win) {
  1126.             //Browser has allowed it to be opened
  1127.             win.focus();
  1128.         } else {
  1129.             //Browser has blocked it
  1130.             alert('このサイトのポップアップを許可してください');
  1131.         }
  1132.     }
  1133.  
  1134.  
  1135.     function initDuplicateHandle() {
  1136.         var duplicateSettingData = getCachedDuplicationSettingData();
  1137.         $('#enable-duplicate-handle').prop('checked', duplicateSettingData.enable);
  1138.         duplicateSettingData.enable ? $('.duplicate-control.duplicate-control-option').show() : $('.duplicate-control.duplicate-control-option').hide();
  1139.         $('#duplicate-sender').prop('checked', duplicateSettingData.sender);
  1140.         $('#duplicate-subject').prop('checked', duplicateSettingData.subject);
  1141.         $('#enable-duplicate-handle').change(function() {
  1142.             var enable = $(this).is(":checked");
  1143.             enable ? $('.duplicate-control.duplicate-control-option').show() : $('.duplicate-control.duplicate-control-option').hide();
  1144.             localStorage.setItem("enableDuplicateHandle", enable);
  1145.         });
  1146.  
  1147.         $('#duplicate-sender').change(function() {
  1148.             var senderEnable = $(this).is(":checked");
  1149.             localStorage.setItem("handleDuplicateSender", senderEnable);
  1150.         });
  1151.  
  1152.         $('#duplicate-subject').change(function() {
  1153.             var subjectEnable = $(this).is(":checked");
  1154.             localStorage.setItem("handleDuplicateSubject", subjectEnable);
  1155.         });
  1156.     }
  1157.  
  1158.     function getCachedDuplicationSettingData() {
  1159.         var enableDuplicateHandleData = localStorage.getItem("enableDuplicateHandle");
  1160.         var enableDuplicateHandle = typeof enableDuplicateHandleData !== "string" ? false : !!JSON.parse(enableDuplicateHandleData);
  1161.         var handleDuplicateSenderData = localStorage.getItem("handleDuplicateSender");
  1162.         var handleDuplicateSender = typeof handleDuplicateSenderData !== "string" ? false : !!JSON.parse(handleDuplicateSenderData);
  1163.         var handleDuplicateSubjectData = localStorage.getItem("handleDuplicateSubject");
  1164.         var handleDuplicateSubject = typeof handleDuplicateSubjectData !== "string" ? false : !!JSON.parse(handleDuplicateSubjectData);
  1165.         return {
  1166.             enable: enableDuplicateHandle,
  1167.             sender: handleDuplicateSender,
  1168.             handleDuplicateSender: enableDuplicateHandle && handleDuplicateSender,
  1169.             subject: handleDuplicateSubject,
  1170.             handleDuplicateSubject: enableDuplicateHandle && handleDuplicateSubject,
  1171.         }
  1172.     }
  1173.  
  1174.     function initSameDomainHandle() {
  1175.         var enableSameDomainHandle = getCachedSameDomainSettingData();
  1176.         $('#enable-same-domain-handle').prop('checked', enableSameDomainHandle);
  1177.         $('#enable-same-domain-handle').change(function() {
  1178.             var enable = $(this).is(":checked");
  1179.             localStorage.setItem("enableSameDomainHandle", enable);
  1180.         });
  1181.     }
  1182.  
  1183.     function getCachedSameDomainSettingData() {
  1184.         var enableSameDomainHandleData = localStorage.getItem("enableSameDomainHandle");
  1185.         var enableSameDomainHandle = typeof enableSameDomainHandleData !== "string" ? false : !!JSON.parse(enableSameDomainHandleData);
  1186.         return enableSameDomainHandle;
  1187.     }
  1188.  
  1189.     function initcheckDomainInPartnerGroup() {
  1190.         var checkDomainInPartnerGroup = getCachedCheckDomainInPartnerGroupSettingData();
  1191.         $(checkDomainInPartnerGroupId).prop('checked', checkDomainInPartnerGroup);
  1192.         $(checkDomainInPartnerGroupId).change(function() {
  1193.             var enable = $(this).is(":checked");
  1194.             localStorage.setItem(checkDomainInPartnerGroupKey, enable);
  1195.         });
  1196.     }
  1197.  
  1198.     function getCachedCheckDomainInPartnerGroupSettingData() {
  1199.         var checkDomainInPartnerGroupData = localStorage.getItem(checkDomainInPartnerGroupKey);
  1200.         var enablecheckDomainInPartnerGroup = typeof checkDomainInPartnerGroupData !== "string" ? false : !!JSON.parse(checkDomainInPartnerGroupData);
  1201.         return enablecheckDomainInPartnerGroup;
  1202.     }
  1203.  
  1204.     function getInputIdFromUrl(url) {
  1205.         switch (url) {
  1206.             case sourcePrefixUrlKey:
  1207.                 return sourceConditionNameId;
  1208.             case destinationPrefixUrlKey:
  1209.                 return destinationConditionNameId;
  1210.             case matchingPrefixUrlKey:
  1211.                 return matchingConditionNameId;
  1212.             default:
  1213.                 return sourceConditionNameId;
  1214.         }
  1215.     }
  1216.  
  1217.     function setInputValue(inputId, value) {
  1218.         $(inputId).val(value);
  1219.     }
  1220.  
  1221.     function getInputValue(inputId) {
  1222.         return $(inputId).val();
  1223.     }
  1224.  
  1225.     function replaceCondition(rule) {
  1226.         var rules = rule.rules;
  1227.         if(rules){
  1228.             for(var i=rules.length-1;i>=0;i--){
  1229.                 if(rules[i].id){
  1230.                     for(var j=0;j<ruleInvalidateIds.length;j++){
  1231.                         if(rules[i].id == ruleInvalidateIds[j]){
  1232.                             rules.splice(i, 1);
  1233.                             break;
  1234.                         }
  1235.                     }
  1236.                 }else{
  1237.                     replaceCondition(rules[i]);
  1238.                 }
  1239.             }
  1240.         }
  1241.     }
  1242.  
  1243.     function showNotificationModal(title, notificationList) {
  1244.         $('#notificationModal').modal();
  1245.         $( '#notificationModalTitle').text(title);
  1246.         updateNotificationList(notificationList);
  1247.         $('#notificationModalClose').off('click');
  1248.         $("#notificationModalClose").click(function () {
  1249.             $('#notificationModal').modal('hide');
  1250.         });
  1251.         $('.notification-modal-show-more').addClass('hidden');
  1252.         $('#modal-body-content').off('scroll');
  1253.         $('#modal-body-content').scroll(function() {
  1254.             if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
  1255.                 if($(this).scrollTop() > 0){
  1256.                     $('.notification-modal-show-more').removeClass('hidden');
  1257.                 }
  1258.             }else{
  1259.                 $('.notification-modal-show-more').addClass('hidden');
  1260.             }
  1261.         });
  1262.         $('.notification-modal-show-more').off('click');
  1263.         $('.notification-modal-show-more').click(function () {
  1264.             $('.notification-modal-show-more').text('');
  1265.             $('.notification-modal-show-more').addClass('fa fa-spinner fa-spin');
  1266.             function onSuccess(response) {
  1267.                 if(response && response.status) {
  1268.                     var list = response.list;
  1269.                     for(var i=0;i<list.length;i++){
  1270.                         notificationList.push(list[i]);
  1271.                     }
  1272.                     updateNotificationList(notificationList);
  1273.                 } else {
  1274.                     $.alert("条件通知表示に失敗しました。");
  1275.                 }
  1276.                 $('.notification-modal-show-more').text('もっと見せる');
  1277.                 $('.notification-modal-show-more').addClass('hidden');
  1278.                 $('.notification-modal-show-more').removeClass('fa fa-spinner fa-spin');
  1279.             }
  1280.  
  1281.             function onError(response) {
  1282.                 conditionNotification.status = NOTIFICATION_NEW;
  1283.                 $.alert("条件通知表示に失敗しました。");
  1284.                 $('.notification-modal-show-more').text('もっと見せる');
  1285.                 $('.notification-modal-show-more').removeClass('fa fa-spinner fa-spin');
  1286.             }
  1287.  
  1288.             if(notificationList.length>0){
  1289.                 showMoreConditionNotification(notificationList[notificationList.length - 1], onSuccess, onError)
  1290.             }
  1291.         });
  1292.     }
  1293.  
  1294.     function updateNotificationList(notificationList) {
  1295.         $('#modal-body-content').html("");
  1296.         for(var i=0;i<notificationList.length;i++){
  1297.             switch (notificationList[i].status) {
  1298.                 case NOTIFICATION_NEW:
  1299.                     $('#modal-body-content').append(
  1300.                         '<div style="border: grey solid 1px; padding: 10px; margin-bottom: 10px;">' +
  1301.                         '<span>' + notificationList[i].fromAccount + ' があなたに設定条件のレコードを送信しました。</span>' +
  1302.                         '<button class="btn btn-danger pull-right btn-notification-reject"  style="margin-right: 10px;" data-index="'+ i +'">却下</button>' +
  1303.                         '<button class="btn btn-success pull-right btn-notification-accept" style="margin-right: 10px;" data-index="'+ i +'">同意</button>' +
  1304.                         '<button class="btn btn-primary pull-right btn-notification-preview" style="margin-right: 10px;" data-index="'+ i +'">プレビュー</button>' +
  1305.                         '<br/>' +
  1306.                         '<span style="font-size: 13px; color: grey">受信日付:' + notificationList[i].sentAt + '</span>' +
  1307.                         '</div>'
  1308.                     );
  1309.                     break;
  1310.                 case NOTIFICATION_ACCEPT:
  1311.                     $('#modal-body-content').append(
  1312.                         '<div style="border: grey solid 1px; padding: 10px; margin-bottom: 10px;">' +
  1313.                         '<span>' + notificationList[i].fromAccount + ' からのマッチング条件のレコードを受けました。</span>' +
  1314.                         '<br/>' +
  1315.                         '<span style="font-size: 13px; color: grey">受信日付:' + notificationList[i].sentAt + '</span>' +
  1316.                         '</div>'
  1317.                     );
  1318.                     break;
  1319.                 case NOTIFICATION_REJECT:
  1320.                     $('#modal-body-content').append(
  1321.                         '<div style="border: grey solid 1px; padding: 10px; margin-bottom: 10px;">' +
  1322.                         '<span>' + notificationList[i].fromAccount + ' からのマッチング条件のレコードを却下しました。</span>' +
  1323.                         '<br/>' +
  1324.                         '<span style="font-size: 13px; color: grey">受信日付:' + notificationList[i].sentAt + '</span>' +
  1325.                         '</div>'
  1326.                     );
  1327.                     break;
  1328.             }
  1329.  
  1330.         }
  1331.         $('.btn-notification-preview').click(function () {
  1332.             var index = $(this).attr('data-index');
  1333.             showPreviewModal(notificationList[index]);
  1334.         })
  1335.  
  1336.         $('.btn-notification-accept').click(function () {
  1337.             var index = $(this).attr('data-index');
  1338.             $.confirm({
  1339.                 title: '<b>【条件通知】</b>',
  1340.                 titleClass: 'text-center',
  1341.                 content: '<div class="text-center" style="font-size: 16px;">通知を許可しますか。<br/></div>',
  1342.                 buttons: {
  1343.                     confirm: {
  1344.                         text: 'はい',
  1345.                         action: function(){
  1346.                             if(notificationList[index] && notificationList[index] != null){
  1347.                                 notificationList[index].status = NOTIFICATION_ACCEPT;
  1348.                                 editConditionNotification(notificationList[index], function () {
  1349.                                     downNotification(notificationList[index].conditionType);
  1350.                                     $('#notificationModal').modal('hide');
  1351.                                     applyCondition(notificationList[index]);
  1352.                                 });
  1353.                             }
  1354.                             console.log(notificationList[index]);
  1355.                         }
  1356.                     },
  1357.                     cancel: {
  1358.                         text: 'いいえ',
  1359.                         action: function(){}
  1360.                     },
  1361.                 }
  1362.             });
  1363.         })
  1364.  
  1365.         $('.btn-notification-reject').click(function () {
  1366.             var index = $(this).attr('data-index');
  1367.             $.confirm({
  1368.                 title: '<b>【条件通知】</b>',
  1369.                 titleClass: 'text-center',
  1370.                 content: '<div class="text-center" style="font-size: 16px;">通知を本当に拒否したいですか。<br/></div>',
  1371.                 buttons: {
  1372.                     confirm: {
  1373.                         text: 'はい',
  1374.                         action: function(){
  1375.                             if(notificationList[index] && notificationList[index] != null){
  1376.                                 notificationList[index].status = NOTIFICATION_REJECT;
  1377.                                 editConditionNotification(notificationList[index], function () {
  1378.                                     downNotification(notificationList[index].conditionType);
  1379.                                     $('#notificationModal').modal('hide');
  1380.                                 });
  1381.                             }
  1382.                             console.log(notificationList[index]);
  1383.                         }
  1384.                     },
  1385.                     cancel: {
  1386.                         text: 'いいえ',
  1387.                         action: function(){}
  1388.                     },
  1389.                 }
  1390.             });
  1391.         })
  1392.     }
  1393.  
  1394.     function showPreviewModal(conditionNotification) {
  1395.         switch (conditionNotification.conditionType) {
  1396.             case SOURCE_CONDITION:
  1397.                 $('#preview-builder').queryBuilder(default_source_configs);
  1398.                 break;
  1399.             case DESTINATION_CONDITION:
  1400.                 $('#preview-builder').queryBuilder(default_destination_configs);
  1401.                 break;
  1402.             case MATCHING_CONDITION:
  1403.                 $('#preview-builder').queryBuilder(default_matching_configs);
  1404.                 break;
  1405.         }
  1406.         var condition = jQuery.parseJSON(conditionNotification.condition);
  1407.         replaceCondition(condition);
  1408.         $('#preview-builder').queryBuilder('setRules', condition);
  1409.         $('#previewConditionModal').modal();
  1410.         $( '#previewConditionModalTitle').text("条件プレビュー");
  1411.         $('#previewConditionModalClose').off('click');
  1412.         $("#previewConditionModalClose").click(function () {
  1413.             $('#previewConditionModal').modal('hide');
  1414.         });
  1415.     }
  1416.  
  1417.     function editConditionNotification(conditionNotification, callback) {
  1418.         $('.btn-notification-preview').button('loading');
  1419.         $('.btn-notification-accept').button('loading');
  1420.         $('.btn-notification-reject').button('loading');
  1421.         function onSuccess(response) {
  1422.             if(response && response.status) {
  1423.                 if(typeof callback === "function"){
  1424.                     callback();
  1425.                 }
  1426.             } else {
  1427.                 conditionNotification.status = NOTIFICATION_NEW;
  1428.                 $.alert("条件送信に失敗しました。");
  1429.             }
  1430.             $('.btn-notification-preview').button('reset');
  1431.             $('.btn-notification-accept').button('reset');
  1432.             $('.btn-notification-reject').button('reset');
  1433.         }
  1434.  
  1435.         function onError(response) {
  1436.             conditionNotification.status = NOTIFICATION_NEW;
  1437.             $.alert("条件送信に失敗しました。");
  1438.             $('.btn-notification-preview').button('reset');
  1439.             $('.btn-notification-accept').button('reset');
  1440.             $('.btn-notification-reject').button('reset');
  1441.         }
  1442.  
  1443.         updateConditionNotification(conditionNotification, onSuccess, onError)
  1444.     }
  1445.  
  1446.     function downNotification(conditionType){
  1447.         var number = 0;
  1448.         switch (conditionType) {
  1449.             case SOURCE_CONDITION:
  1450.                 number = $('#' +sourceNotificationNewId).text();
  1451.                 break;
  1452.             case DESTINATION_CONDITION:
  1453.                 number = $('#' +destinationNotificationNewId).text();
  1454.                 break;
  1455.             case MATCHING_CONDITION:
  1456.                 number = $('#' +matchingNotificationNewId).text();
  1457.                 break;
  1458.         }
  1459.         updateNotification(number-1, conditionType);
  1460.     }
  1461.  
  1462.     function applyCondition(conditionNotification){
  1463.         var queryBuilder = null;
  1464.         switch (conditionNotification.conditionType) {
  1465.             case SOURCE_CONDITION:
  1466.                 queryBuilder = $(sourceBuilderId);
  1467.                 break;
  1468.             case DESTINATION_CONDITION:
  1469.                 queryBuilder = $(destinationBuilderId);
  1470.                 break;
  1471.             case MATCHING_CONDITION:
  1472.                 queryBuilder = $(matchingBuilderId);
  1473.                 break;
  1474.         }
  1475.         var condition = jQuery.parseJSON(conditionNotification.condition);
  1476.         replaceCondition(condition);
  1477.         queryBuilder.queryBuilder('setRules', condition);
  1478.     }
  1479.  
  1480. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement