aboutsummaryrefslogtreecommitdiffstats
path: root/src/directives.js
blob: 5cee097891888d393abc826b273dc927dced3e28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
angularDirective("ng-init", function(expression){
  return function(element){
    this.$tryEval(expression, element);
  };
});

angularDirective("ng-controller", function(expression){
  return function(element){
    var controller = getter(window, expression, true) || getter(this, expression, true);
    if (!controller)
      throw "Can not find '"+expression+"' controller.";
    if (!isFunction(controller))
      throw "Reference '"+expression+"' is not a class.";
    this.$become(controller);
    (this.init || noop)();
  };
});

angularDirective("ng-eval", function(expression){
  return function(element){
    this.$onEval(expression, element);
  };
});

angularDirective("ng-bind", function(expression){
  var templateFn = compileBindTemplate("{{" + expression + "}}");
  return function(element) {
    var lastValue;
    this.$onEval(function() {
      var value = templateFn.call(this, element);
      if (value != lastValue) {
        element.text(value);
        lastValue = value;
      }
    }, element);
  };
});

var bindTemplateCache = {};
function compileBindTemplate(template){
  var fn = bindTemplateCache[template];
  if (!fn) {
    var bindings = [];
    foreach(parseBindings(template), function(text){
      var exp = binding(text);
      bindings.push(exp ? function(element){
        var error, value = this.$tryEval(exp, function(e){
          error = toJson(e);
        });
        elementError(element, NG_EXCEPTION, error);
        return error ? error : value;
      } : function() {
        return text;
      });
    });
    bindTemplateCache[template] = fn = function(element){
      var parts = [], self = this;
      foreach(bindings, function(fn){
        var value = fn.call(self, element);
        if (isObject(value)) value = toJson(value, true);
        parts.push(value);
      });
      return parts.join('');
    };
  }
  return fn;
}

angularDirective("ng-bind-template", function(expression){
  var templateFn = compileBindTemplate(expression);
  return function(element) {
    var lastValue;
    this.$onEval(function() {
      var value = templateFn.call(this, element);
      if (value != lastValue) {
        element.text(value);
        lastValue = value;
      }
    }, element);
  };
});

angularDirective("ng-bind-attr", function(expression){
  return function(element){
    this.$onEval(function(){
      foreach(this.$eval(expression), function(bindExp, key) {
        var value = compileBindTemplate(bindExp).call(this, element);
        if (key == 'disabled' && !toBoolean(value)) {
          element.removeAttr('disabled');
        } else {
          element.attr(key, value);
        }
      }, this);
    }, element);
  };
});

angularWidget("@ng-non-bindable", noop);

angularWidget("@ng-repeat", function(expression, element){
  element.removeAttr('ng-repeat');
  element.replaceWith(this.comment("ng-repeat: " + expression));
  var template = this.compile(element);
  return function(reference){
    var match = expression.match(/^\s*(.+)\s+in\s+(.*)\s*$/),
        lhs, rhs, valueIdent, keyIdent;
    if (! match) {
      throw "Expected ng-repeat in form of 'item in collection' but got '" +
      expression + "'.";
    }
    lhs = match[1];
    rhs = match[2];
    match = lhs.match(/^([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\)$/);
    if (!match) {
      throw "'item' in 'item in collection' should be identifier or (key, value) but got '" +
      keyValue + "'.";
    }
    valueIdent = match[3] || match[1];
    keyIdent = match[2];

    if (isUndefined(this.$eval(rhs))) this.$set(rhs, []);

    var children = [], currentScope = this;
    this.$onEval(function(){
      var index = 0, childCount = children.length, childScope, lastElement = reference;
      foreach(this.$tryEval(rhs, reference), function(value, key){
        function assign(scope) {
          scope[valueIdent] = value;
          if (keyIdent) scope[keyIdent] = key;
        }
        if (index < childCount) {
          // reuse existing child
          assign(childScope = children[index]);
        } else {
          // grow children
          assign(childScope = template(element.clone(), createScope(currentScope)));
          lastElement.after(childScope.$element);
          childScope.$index = index;
          childScope.$element.attr('ng-repeat-index', index);
          childScope.$init();
          children.push(childScope);
        }
        childScope.$eval();
        lastElement = childScope.$element;
        index ++;
      });
      // shrink children
      while(children.length > index) {
        children.pop().$element.remove();
      }
    }, reference);
  };
});

angularDirective("ng-click", function(expression, element){
  return function(element){
    var self = this;
    element.click(function(){
      self.$tryEval(expression, element);
      self.$root.$eval();
      return false;
    });
  };
});

angularDirective("ng-watch", function(expression, element){
  return function(element){
    var self = this;
    new Parser(expression).watch()({
      scope:{get: self.$get, set: self.$set},
      addListener:function(watch, exp){
        self.$watch(watch, function(){
          return exp({scope:{get: self.$get, set: self.$set}, state:self});
        }, element);
      }
    });
  };
});

function ngClass(selector) {
  return function(expression, element){
    var existing = element[0].className + ' ';
    return function(element){
      this.$onEval(function(){
        var value = this.$eval(expression);
        if (selector(this.$index)) {
          if (isArray(value)) value = value.join(' ');
          element[0].className = trim(existing + value);
        }
      }, element);
    };
  };
}

angularDirective("ng-class", ngClass(function(){return true;}));
angularDirective("ng-class-odd", ngClass(function(i){return i % 2 === 0;}));
angularDirective("ng-class-even", ngClass(function(i){return i % 2 === 1;}));

angularDirective("ng-show", function(expression, element){
  return function(element){
    this.$onEval(function(){
      element.css('display', toBoolean(this.$eval(expression)) ? '' : 'none');
    }, element);
  };
});

angularDirective("ng-hide", function(expression, element){
  return function(element){
    this.$onEval(function(){
      element.css('display', toBoolean(this.$eval(expression)) ? 'none' : '');
    }, element);
  };
});

angularDirective("ng-style", function(expression, element){
  return function(element){
    this.$onEval(function(){
      element.css(this.$eval(expression));
    }, element);
  };
});