Project

General

Profile

1
// Generated by CoffeeScript 1.8.0
2
(function() {
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

    
5
  angular.module('localytics.directives', []);
6

    
7
  angular.module('localytics.directives').directive('chosen', [
8
    '$timeout', function($timeout) {
9
      var CHOSEN_OPTION_WHITELIST, NG_OPTIONS_REGEXP, isEmpty, snakeCase;
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+(.*?))?$/;
11
      CHOSEN_OPTION_WHITELIST = ['noResultsText', 'allowSingleDeselect', 'disableSearchThreshold', 'disableSearch', 'enableSplitWordSearch', 'inheritSelectClasses', 'maxSelectedOptions', 'placeholderTextMultiple', 'placeholderTextSingle', 'searchContains', 'singleBackstrokeDelete', 'displayDisabledOptions', 'displaySelectedOptions', 'width'];
12
      snakeCase = function(input) {
13
        return input.replace(/[A-Z]/g, function($1) {
14
          return "_" + ($1.toLowerCase());
15
        });
16
      };
17
      isEmpty = function(value) {
18
        var key;
19
        if (angular.isArray(value)) {
20
          return value.length === 0;
21
        } else if (angular.isObject(value)) {
22
          for (key in value) {
23
            if (value.hasOwnProperty(key)) {
24
              return false;
25
            }
26
          }
27
        }
28
        return true;
29
      };
30
      return {
31
        restrict: 'A',
32
        require: '?ngModel',
33
        terminal: true,
34
        link: function(scope, element, attr, ngModel) {
35
          var chosen, defaultText, disableWithMessage, empty, initOrUpdate, match, options, origRender, removeEmptyMessage, startLoading, stopLoading, valuesExpr, viewWatch;
36
          element.addClass('localytics-chosen');
37
          options = scope.$eval(attr.chosen) || {};
38
          angular.forEach(attr, function(value, key) {
39
            if (__indexOf.call(CHOSEN_OPTION_WHITELIST, key) >= 0) {
40
              return options[snakeCase(key)] = scope.$eval(value);
41
            }
42
          });
43
          startLoading = function() {
44
            return element.addClass('loading').attr('disabled', true).trigger('chosen:updated');
45
          };
46
          stopLoading = function() {
47
            return element.removeClass('loading').attr('disabled', false).trigger('chosen:updated');
48
          };
49
          chosen = null;
50
          defaultText = null;
51
          empty = false;
52
          initOrUpdate = function() {
53
            if (chosen) {
54
              return element.trigger('chosen:updated');
55
            } else {
56
              chosen = element.chosen(options).data('chosen');
57
              return defaultText = chosen.default_text;
58
            }
59
          };
60
          removeEmptyMessage = function() {
61
            empty = false;
62
            return element.attr('data-placeholder', defaultText);
63
          };
64
          disableWithMessage = function() {
65
            empty = true;
66
            return element.attr('data-placeholder', chosen.results_none_found).attr('disabled', true).trigger('chosen:updated');
67
          };
68
          if (ngModel) {
69
            origRender = ngModel.$render;
70
            ngModel.$render = function() {
71
              origRender();
72
              return initOrUpdate();
73
            };
74
            if (attr.multiple) {
75
              viewWatch = function() {
76
                return ngModel.$viewValue;
77
              };
78
              scope.$watch(viewWatch, ngModel.$render, true);
79
            }
80
          } else {
81
            initOrUpdate();
82
          }
83
          attr.$observe('disabled', function() {
84
            return element.trigger('chosen:updated');
85
          });
86
          if (attr.ngOptions && ngModel) {
87
            match = attr.ngOptions.match(NG_OPTIONS_REGEXP);
88
            valuesExpr = match[7];
89
            scope.$watchCollection(valuesExpr, function(newVal, oldVal) {
90
              var timer;
91
              return timer = $timeout(function() {
92
                if (angular.isUndefined(newVal)) {
93
                  return startLoading();
94
                } else {
95
                  if (empty) {
96
                    removeEmptyMessage();
97
                  }
98
                  stopLoading();
99
                  if (isEmpty(newVal)) {
100
                    return disableWithMessage();
101
                  }
102
                }
103
              });
104
            });
105
            return scope.$on('$destroy', function(event) {
106
              if (typeof timer !== "undefined" && timer !== null) {
107
                return $timeout.cancel(timer);
108
              }
109
            });
110
          }
111
        }
112
      };
113
    }
114
  ]);
115

    
116
}).call(this);
(1-1/2)