Last active
August 29, 2015 14:06
-
-
Save ramrrr/4f81f3a97647ee521174 to your computer and use it in GitHub Desktop.
Code share Svein
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vvar App = require("core/App") | |
| var HasModal = require("core/HasModal") | |
| var Voice = require("alertComposer/voice/VoiceChannel") | |
| var Sms = require("alertComposer/sms/SmsChannel") | |
| var Email = require("alertComposer/email/EmailChannel") | |
| var Recipient = require("alertComposer/recipient/Recipient") | |
| var Person = require("alertComposer/recipient/Person") | |
| var Group = require("alertComposer/recipient/Group") | |
| var Area = require("alertComposer/recipient/Area") | |
| require('./EventSummary') | |
| require('./ChannelCountView') | |
| require("../send/sequentialEventAlert") | |
| require("../send/SequentialEventAlertController") | |
| require("../send/SequentialEventAlertView") | |
| require("./ValidationStatusIconView") | |
| require("alertComposer/alertTemplate/config/ValidationStatusIconViewConfig") | |
| var ViewSwitcher = require("alertComposer/send/ViewSwitcher"); | |
| var securityModeToType = { | |
| 'NoMode': 'SecurityModeDisabled', | |
| 'Basic': 'SecurityModeUsernamePassword' | |
| } | |
| module.exports = App.EventSummaryController = Ember.ObjectController.extend(HasModal,ViewSwitcher, { | |
| privateCount: 0, | |
| enterpriseCount: 0, | |
| recipientNames: [], | |
| eventController: Ember.computed.alias('target.target'), | |
| event: Ember.computed.alias('target.target.model'), | |
| alert: Ember.computed.alias('target.model'), | |
| alertController: Ember.computed.alias('target'), | |
| isValidationComplete: false, | |
| hasErrors: false, | |
| hasAuthenticationFailed: false, | |
| emptyPassword: false, | |
| isError: false, | |
| password: "", | |
| voiceChannel: null, | |
| isPrivate: false, | |
| isEnterprise: false, | |
| categoryNameArray :[], | |
| maxCategoryNameLength:60, | |
| mapCount:function(){ | |
| return this.get("privateCount")+this.get("enterpriseCount") | |
| }.property("enterpriseCount","privateCount"), | |
| personCount: function(){ | |
| var count=0; | |
| for(i=0;i<this.get('alert.recipients').length;i++){ | |
| if(this.get('alert.recipients').get(i) instanceof Person){ | |
| count++; | |
| } | |
| } | |
| return count; | |
| }.property('alert.recipients.@each'), | |
| //Temporary function just to check the count | |
| groupCount: function(){ | |
| var count=0; | |
| for(i=0;i<this.get('alert.recipients').length;i++){ | |
| if(this.get('alert.recipients').get(i) instanceof Group){ | |
| count++; | |
| } | |
| } | |
| return count; | |
| }.property('alert.recipients.@each'), | |
| grpPersonCount:function(){ | |
| return this.get("groupCount")+ this.get("personCount") | |
| }.property('alert.recipients.@each'), | |
| totalCount:function(){ | |
| return this.get("mapCount")+this.get("groupCount")+ this.get("personCount") | |
| }.property('mapCount','groupCount','personCount'), | |
| channelModel: function(){ | |
| if (this.get('alert.channels').toArray().length > 0) { | |
| return this.get('alert.channels') | |
| } else { | |
| return this.get('alert') | |
| } | |
| }.property('alert.channels.@each'), | |
| recipientsModel: function() { | |
| return this.get('alert') | |
| }.property('alert.recipients.@each'), | |
| securityMode: function () { | |
| var modes = heimdall.userContext.organization.get("modules")["securityModule"].get("resources")[ | |
| "securityMode"] | |
| if (!modes) { | |
| modes = "Basic"; | |
| } | |
| return modes; | |
| }.property(), | |
| init: function() { | |
| var self = this; | |
| }, | |
| //starts the validation only if all the models have updated data and are ready for vaildation | |
| startValidation: function() { | |
| if(this.get('target.model') != null) { | |
| var readyToValidate = this.get('target.model').isReadyForValidation() | |
| if(readyToValidate && this.get('target.currentViewName') == "EventSummary") { | |
| this.validateEventModel() | |
| } | |
| } | |
| }.observes('target.model.activeChannel.isReadyForValidation', 'target.model.activeChannel.message.isReadyForValidation'), | |
| hasNoRecipientForChannel :function(){ | |
| var hasNoRecipient = false; | |
| if (this.get('alert.channels').toArray().length > 0) { | |
| this.get('alert.channels').forEach(function(channel){ | |
| var total = 0; | |
| total = channel.mapCount+ channel.groupPersonCounts ; | |
| if(total<=0){ | |
| hasNoRecipient = true | |
| } | |
| }) | |
| } | |
| return hasNoRecipient; | |
| }.property('alert.channels.@each'), | |
| doesAtleastOneChannelHaveRecipient:function(){ | |
| var hasRecipient = false; | |
| if (this.get('alert.channels').toArray().length > 0) { | |
| this.get('alert.channels').forEach(function(channel){ | |
| var total = 0; | |
| total = channel.mapCount+ channel.groupPersonCounts ; | |
| if(total>0){ | |
| hasRecipient = true | |
| } | |
| }) | |
| } | |
| return hasRecipient; | |
| }.property('alert.channels.@each'), | |
| channelRecipientValidation:function(){ | |
| if(this.get("doesAtleastOneChannelHaveRecipient")){ | |
| if(this.get("hasNoRecipientForChannel")) { | |
| return true | |
| } | |
| else{ | |
| return false | |
| } | |
| } else{ | |
| return false | |
| } | |
| }.property("doesAtleastOneChannelHaveRecipient","hasNoRecipientForChannel"), | |
| hasNoChannelConfigurationError :function(){ | |
| var hasNoError = true; | |
| if (this.get('alert.channels').toArray().length > 0) { | |
| this.get('alert.channels').forEach(function(channel){ | |
| if(channel.validationErrors.channelConfigInvalid){ | |
| hasNoError = false | |
| } | |
| }) | |
| } | |
| return hasNoError; | |
| }.property('alert.channels.@each'), | |
| editPath: function (step) { | |
| this.set('alertController.fromEventSummary', true); | |
| if (step == 'alertTemplate') { | |
| this.set('target.currentViewName', 'AlertTemplateCategory') | |
| } else if (step == 'map') { | |
| this.set('target.currentViewName', 'MapRecipient') | |
| } else if (step == 'channel') { | |
| this.set('target.currentViewName', 'Channel') | |
| } else if (step == 'message') { | |
| this.set('target.currentViewName', 'Message') | |
| } else if (step == 'gas') { | |
| this.set('target.currentViewName', 'PersonOrGroup') | |
| } | |
| }, | |
| viewNameObserver: function() { | |
| this.set('isValidationComplete', false) | |
| }.observes('target.currentViewName'), | |
| isFromAlertTemplate:function(){ | |
| return this.get('event.isFromAlertTmplte'); | |
| }.property('target.target.model'), | |
| validateEventModel: function () { | |
| this.set('isValidationComplete', false) | |
| var self = this; | |
| this.get('event').validate(function (ok) { | |
| self.set('isValidationComplete', true); | |
| self.set('hasErrors', !ok); | |
| if(alert != null) | |
| self.get('target.model').resetValidationStatus() | |
| }); | |
| this.categoryNameTruncator(); | |
| }, | |
| areaNames: function () { | |
| var self = this; | |
| self.get("recipientNames").clear(); | |
| var recipientLength = this.get('target.model.recipients.length') | |
| if (this.get('target.model.recipients')) { | |
| this.get('target.model.recipients').forEach(function (recipient) { | |
| if(recipient instanceof Area){ | |
| if (recipientLength == 1) { | |
| self.get("recipientNames").pushObject(recipient.get('areaName')) | |
| } else { | |
| if (self.get("recipientNames.length") < recipientLength - 1) { | |
| self.get("recipientNames").pushObject(recipient.get('areaName') + ",") | |
| } else { | |
| self.get("recipientNames").pushObject(recipient.get('areaName')) | |
| } | |
| } | |
| } | |
| }) | |
| } | |
| }.observes("target.model.recipients.@each"), | |
| channels:function(){ | |
| return this.get("alert.channels"); | |
| }.property("target.model.channels.@each"), | |
| channelsToView:function(){ | |
| var previousChannelArray = []; | |
| var tempChannelArray = []; | |
| this.get("alert.channels").forEach(function (channel) { | |
| previousChannelArray.push(channel); | |
| }) | |
| for (var i = 0; i < previousChannelArray.length; i++) { | |
| if (previousChannelArray[i] instanceof Voice) { | |
| tempChannelArray[0] = previousChannelArray[i] | |
| } | |
| else if (previousChannelArray[i] instanceof Sms) { | |
| tempChannelArray[ 1] = previousChannelArray[i] | |
| } | |
| else if (previousChannelArray[i] instanceof Email) { | |
| tempChannelArray[2] = previousChannelArray[i] | |
| } | |
| } | |
| previousChannelArray.clear(); | |
| for (var i = 0; i < tempChannelArray.length; i++) { | |
| if (tempChannelArray[i]!= undefined) { | |
| previousChannelArray.pushObject(tempChannelArray[i]); | |
| } | |
| } | |
| return previousChannelArray; | |
| }.property("target.model.channels.@each"), | |
| channelCount: function () { | |
| var self = this; | |
| var privateCount = 0; | |
| var enterpriseCount =0; | |
| if (this.get("target.model.recipients.length") > 0) { | |
| if (this.get("target.model.channels.length") > 0) { | |
| this.get('target.model.channels').forEach(function(channel){ | |
| if (channel instanceof App.VoiceChannel) { | |
| privateCount = channel.get("privateMobile") + channel.get("privateFixed"); | |
| enterpriseCount = channel.get("companyMobile") + channel.get("companyFixed"); | |
| self.set("privateCount",privateCount) | |
| self.set("enterpriseCount", enterpriseCount) | |
| } | |
| }) | |
| } | |
| } | |
| }.observes("target.model.channels.@each.count", "target.model.countUpdatedForRecipient","target.model.channels.@each", 'target.model.recipients.@each'), | |
| recipientType: function() { | |
| //Resetting flags | |
| this.set("isPrivate", false) | |
| this.set("isEnterprise", false) | |
| if (this.get("target.model.channels.length") > 0) { | |
| if (this.get("target.model.recipients.length") > 0) { | |
| var privateRule = this.get("target.model.recipients").objectAt(0).get("privateFixed") | |
| var enterpriseRule = this.get("target.model.recipients").objectAt(0).get("entrprseFixed") | |
| if (privateRule == "none" && enterpriseRule == "none") { | |
| this.set("isPrivate", true) | |
| this.set("isEnterprise", true) | |
| } else if (privateRule == "Fixed Voice" && enterpriseRule == "Fixed Voice") { | |
| this.set("isPrivate", true) | |
| this.set("isEnterprise", true) | |
| } else { | |
| if (privateRule == "Fixed Voice" && enterpriseRule == "none") | |
| this.set("isPrivate", true) | |
| if (enterpriseRule == "Fixed Voice" && privateRule == "none") | |
| this.set("isEnterprise", true) | |
| } | |
| } else { | |
| this.set("isPrivate", true) | |
| this.set("isEnterprise", true) | |
| } | |
| console.log("Private",this.get("isPrivate")); | |
| console.log("Enterprise",this.get("isEnterprise")); | |
| } | |
| }.observes("target.model.recipients.@each.privateFixed", "target.model.recipients.@each.entrprseFixed", "target.model.channels.@each"), | |
| hasanyRecipients:function(){ | |
| if( this.get("target.model.recipients.length")>0){ | |
| return true ; | |
| } | |
| return false; | |
| }.property("target.model.recipients.@each"), | |
| hasRecipient:function(){ | |
| return this.get("isPrivate")||this.get("isEnterprise"); | |
| }.property("isPrivate","isEnterprise"), | |
| setVerify: function () { | |
| this.set('hasAuthenticationFailed', false); | |
| this.set('emptyPassword', false); | |
| this.set('password', ""); | |
| var securityModeSet = this.get('securityMode') | |
| if (securityModeSet == "NoMode") { | |
| this.verifyAndSend() | |
| } else { | |
| this.setModal(this.get('securityMode')) | |
| } | |
| }, | |
| verifyAndSend: function () { | |
| var self = this; | |
| var alert = self.get("alert"); | |
| if (alert.get("id") != null) { | |
| alert.set("id", null); | |
| } | |
| $.ajax({ | |
| url: App.get("baseUrl") + "/alert/ajax/security", | |
| type: "POST", | |
| data: { | |
| "securityMode": securityModeToType[this.get('securityMode')], | |
| "username": heimdall.userContext.user.get("username"), | |
| "password": this.get("password") | |
| }, | |
| success: function (data, textStatus, jqXHR) { | |
| self.setModal('none'); | |
| //this will add preloader after authentication, till the time alert is sent | |
| //on success of alert the page will be redirected and automatically remove the preloader | |
| //on failure the removeLoader is called in sendAlert() failure part | |
| addPreloader() | |
| self.sendAlert(data); | |
| } | |
| }).fail(function (data) { | |
| if (self.get("password") == "") { | |
| self.set("emptyPassword", true); | |
| } else { | |
| self.set("emptyPassword", false); | |
| } | |
| self.set("password",""); | |
| self.set('hasAuthenticationFailed', true); | |
| $("#passwordId").focus(); | |
| }); | |
| }, | |
| sendAlert: function (token) { | |
| console.log("EventController.js model::" + JSON.stringify(this.get("event").serialize())); | |
| $.ajax({ | |
| url: App.get("baseUrl") + "/sendDirect/ajax", | |
| type: "POST", | |
| data: JSON.stringify({ | |
| event: this.get("event").serialize(), | |
| token: token | |
| }), | |
| contentType: "application/json; charset=utf-8", | |
| dataType: "json", | |
| success: function (data, textStatus, jqXHR) { | |
| window.location = App.get("baseUrl") + "/monitor/status/event/" + data | |
| } | |
| }).fail(function (data) { | |
| //preloader will be removed on failure of alert send | |
| removePreloader() | |
| }); | |
| }, | |
| categoryNameTruncator:function() { | |
| var maxLength = this.get('maxEventNameLength') | |
| var self = this; | |
| self.set("categoryNameArray",[]); | |
| if(this.get("event.categoryPath.length") > maxLength) { | |
| var nameArray = this.get("event.categoryPath").split(" "); | |
| self.get("categoryNameArray").pushObjects(self.stringMaker(nameArray)); | |
| } else { | |
| self.get("categoryNameArray").push(this.get("event.categoryPath")); | |
| } | |
| }, | |
| stringMaker:function(nameArray) { | |
| var maxLength = this.get('maxCategoryNameLength'); | |
| var finalEventArray = []; | |
| finalEventArray.push(""); | |
| var self = this; | |
| nameArray.forEach(function(name){ | |
| var stringToPush = finalEventArray[finalEventArray.length-1] + " " + name+"/"; | |
| // check if new element combined with last | |
| // element of finalEventArray is greater than | |
| // max length. | |
| if(stringToPush.length > maxLength) { | |
| //check for new element's length | |
| if(name.length > maxLength){ | |
| var trimmedStringArray = self.stringTrimmer(name); | |
| trimmedStringArray.forEach(function(trimmedString){ | |
| finalEventArray.push(trimmedString); | |
| }) | |
| } else { | |
| finalEventArray.push(name); | |
| } | |
| } else { | |
| finalEventArray[finalEventArray.length-1] = stringToPush; | |
| } | |
| }) | |
| return finalEventArray.filter(function(element){return element !== ''});; | |
| }, | |
| stringTrimmer:function(string) { | |
| var maxLength = this.get('maxCategoryNameLength') | |
| var stringArray = []; | |
| if(string.length > maxLength) { | |
| var fullName = string; | |
| stringArray.push(fullName.substring(0,maxLength)); | |
| stringArray.push(fullName.substring(maxLength,fullName.length)); | |
| } else { | |
| stringArray.push(string) | |
| } | |
| return stringArray; | |
| }, | |
| getSequence: function(viewName){ | |
| return this.getCurrentSequence(viewName)+1; | |
| } , | |
| checkNoErrorsInNameOrMsg:function(){ | |
| if(!this.get("event.validationErrors.hasErrors")){ | |
| this.get("alert.channels").forEach(function(channel){ | |
| if(channel.validationError.message || channel.validationError.channelConfig){ | |
| return false | |
| } | |
| }) | |
| return true | |
| } | |
| return false | |
| }.property("target.model","target.target.model") , | |
| disableSend:function(){ | |
| if(this.get("hasErrors")) { | |
| if(this.get("checkNoErrorsInNameOrMsg")) { | |
| if(this.get("doesAtleastOneChannelHaveRecipient")) { | |
| return false | |
| } | |
| } | |
| return true; | |
| } else | |
| { | |
| return false; | |
| } | |
| }.property("hasErrors","doesAtleastOneChannelHaveRecipient","hasNoChannelConfigurationError") | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment