summaryrefslogtreecommitdiffstats
path: root/ng-vaadin.js
blob: 07f1664a2a3e4543107c9761f73426db3160fb89 (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
(function(angular) {'use strict';

  var vModule = angular.module('vaadin-x', []);

  function attrFnc() {
    return {
      restrict : 'E',
      link : function(scope, element, attrs) {

        var attrMap = {};
        for ( var prop in attrs.$attr) {
          var _attr = attrs.$attr[prop];
          var _match = element.attr(_attr).match(/\{\{\s*([\.\w]+)\s*\}\}/);
          if (_match) {
            attrMap[_attr] = _match[1];
          }
        }

        new MutationObserver(function() {
          scope.$apply();
        }).observe(element[0], {
          attributes : true
        });

        scope.$watch(function() {
          for ( var attr in attrMap) {
            var value = element.attr(attr);
            var tokens = attrMap[attr].split(/\./);
            var parent = scope;
            for (var i = 0; i < tokens.length - 1; i++) {
              if (typeof (parent[tokens[i]]) == 'undefined') {
                parent[tokens[i]] = {};
              }
              parent = parent[tokens[i]];
            }
            // console.log(tokens[tokens.length - 1] + " " + value);
            parent[tokens[tokens.length - 1]] = value;
          }
        });
      }
    };
  }

  var vaadinComponents = ['vSlider', 'vProgress', 'vGrid'];

  angular.forEach(vaadinComponents, function(tag) {
    vModule.directive(tag, attrFnc);
  });

})(angular);