| .. | .. |
|---|
| 1 | | -// Generated by CoffeeScript 1.6.3 |
|---|
| 1 | +// Generated by CoffeeScript 1.6.2 |
|---|
| 2 | 2 | (function() { |
|---|
| 3 | 3 | var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; |
|---|
| 4 | 4 | |
|---|
| 5 | 5 | angular.module('localytics.directives', []); |
|---|
| 6 | 6 | |
|---|
| 7 | 7 | angular.module('localytics.directives').directive('chosen', function() { |
|---|
| 8 | | - var CHOSEN_OPTION_WHITELIST, NG_OPTIONS_REGEXP, chosen, isEmpty, snakeCase; |
|---|
| 8 | + var CHOSEN_OPTION_WHITELIST, NG_OPTIONS_REGEXP, isEmpty, snakeCase; |
|---|
| 9 | + |
|---|
| 9 | 10 | NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+(.*?)(?:\s+track\s+by\s+(.*?))?$/; |
|---|
| 10 | 11 | CHOSEN_OPTION_WHITELIST = ['noResultsText', 'allowSingleDeselect', 'disableSearchThreshold', 'disableSearch', 'enableSplitWordSearch', 'inheritSelectClasses', 'maxSelectedOptions', 'placeholderTextMultiple', 'placeholderTextSingle', 'searchContains', 'singleBackstrokeDelete', 'displayDisabledOptions', 'displaySelectedOptions', 'width']; |
|---|
| 11 | 12 | snakeCase = function(input) { |
|---|
| .. | .. |
|---|
| 15 | 16 | }; |
|---|
| 16 | 17 | isEmpty = function(value) { |
|---|
| 17 | 18 | var key; |
|---|
| 19 | + |
|---|
| 18 | 20 | if (angular.isArray(value)) { |
|---|
| 19 | 21 | return value.length === 0; |
|---|
| 20 | 22 | } else if (angular.isObject(value)) { |
|---|
| .. | .. |
|---|
| 26 | 28 | } |
|---|
| 27 | 29 | return true; |
|---|
| 28 | 30 | }; |
|---|
| 29 | | - return chosen = { |
|---|
| 31 | + return { |
|---|
| 30 | 32 | restrict: 'A', |
|---|
| 31 | 33 | require: '?ngModel', |
|---|
| 32 | 34 | terminal: true, |
|---|
| 33 | | - link: function(scope, element, attr, ctrl) { |
|---|
| 34 | | - var disableWithMessage, empty, initOrUpdate, initialized, match, options, origRender, removeEmptyMessage, startLoading, stopLoading, valuesExpr, viewWatch; |
|---|
| 35 | + link: function(scope, element, attr, ngModel) { |
|---|
| 36 | + var chosen, defaultText, disableWithMessage, empty, initOrUpdate, match, options, origRender, removeEmptyMessage, startLoading, stopLoading, valuesExpr, viewWatch; |
|---|
| 37 | + |
|---|
| 35 | 38 | element.addClass('localytics-chosen'); |
|---|
| 36 | 39 | options = scope.$eval(attr.chosen) || {}; |
|---|
| 37 | 40 | angular.forEach(attr, function(value, key) { |
|---|
| .. | .. |
|---|
| 45 | 48 | stopLoading = function() { |
|---|
| 46 | 49 | return element.removeClass('loading').attr('disabled', false).trigger('chosen:updated'); |
|---|
| 47 | 50 | }; |
|---|
| 48 | | - initialized = false; |
|---|
| 51 | + chosen = null; |
|---|
| 52 | + defaultText = null; |
|---|
| 49 | 53 | empty = false; |
|---|
| 50 | 54 | initOrUpdate = function() { |
|---|
| 51 | | - if (initialized) { |
|---|
| 55 | + if (chosen) { |
|---|
| 52 | 56 | return element.trigger('chosen:updated'); |
|---|
| 53 | 57 | } else { |
|---|
| 54 | | - element.chosen(options); |
|---|
| 55 | | - return initialized = true; |
|---|
| 58 | + chosen = element.chosen(options).data('chosen'); |
|---|
| 59 | + return defaultText = chosen.default_text; |
|---|
| 56 | 60 | } |
|---|
| 57 | 61 | }; |
|---|
| 58 | 62 | removeEmptyMessage = function() { |
|---|
| 59 | 63 | empty = false; |
|---|
| 60 | | - return element.find('option.empty').remove(); |
|---|
| 64 | + return element.attr('data-placeholder', defaultText); |
|---|
| 61 | 65 | }; |
|---|
| 62 | | - disableWithMessage = function(message) { |
|---|
| 66 | + disableWithMessage = function() { |
|---|
| 63 | 67 | empty = true; |
|---|
| 64 | | - return element.empty().append("<option selected class=\"empty\">" + message + "</option>").attr('disabled', true).trigger('chosen:updated'); |
|---|
| 68 | + return element.attr('data-placeholder', chosen.results_none_found).attr('disabled', true).trigger('chosen:updated'); |
|---|
| 65 | 69 | }; |
|---|
| 66 | | - if (ctrl) { |
|---|
| 67 | | - origRender = ctrl.$render; |
|---|
| 68 | | - ctrl.$render = function() { |
|---|
| 70 | + if (ngModel) { |
|---|
| 71 | + origRender = ngModel.$render; |
|---|
| 72 | + ngModel.$render = function() { |
|---|
| 69 | 73 | origRender(); |
|---|
| 70 | 74 | return initOrUpdate(); |
|---|
| 71 | 75 | }; |
|---|
| 72 | 76 | if (attr.multiple) { |
|---|
| 73 | 77 | viewWatch = function() { |
|---|
| 74 | | - return ctrl.$viewValue; |
|---|
| 78 | + return ngModel.$viewValue; |
|---|
| 75 | 79 | }; |
|---|
| 76 | | - scope.$watch(viewWatch, ctrl.$render, true); |
|---|
| 80 | + scope.$watch(viewWatch, ngModel.$render, true); |
|---|
| 77 | 81 | } |
|---|
| 78 | 82 | } else { |
|---|
| 79 | 83 | initOrUpdate(); |
|---|
| 80 | 84 | } |
|---|
| 81 | | - attr.$observe('disabled', function(value) { |
|---|
| 85 | + attr.$observe('disabled', function() { |
|---|
| 82 | 86 | return element.trigger('chosen:updated'); |
|---|
| 83 | 87 | }); |
|---|
| 84 | | - if (attr.ngOptions) { |
|---|
| 88 | + if (attr.ngOptions && ngModel) { |
|---|
| 85 | 89 | match = attr.ngOptions.match(NG_OPTIONS_REGEXP); |
|---|
| 86 | 90 | valuesExpr = match[7]; |
|---|
| 87 | | - if (angular.isUndefined(scope.$eval(valuesExpr))) { |
|---|
| 88 | | - startLoading(); |
|---|
| 89 | | - } |
|---|
| 90 | 91 | return scope.$watchCollection(valuesExpr, function(newVal, oldVal) { |
|---|
| 91 | | - if (newVal !== oldVal) { |
|---|
| 92 | + if (angular.isUndefined(newVal)) { |
|---|
| 93 | + return startLoading(); |
|---|
| 94 | + } else { |
|---|
| 92 | 95 | if (empty) { |
|---|
| 93 | 96 | removeEmptyMessage(); |
|---|
| 94 | 97 | } |
|---|
| 95 | 98 | stopLoading(); |
|---|
| 96 | 99 | if (isEmpty(newVal)) { |
|---|
| 97 | | - return disableWithMessage(options.no_results_text || 'No values available'); |
|---|
| 100 | + return disableWithMessage(); |
|---|
| 98 | 101 | } |
|---|
| 99 | 102 | } |
|---|
| 100 | 103 | }); |
|---|
| .. | .. |
|---|
| 103 | 106 | }; |
|---|
| 104 | 107 | }); |
|---|
| 105 | 108 | |
|---|
| 106 | | -}).call(this); |
|---|
| 109 | +}).call(this); |
|---|