Last active
August 29, 2015 14:26
-
-
Save jme900/6afaa5097180c9f726f2 to your computer and use it in GitHub Desktop.
This file contains 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
(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://localhost/rule/config').success(function(config) { | |
var sentiment = { | |
min:0, | |
max:0 | |
} | |
if($scope.rule.supporters){ | |
sentiment.max=config.supporters.max; | |
sentiment.min=config.supporters.min; | |
if($scope.rule.undecideds){ | |
sentiment.min=config.undecided.min; | |
if($scope.rule.oppositions){ | |
sentiment.min=config.opposition.min; | |
} | |
} | |
} | |
else if($scope.rule.undecideds){ | |
sentiment.max=config.undecided.max; | |
sentiment.min=config.undecided.min; | |
if($scope.rule.oppositions){ | |
sentiment.min=config.opposition.min; | |
} | |
} | |
else if($scope.rule.oppositions){ | |
sentiment.max=config.opposition.max; | |
sentiment.min=config.opposition.min; | |
} | |
$scope.rule.sentiment = sentiment; | |
$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 getting rule settings/configs prior to 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