aboutsummaryrefslogtreecommitdiffstats
path: root/src/directives.js
blob: 9f98badbbdf83eed3fe1f173ffdeda32af0ed7cd (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
angularDirective("ng-init", function(expression){
  return function(){
    this.$eval(expression);
  };
});

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

angularDirective("ng-bind", function(expression){
  return function(element) {
    this.$watch(expression, function(value){
      element.text(value);
    });
  };
});

angularDirective("ng-bind-attr", function(expression){
  return function(element){
    this.$watch(expression, bind(element, element.attr));
  };
});

angularDirective("ng-non-bindable", function(){
  this.descend(false);
});

angularDirective("ng-repeat", function(expression, element){
  var reference = this.reference("ng-repeat: " + expression),
      r = element.removeAttr('ng-repeat'),
      template = this.compile(element),
      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];

  var parent = element.parent();
  element.replaceWith(reference);
  return function(){
    var children = [],
        currentScope = this;
    this.$addEval(rhs, function(items){
      var index = 0, childCount = children.length, childScope, lastElement = reference;
      foreach(items || [], function(value, key){
        if (index < childCount) {
          // reuse existing child
          childScope = children[index];
        } else {
          // grow children
          childScope = template(element.clone(), currentScope);
          childScope.init();
          childScope.scope.set('$index', index);
          childScope.element.attr('ng-index', index);
          lastElement.after(childScope.element);
          children.push(childScope);
        }
        childScope.scope.set(valueIdent, value);
        if (keyIdent) childScope.scope.set(keyIdent, key);
        childScope.scope.updateView();
        lastElement = childScope.element;
        index ++;
      });
      // shrink children
      while(children.length > index) {
        children.pop().element.remove();
      }
    });
  };
}, {exclusive: true});

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

angularDirective("ng-watch", function(expression, element){
  var match = expression.match(/^([^.]*):(.*)$/);
  if (!match) {
    throw "Expecting watch expression 'ident_to_watch: watch_statement' got '"
        + expression + "'";
  }
  return function(){
    this.$watch(match[1], match[2]);
  };
});

//Styling
//
//ng-class
//ng-class-odd, ng-class-even
//ng-style
//ng-show, ng-hide

/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////
/////////////////////////////////////////





//widget related
//ng-validate, ng-required, ng-formatter
//ng-error

//ng-scope ng-controller????