Skip to content

Instantly share code, notes, and snippets.

@Samstiles
Forked from jme900/rule.js
Last active August 29, 2015 14:26
Show Gist options
  • Save Samstiles/a04dd476f14a7bbc287b to your computer and use it in GitHub Desktop.
Save Samstiles/a04dd476f14a7bbc287b to your computer and use it in GitHub Desktop.
(function () {
'use strict';
angular.module('eyesover')
.directive('rule', function () {
return {
scope: {
rule: '=ruleset'
},
replace: true,
restrict: 'E',
templateUrl: 'components/rule/rule.html',
link: function () {},
controller: function ($scope, $timeout, toastr, $http, $state) {
$scope.originalRule = _.clone($scope.rule);
$scope.originalKeywords = _.clone($scope.rule.keywords);
$scope.newKeyword = '';
console.log($scope.$parent.$parent.rules);
$scope.addKeyword = function() {
$scope.rule.keywords.push($scope.newKeyword);
$scope.newKeyword = '';
};
$scope.toggleListener = function(l) {
$timeout(function() {
$scope.rule[l] = !$scope.rule[l];
console.log($scope.rule);
});
};
$scope.useThisRule = function() {
$timeout(function() {
$scope.$parent.$parent.selectedMessage = $scope.rule.message;
}, 5);
};
$scope.saveRule = function() {
$http.get('https://THAT URL YOU JUST SENT ME').success(function(whateverYouCallThisParameterIsTheDataFromThatEndpointYouJustSentMe) {
$http.put('https://localhost/rule/' + $scope.rule.id, $scope.rule)
.success(function () {
console.log('Successfully saved rule!');
toastr.success('Rule saved.');
})
.error(function () {
console.error('Error saving rule!');
toastr.error('Failed to save rule.');
});
}).error(function(data) {
console.error('Error saving rule!');
toastr.error('Failed to save rule.');
});
};
$scope.deleteRule = function() {
$http.get('https://localhost/rule/postRules?action=delete&name=' + $scope.rule.name)
.success(function () {
console.log('Successfully deleted rule!');
toastr.success('Rule deleted.');
$state.go($state.current, {}, {reload: true});
})
.error(function () {
console.error('Error deleting rule!');
toastr.error('Failed to delete rule.');
});
};
$scope.cancel = function() {
console.log('RULE BEFORE CANCEL:', $scope.rule);
$scope.rule = _.clone($scope.originalRule);
$scope.rule.keywords = _.clone($scope.originalKeywords);
console.log('RULE AFTER CANCEL:', $scope.rule);
};
$scope.removeKeyword = function(keyword) {
$scope.rule.keywords.splice($scope.rule.keywords.indexOf(keyword), 1);
};
}
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment