From 63b0fc6269b38edf7234b9f151b80d81f614c0a3 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 4 Aug 2015 10:11:24 +0300 Subject: Initial commit First public commit --- static/js/rules.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 static/js/rules.js (limited to 'static/js/rules.js') diff --git a/static/js/rules.js b/static/js/rules.js new file mode 100644 index 0000000..909743d --- /dev/null +++ b/static/js/rules.js @@ -0,0 +1,118 @@ +/** + * rules.js + */ + +// start data model +// a condition consist of a key, an operator (=) and a value +var Condition = function(init){ + var self = this; + + self.init = init; + + self.id = null; + self.key = ko.observable('QUEUE'); + self.value = ko.observable(''); + self.operator = ko.observable('='); + + self.keyChoices = [ + {key: 'QUEUE', title: 'Queue'}, + {key: 'STATUS', title: 'Status'}, + {key: 'CUSTOMER', title: 'Customer'}, + {key: 'DEVICE', title: 'Device'}, + ] + + self.valueChoices = ko.computed(function(){ + return self.init[self.key()]; + }); + + self.selectable = new Array('QUEUE', 'STATUS'); + + self.canSelect = function(){ + return self.selectable.indexOf(self.key()) != -1; + } + +} + +var Action = function(init){ + var self = this; + + self.init = init; + + self.id = null; + self.title = ko.observable('Send Email'); + self.key = ko.observable('SEND_EMAIL'); + self.value = ko.observable(''); + + self.selectable = new Array( + 'SET_QUEUE', 'SET_STATUS', 'SET_USER', 'ADD_TAG' + ); + + self.keyChoices = [ + {title: 'Send Email', key: 'SEND_EMAIL'}, + {title: 'Send SMS', key: 'SEND_SMS'}, + {title: 'Add Tag', key: 'ADD_TAG'}, + {title: 'Set Queue', key: 'SET_QUEUE'}, + {title: 'Assign to', key: 'SET_USER'} + ]; + + self.canSelect = function() { + return self.selectable.indexOf(self.key()) != -1; + } + + self.valueChoices = ko.computed(function(){ + return self.init[self.key()]; + }); +} + +var Rule = function(condInit, actionInit){ + var self = this; + + self.id = null; + self.match = ko.observable('ANY'); + self.description = ko.observable('New Rule'); + + self.matchChoices = [ + {title: 'Any', value: 'ANY'}, + {title: 'All', value: 'ALL'} + ]; + + // rules consist of conditions and actions + self.conditions = ko.observableArray([new Condition(condInit)]); + self.actions = ko.observableArray([new Action(actionInit)]); + +} +// end data model + +var ViewModel = function(cData, aData){ + var self = this; + self.rule = new Rule(cData, aData); + + self.addCondition = function(){ + self.rule.conditions.push(new Condition(cData)); + }; + + self.removeCondition = function(){ + if (self.rule.conditions().length < 2) return; + self.rule.conditions.remove(this); + }; + + self.addAction = function(){ + self.rule.actions.push(new Action(aData)); + }; + + self.removeAction = function(){ + if (self.rule.actions().length < 2) return; + self.rule.actions.remove(this); + }; + + self.validateAndSave = function(form){ + $.post($(form).attr('action'), $(form).serialize(), function(r){ + console.log(r); + }); + }; + + self.init = function(data){ + console.log(data); + self.rule = new Rule(cData, aData); + } +} -- cgit v1.2.3