directoryRender: function(data, target, callbacks){ var self = this, directory_html = $(monster.template(self, 'directory-edit', data)), directoryForm = directory_html.find('#directory-form'); self.directoryRenderUserList(data, directory_html); monster.ui.validate(directoryForm, { rules: { 'min_dtmf': { digits: true }, 'max_dtmf': { digits: true } } }); $('*[rel=popover]:not([type="text"])', directory_html).popover({ trigger: 'hover' }); $('*[rel=popover][type="text"]', directory_html).popover({ trigger: 'focus' }); self.winkstartTabs(directory_html); $('.directory-save', directory_html).click(function(ev) { ev.preventDefault(); if(monster.ui.valid(directoryForm)) { var form_data = monster.ui.getFormData('directory-form'); self.directoryCleanFormData(form_data); var old_list = {}, new_list = {}; $('.rows .row:not(#row_no_data)', directory_html).each(function() { new_list[$(this).data('id')] = $('#user_callflow_id', $(this)).val(); }); data.field_data.user_list = { old_list: data.field_data.old_list, new_list: new_list }; self.directorySave(form_data, data, callbacks.save_success); } else { monster.ui.alert(self.i18n.active().callflows.directory.there_were_errors_on_the_form); } }); $('.directory-delete', directory_html).click(function(ev) { ev.preventDefault(); monster.ui.confirm(self.i18n.active().callflows.directory.are_you_sure_you_want_to_delete, function() { self.directoryDelete(data.data.id, callbacks.delete_success); }); }); $('.add_user_div', directory_html).click(function() { var $user = $('#select_user_id', directory_html); var $callflow = $('#callflow_id', directory_html); if($user.val() != 'empty_option_user' && $callflow.val() != 'empty_option_callflow') { var user_id = $user.val(), user_data = { user_id: user_id, user_name: $('#option_user_'+user_id, directory_html).text(), callflow_id: $callflow.val(), field_data: { callflows: data.field_data.callflows }, _t: function(param){ return window.translate['directory'][param]; } }; if($('#row_no_data', directory_html).size() > 0) { $('#row_no_data', directory_html).remove(); } $('.rows', directory_html).prepend(monster.template(self, 'directory-userRow', user_data)); $('#option_user_'+user_id, directory_html).hide(); $user.val('empty_option_user'); $callflow.val('empty_option_callflow'); } else { monster.ui.alert('warning', self.i18n.active().callflows.directory.noDataSelected); } }); $(directory_html).delegate('.action_user.delete', 'click', function() { var user_id = $(this).data('id'); //removes it from the grid $('#row_user_'+user_id, directory_html).remove(); //re-add it to the dropdown $('#option_user_'+user_id, directory_html).show(); //if grid empty, add no data line if($('.rows .row', directory_html).size() == 0) { $('.rows', directory_html).append(monster.template(self, 'directory-userRow')); } }); (target) .empty() .append(directory_html); },
popupHtml.find('#submit_btn').on('click', function(ev) { ev.preventDefault(); var e911FormData = monster.ui.getFormData('e911'); _.extend(dataNumber, { e911: e911FormData }); var callbackSuccess = function callbackSuccess(data) { var phoneNumber = monster.util.formatPhoneNumber(data.data.id), template = self.getTemplate({ name: '!' + self.i18n.active().e911.successE911, data: { phoneNumber: phoneNumber }, submodule: 'e911' }); monster.ui.toast({ type: 'success', message: template }); popup.dialog('destroy').remove(); callbacks.success && callbacks.success(data); }; self.e911UpdateNumber(dataNumber.id, accountId, dataNumber, { success: function(data) { callbackSuccess(data); }, multipleChoices: function(addresses) { var templatePopupAddresses = $(self.getTemplate({ name: 'addressesDialog', data: addresses, submodule: 'e911' })), popupAddress; templatePopupAddresses.find('.address-option').on('click', function() { templatePopupAddresses.find('.address-option.active').removeClass('active'); $(this).addClass('active'); templatePopupAddresses.find('.save-address').removeClass('disabled'); }); templatePopupAddresses.find('.cancel-link').on('click', function() { popupAddress .dialog('destroy') .remove(); }); templatePopupAddresses.find('.save-address').on('click', function() { if (templatePopupAddresses.find('.address-option').hasClass('active')) { var index = templatePopupAddresses.find('.address-option.active').data('id'), dataAddress = addresses.details[index]; _.extend(dataNumber, { e911: dataAddress }); self.e911UpdateNumber(dataNumber.id, accountId, dataNumber, { success: function(data) { popupAddress .dialog('destroy') .remove(); callbackSuccess(data); } }); } }); popupAddress = monster.ui.dialog(templatePopupAddresses, { title: self.i18n.active().e911.chooseAddressPopup.title }); }, invalidAddress: function(data) { monster.ui.alert('error', self.i18n.active().e911.invalidAddress); } }); });
e911Render: function(dataNumber, pAccountId, callbacks) { var self = this, accountId = pAccountId || self.accountId, popupHtml = $(self.getTemplate({ name: 'dialog', data: dataNumber.e911 || {}, submodule: 'e911' })), popup; popupHtml.find('#postal_code').change(function() { var zipCode = $(this).val(); if (zipCode) { self.e911GetAddressFromZipCode({ data: { zipCode: zipCode }, success: function(results) { if (!_.isEmpty(results)) { var length = results[0].address_components.length; popupHtml.find('#locality').val(results[0].address_components[1].long_name); // Last component is country, before last is state, before can be county if exists or city if no county, so we had to change from 3 to length-2. popupHtml.find('#region').val(results[0].address_components[length - 2].short_name); } } }); } }); popupHtml.find('.inline_field > input').keydown(function() { popup.find('.gmap_link_div').hide(); }); popupHtml.find('#submit_btn').on('click', function(ev) { ev.preventDefault(); var e911FormData = monster.ui.getFormData('e911'); _.extend(dataNumber, { e911: e911FormData }); var callbackSuccess = function callbackSuccess(data) { var phoneNumber = monster.util.formatPhoneNumber(data.data.id), template = self.getTemplate({ name: '!' + self.i18n.active().e911.successE911, data: { phoneNumber: phoneNumber }, submodule: 'e911' }); monster.ui.toast({ type: 'success', message: template }); popup.dialog('destroy').remove(); callbacks.success && callbacks.success(data); }; self.e911UpdateNumber(dataNumber.id, accountId, dataNumber, { success: function(data) { callbackSuccess(data); }, multipleChoices: function(addresses) { var templatePopupAddresses = $(self.getTemplate({ name: 'addressesDialog', data: addresses, submodule: 'e911' })), popupAddress; templatePopupAddresses.find('.address-option').on('click', function() { templatePopupAddresses.find('.address-option.active').removeClass('active'); $(this).addClass('active'); templatePopupAddresses.find('.save-address').removeClass('disabled'); }); templatePopupAddresses.find('.cancel-link').on('click', function() { popupAddress .dialog('destroy') .remove(); }); templatePopupAddresses.find('.save-address').on('click', function() { if (templatePopupAddresses.find('.address-option').hasClass('active')) { var index = templatePopupAddresses.find('.address-option.active').data('id'), dataAddress = addresses.details[index]; _.extend(dataNumber, { e911: dataAddress }); self.e911UpdateNumber(dataNumber.id, accountId, dataNumber, { success: function(data) { popupAddress .dialog('destroy') .remove(); callbackSuccess(data); } }); } }); popupAddress = monster.ui.dialog(templatePopupAddresses, { title: self.i18n.active().e911.chooseAddressPopup.title }); }, invalidAddress: function(data) { monster.ui.alert('error', self.i18n.active().e911.invalidAddress); } }); }); popupHtml.find('#remove_e911_btn').on('click', function(e) { e.preventDefault(); self.callApi({ resource: 'numbers.list', data: { accountId: accountId }, success: function(data, status) { var e911Count = _.countBy(data.data.numbers, function(number) { return (number.hasOwnProperty('features') && number.features.indexOf('e911') >= 0); }).true; if (e911Count > 1) { delete dataNumber.e911; self.e911UpdateNumber(dataNumber.id, accountId, dataNumber, { success: function(data) { var phoneNumber = monster.util.formatPhoneNumber(data.data.id), template = self.getTemplate({ name: '!' + self.i18n.active().e911.successE911, data: { phoneNumber: phoneNumber }, submodule: 'e911' }); monster.ui.toast({ type: 'success', message: template }); popup.dialog('destroy').remove(); callbacks.success && callbacks.success(data); } }); } else { monster.ui.alert(self.i18n.active().e911.lastE911Error); } } }); }); popup = monster.ui.dialog(popupHtml, { title: self.i18n.active().e911.dialogTitle }); // Fixing the position of the rotated text using its width var rotatedText = popup.find('#e911_rotated_text'), rotatedTextOffset = rotatedText.width() / 2; rotatedText.css({'top': 40 + rotatedTextOffset + 'px', 'left': 25 - rotatedTextOffset + 'px'}); },
invalidAddress: function(data) { monster.ui.alert('error', self.i18n.active().e911.invalidAddress); }
featureCodeBindEvents: function(template, actions) { var self = this; monster.ui.tooltips(template); template.find('.featurecode_enabled').each(function() { var action_wrapper = $(this).parents('.action_wrapper'), number_field = action_wrapper.find('.featurecode-number'); !$(this).is(':checked') ? $(number_field).attr('disabled', '') : $(number_field).removeAttr('disabled'); }); template.find('.featurecode-number').on('blur keyup focus', function() { var action_wrapper = $(this).parents('.action_wrapper'); action_wrapper.data('number', $(this).val()); $(this).val() !== actions[action_wrapper.data('action')].number ? action_wrapper.addClass('changed') : action_wrapper.removeClass('changed'); }); template.find('.featurecode_enabled').on('change', function() { //$('.featurecode_enabled', template).change(function() { var $this = $(this), action_wrapper = $this.parents('.action_wrapper'), number_field = action_wrapper.find('.featurecode-number'); if (!$this.is(':checked') && action_wrapper.data('enabled') === true) { action_wrapper.addClass('disabled'); } else if ($this.is(':checked') && action_wrapper.data('enabled') === false) { action_wrapper.addClass('enabled'); } else { action_wrapper.removeClass('enabled'); action_wrapper.removeClass('disabled'); } !$this.is(':checked') ? number_field.attr('disabled', '') : number_field.removeAttr('disabled'); }); template.find('.featurecode-save').on('click', function(e) { var $this = $(this); e.preventDefault(); if (!$this.hasClass('disabled')) { var formData = self.featureCodeCleanFormData(template, actions); $this.addClass('disabled'); self.featureCodeMassUpdate(formData, function() { monster.ui.toast({ type: 'success', message: self.i18n.active().callflows.featureCodes.successUpdate }); $this.removeClass('disabled'); self.featureCodeRender(); }, function() { $this.removeClass('disabled'); }); } }); _.each(actions, function(item) { if (item.hasOwnProperty('editConfiguration')) { template.find('[data-id="' + item.id + '"] .edit-configuration-link').on('click', function() { item.editConfiguration(); }); } }); },
featureCodeMassUpdate: function(form_data, callback, errorCallback) { var self = this, count = form_data.created_callflows.length + form_data.deleted_callflows.length + form_data.updated_callflows.length; if (count) { var parallelRequests = {}; _.each(form_data.created_callflows, function(callflow) { parallelRequests['create_' + callflow.action] = function(callback) { var dataCallflow = { flow: callflow.flow, patterns: callflow.patterns, numbers: callflow.numbers, featurecode: { name: callflow.action, number: callflow.number } }; self.featureCodeCreate(dataCallflow, function(data) { callback && callback(null, data); }); }; }); _.each(form_data.updated_callflows, function(callflow) { parallelRequests['update_' + callflow.action] = function(callback) { var dataCallflow = { flow: callflow.flow, patterns: callflow.patterns, numbers: callflow.numbers, featurecode: { name: callflow.action, number: callflow.number } }; self.featureCodeUpdate(callflow.id, dataCallflow, function(data) { callback && callback(null, data); }); }; }); _.each(form_data.deleted_callflows, function(callflow) { parallelRequests['delete_' + callflow.action] = function(callback) { self.featureCodeDelete(callflow.id, function(data) { callback && callback(null, data); }); }; }); monster.parallel(parallelRequests, function(err, results) { callback && callback(); }); } else { errorCallback && errorCallback(); monster.ui.toast({ type: 'error', message: self.i18n.active().callflows.featureCodes.nothing_to_save }); } },