Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / foundation-apps / js / angular / vendor / svgDirs.js
1 'use strict';
2
3 (function(){
4   var svgDirectives = {};
5
6   angular.forEach([
7       'clipPath',
8       'colorProfile',
9       'src',
10       'cursor',
11       'fill',
12       'filter',
13       'marker',
14       'markerStart',
15       'markerMid',
16       'markerEnd',
17       'mask',
18       'stroke'
19     ],
20     function(attr) {
21       svgDirectives[attr] = [
22           '$rootScope',
23           '$location',
24           '$interpolate',
25           '$sniffer',
26           'urlResolve',
27           'computeSVGAttrValue',
28           'svgAttrExpressions',
29           function(
30               $rootScope,
31               $location,
32               $interpolate,
33               $sniffer,
34               urlResolve,
35               computeSVGAttrValue,
36               svgAttrExpressions) {
37             return {
38               restrict: 'A',
39               link: function(scope, element, attrs) {
40                 var initialUrl;
41
42                 //Only apply to svg elements to avoid unnecessary observing
43                 //Check that is in html5Mode and that history is supported
44                 if ((!svgAttrExpressions.SVG_ELEMENT.test(element[0] &&
45                     element[0].toString())) ||
46                   !$location.$$html5 ||
47                   !$sniffer.history) return;
48
49                 //Assumes no expressions, since svg is unforgiving of xml violations
50                 initialUrl = attrs[attr];
51                 attrs.$observe(attr, updateValue);
52                 $rootScope.$on('$locationChangeSuccess', updateValue);
53
54                 function updateValue () {
55                   var newVal = computeSVGAttrValue(initialUrl);
56                   //Prevent recursive updating
57                   if (newVal && attrs[attr] !== newVal) attrs.$set(attr, newVal);
58                 }
59               }
60             };
61           }];
62   });
63
64   angular.module('ngSVGAttributes', []).
65     factory('urlResolve', [function() {
66       //Duplicate of urlResolve & urlParsingNode in angular core
67       var urlParsingNode = document.createElement('a');
68       return function urlResolve(url) {
69         urlParsingNode.setAttribute('href', url);
70         return urlParsingNode;
71       };
72     }]).
73     value('svgAttrExpressions', {
74       FUNC_URI: /^url\((.*)\)$/,
75       SVG_ELEMENT: /SVG[a-zA-Z]*Element/,
76       HASH_PART: /#.*/
77     }).
78     factory('computeSVGAttrValue', [
79                 '$location', '$sniffer', 'svgAttrExpressions', 'urlResolve',
80         function($location,   $sniffer,   svgAttrExpressions,   urlResolve) {
81           return function computeSVGAttrValue(url) {
82             var match, fullUrl;
83             if (match = svgAttrExpressions.FUNC_URI.exec(url)) {
84               //hash in html5Mode, forces to be relative to current url instead of base
85               if (match[1].indexOf('#') === 0) {
86                 fullUrl = $location.absUrl().
87                   replace(svgAttrExpressions.HASH_PART, '') +
88                   match[1];
89               }
90               //Presumably links to external SVG document
91               else {
92                 fullUrl = urlResolve(match[1]);
93               }
94             }
95             return fullUrl ? 'url(' + fullUrl + ')' : null;
96           };
97         }
98       ]
99     ).
100     directive(svgDirectives);
101 }());