Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / foundation-apps / js / angular / components / interchange / interchange.js
1 (function() {
2   'use strict';
3
4   angular.module('foundation.interchange', ['foundation.core', 'foundation.mediaquery'])
5     .directive('zfInterchange', zfInterchange)
6   ;
7
8   zfInterchange.$inject = [ '$compile', '$http', '$templateCache', 'FoundationApi', 'FoundationMQ'];
9
10   function zfInterchange($compile, $http, $templateCache, foundationApi, foundationMQ) {
11
12     var directive = {
13       restrict: 'EA',
14       transclude: 'element',
15       scope: {
16         position: '@'
17       },
18       replace: true,
19       template: '<div></div>',
20       link: link
21     };
22
23     return directive;
24
25     function link(scope, element, attrs, ctrl, transclude) {
26       var childScope, current, scenarios, innerTemplates;
27
28       var globalQueries = foundationMQ.getMediaQueries();
29
30       //setup
31       foundationApi.subscribe('resize', function(msg) {
32         transclude(function(clone, newScope) {
33           if(!scenarios || !innerTemplates) {
34             collectInformation(clone);
35           }
36
37           var ruleMatches = foundationMQ.match(scenarios);
38           var scenario = ruleMatches.length === 0 ? null : scenarios[ruleMatches[0].ind];
39
40           //this could use some love
41           if(scenario && checkScenario(scenario)) {
42             var compiled;
43
44             if(childScope) {
45               childScope.$destroy();
46               childScope = null;
47             }
48
49             if(typeof scenario.templ !== 'undefined') {
50               childScope = newScope;
51
52               //temp container
53               var tmp = document.createElement('div');
54               tmp.appendChild(innerTemplates[scenario.templ][0]);
55
56               element.html(tmp.innerHTML);
57               $compile(element.contents())(childScope);
58               current = scenario;
59             } else {
60               var loader = templateLoader(scenario.src);
61               loader.success(function(html) {
62                 childScope = newScope;
63                 element.html(html);
64               }).then(function(){
65                 $compile(element.contents())(childScope);
66                 current = scenario;
67               });
68             }
69           }
70         });
71
72       });
73
74       //init
75       foundationApi.publish('resize', 'initial resize');
76
77       function templateLoader(templateUrl) {
78         return $http.get(templateUrl, {cache: $templateCache});
79       }
80
81       function collectInformation(el) {
82         var data = foundationMQ.collectScenariosFromElement(el);
83
84         scenarios = data.scenarios;
85         innerTemplates = data.templates;
86       }
87
88       function checkScenario(scenario) {
89         return !current || current !== scenario;
90       }
91     }
92   }
93
94 })();