mapping
7 years ago
angular-chosen.directive.js
7 years ago
attributes.service.js
7 years ago
autodetect.directive.js
7 years ago
cascade.directive.js
7 years ago
contenteditable.directive.js
7 years ago
currency.service.js
7 years ago
droppable.directive.js
7 years ago
export.service.js
7 years ago
focusMeWhenEnabled.directive.js
7 years ago
googleCategoriesService.js
7 years ago
main.controller.js
7 years ago
styledInput.directive.js
7 years ago
template.service.js
7 years ago
tipsy.directive.js
7 years ago
wpHttp.service.js
7 years ago
angular-chosen.directive.js
110 lines
| 1 | function chosen($timeout) { |
| 2 | var EVENTS, scope, linker, watchCollection; |
| 3 | |
| 4 | /* |
| 5 | * List of events and the alias used for binding with angularJS |
| 6 | */ |
| 7 | EVENTS = [{ |
| 8 | onChange: 'change' |
| 9 | }, { |
| 10 | onReady: 'chosen:ready' |
| 11 | }, { |
| 12 | onMaxSelected: 'chosen:maxselected' |
| 13 | }, { |
| 14 | onShowDropdown: 'chosen:showing_dropdown' |
| 15 | }, { |
| 16 | onHideDropdown: 'chosen:hiding_dropdown' |
| 17 | }, { |
| 18 | onNoResult: 'chosen:no_results' |
| 19 | }]; |
| 20 | |
| 21 | /* |
| 22 | * Items to be added in the scope of the directive |
| 23 | */ |
| 24 | scope = { |
| 25 | options: '=', // the options array |
| 26 | ngModel: '=', // the model to bind to,, |
| 27 | ngDisabled: '=' |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * initialize the list of items |
| 32 | * to watch to trigger the chosen:updated event |
| 33 | */ |
| 34 | watchCollection = []; |
| 35 | Object.keys(scope).forEach(function (scopeName) { |
| 36 | watchCollection.push(scopeName); |
| 37 | }); |
| 38 | |
| 39 | /* |
| 40 | * Add the list of event handler of the chosen |
| 41 | * in the scope. |
| 42 | */ |
| 43 | EVENTS.forEach(function (event) { |
| 44 | var eventNameAlias = Object.keys(event)[0]; |
| 45 | scope[eventNameAlias] = '='; |
| 46 | }); |
| 47 | |
| 48 | /* Linker for the directive */ |
| 49 | linker = function ($scope, iElm, iAttr) { |
| 50 | var maxSelection = parseInt(iAttr.maxSelection, 10), |
| 51 | searchThreshold = parseInt(iAttr.searchThreshold, 10); |
| 52 | |
| 53 | if (isNaN(maxSelection) || maxSelection === Infinity) { |
| 54 | maxSelection = undefined; |
| 55 | } |
| 56 | |
| 57 | if (isNaN(searchThreshold) || searchThreshold === Infinity) { |
| 58 | searchThreshold = undefined; |
| 59 | } |
| 60 | |
| 61 | var allowSingleDeselect = iElm.attr('allow-single-deselect') !== undefined ? true : false; |
| 62 | var noResultsText = iElm.attr('no-results-text') !== undefined ? iAttr.noResultsText : "No results found."; |
| 63 | |
| 64 | iElm.chosen({ |
| 65 | width: '100%', |
| 66 | max_selected_options: maxSelection, |
| 67 | disable_search_threshold: searchThreshold, |
| 68 | search_contains: true, |
| 69 | allow_single_deselect: allowSingleDeselect, |
| 70 | no_results_text: noResultsText |
| 71 | }); |
| 72 | |
| 73 | iElm.on('change', function () { |
| 74 | iElm.trigger('chosen:updated'); |
| 75 | }); |
| 76 | |
| 77 | $scope.$watchGroup(watchCollection, function () { |
| 78 | $timeout(function () { |
| 79 | iElm.trigger('chosen:updated'); |
| 80 | }, 100); |
| 81 | }); |
| 82 | |
| 83 | $scope.$on('chosen:updated', function() { |
| 84 | iElm.trigger('chosen:updated'); |
| 85 | }); |
| 86 | |
| 87 | // assign event handlers |
| 88 | EVENTS.forEach(function (event) { |
| 89 | var eventNameAlias = Object.keys(event)[0]; |
| 90 | |
| 91 | if (typeof $scope[eventNameAlias] === 'function') { // check if the handler is a function |
| 92 | iElm.on(event[eventNameAlias], function (event) { |
| 93 | $scope.$apply(function () { |
| 94 | $scope[eventNameAlias](event); |
| 95 | }); |
| 96 | }); // listen to the event triggered by chosen |
| 97 | } |
| 98 | }); |
| 99 | }; |
| 100 | |
| 101 | // return the directive |
| 102 | return { |
| 103 | name: 'chosen', |
| 104 | scope: scope, |
| 105 | restrict: 'A', |
| 106 | link: linker |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | GoogleMerchants.directive('chosen', ['$timeout', chosen]); |