From b21c9cdc30d42c749910393a9b68e32b33e1d604 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 17 Feb 2015 15:51:53 -0800 Subject: [PATCH] CRM-15970 - crmUiOrder - Add directives for managing sort order. --- js/angular-crm-ui.js | 104 +++++++++++++++++++++++++++++ tests/karma/unit/crmUiOrderSpec.js | 87 ++++++++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 tests/karma/unit/crmUiOrderSpec.js diff --git a/js/angular-crm-ui.js b/js/angular-crm-ui.js index 28ccdc93e2..731941bfe1 100644 --- a/js/angular-crm-ui.js +++ b/js/angular-crm-ui.js @@ -454,6 +454,110 @@ }; }) + // CrmUiOrderCtrl is a controller class which manages sort orderings. + // Ex: + // JS: $scope.myOrder = new CrmUiOrderCtrl(['+field1', '-field2]); + // $scope.myOrder.toggle('field1'); + // $scope.myOrder.setDir('field2', ''); + // HTML: ... + .service('CrmUiOrderCtrl', function(){ + // + function CrmUiOrderCtrl(defaults){ + this.values = defaults; + } + angular.extend(CrmUiOrderCtrl.prototype, { + get: function get() { + return this.values; + }, + getDir: function getDir(name) { + if (this.values.indexOf(name) >= 0 || this.values.indexOf('+' + name) >= 0) { + return '+'; + } + if (this.values.indexOf('-' + name) >= 0) { + return '-'; + } + return ''; + }, + // @return bool TRUE if something is removed + remove: function remove(name) { + var idx = this.values.indexOf(name); + if (idx >= 0) { + this.values.splice(idx, 1); + return true; + } + else { + return false; + } + }, + setDir: function setDir(name, dir) { + return this.toggle(name, dir); + }, + // Toggle sort order on a field. + // To set a specific order, pass optional parameter 'next' ('+', '-', or ''). + toggle: function toggle(name, next) { + if (!next && next !== '') { + next = '+'; + if (this.remove(name) || this.remove('+' + name)) { + next = '-'; + } + if (this.remove('-' + name)) { + next = ''; + } + } + + if (next == '+') { + this.values.unshift('+' + name); + } + else if (next == '-') { + this.values.unshift('-' + name); + } + } + }); + return CrmUiOrderCtrl; + }) + + // Define a controller which manages sort order. You may interact with the controller + // directly ("myOrder.toggle('fieldname')") order using the helper, crm-ui-order-by. + // example: + // + // My Field + // ... + //