Skip to content
This repository was archived by the owner on Sep 10, 2019. It is now read-only.

Commit f8e91d8

Browse files
committed
Updates to iconic directive to broadcast when svg injection is completed
1 parent ac7ae70 commit f8e91d8

File tree

3 files changed

+127
-8
lines changed

3 files changed

+127
-8
lines changed

js/angular/app.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
angular.module('application', [
55
'ui.router',
66
'ngAnimate',
7+
'ngSVGAttributes',
78

89
//foundation
910
'foundation',
@@ -20,15 +21,22 @@
2021
$urlProvider.otherwise('/');
2122

2223
$locationProvider.html5Mode({
23-
enabled:false,
24+
enabled:true,
2425
requireBase: false
2526
});
26-
27-
$locationProvider.hashPrefix('!');
2827
}
28+
29+
run.$inject = ['$rootScope', '$location', '$compile'];
2930

30-
function run() {
31+
function run($rootScope, $location, $compile) {
3132
FastClick.attach(document.body);
33+
34+
if ($location.$$html5) {
35+
$rootScope.$on('$zfIconicInjected', function(event, injectedElem) {
36+
var angElem = angular.element(injectedElem);
37+
$compile(angElem.contents())(angElem.scope());
38+
});
39+
}
3240
}
3341

3442
})();

js/angular/components/iconic/iconic.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
}
2222
}
2323

24-
zfIconic.$inject = ['Iconic', 'FoundationApi']
24+
zfIconic.$inject = ['Iconic', 'FoundationApi', '$location']
2525

26-
function zfIconic(iconic, foundationApi) {
26+
function zfIconic(iconic, foundationApi, $location) {
2727
var directive = {
2828
restrict: 'A',
2929
template: '<img ng-transclude>',
@@ -52,7 +52,11 @@
5252
iAttrs.$set('data-src', scope.dynSrc);
5353
} else {
5454
// To support expressions on data-src
55-
iAttrs.$set('data-src', assetPath + scope.icon + '.svg');
55+
if (scope.icon) {
56+
iAttrs.$set('data-src', assetPath + scope.icon + '.svg');
57+
} else {
58+
iAttrs.$set('data-src', iAttrs.src);
59+
}
5660
};
5761

5862
var iconicClass;
@@ -75,7 +79,13 @@
7579
function postLink(scope, element, attrs) {
7680
var ico = iconic.getAccess();
7781

78-
ico.inject(element[0]);
82+
ico.inject(element[0], {
83+
each: function(injectedElem) {
84+
if ($location.$$html5) {
85+
scope.$emit('$zfIconicInjected', injectedElem);
86+
}
87+
}
88+
});
7989

8090
foundationApi.subscribe('resize', function() {
8191
ico.update();

js/angular/vendor/svgDirs.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}());

0 commit comments

Comments
 (0)