/
jquery-mobile-angular-adapter-1.0.7-rc3-SNAPSHOT.js
1616 lines (1468 loc) · 57 KB
/
jquery-mobile-angular-adapter-1.0.7-rc3-SNAPSHOT.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* jQuery Mobile angularJS adaper v1.0.7-rc3-SNAPSHOT
* http://github.com/tigbro/jquery-mobile-angular-adapter
*
* Copyright 2011, Tobias Bosch (OPITZ CONSULTING GmbH)
* Licensed under the MIT license.
*/
(function($) {
function patch(obj, fnName, callback) {
var _old = obj[fnName];
obj[fnName] = function() {
return callback(_old, this, arguments);
}
}
// selectmenu may create parent elements and extra pages
patch($.mobile.selectmenu.prototype, 'destroy', function(old, self, args) {
old.apply(self, args);
var menuPage = self.menuPage;
var screen = self.screen;
var listbox = self.listbox;
menuPage && menuPage.remove();
screen && screen.remove();
listbox && listbox.remove();
});
// Listview may create subpages that need to be removed when the widget is destroyed.
patch($.mobile.listview.prototype, "destroy", function(old, self, args) {
// Destroy the widget instance first to prevent
// a stack overflow.
// Note: If there are more than 1 listview on the page, childPages will return
// the child pages of all listviews.
var id = self.element.attr('id');
var childPageRegex = new RegExp($.mobile.subPageUrlKey + "=" +id+"-");
var childPages = self.childPages();
old.apply(self, args);
for (var i=0; i<childPages.length; i++) {
var childPage = $(childPages[i]);
var dataUrl = childPage.attr('data-url');
if (dataUrl.match(childPageRegex)) {
childPage.remove();
}
}
});
// Copy of the initialization code from jquery mobile for controlgroup.
// Needed in jqm 1.1, as we want to do a manual initialization.
// See the open task in jqm 1.1 for controlgroup.
if ( $.fn.controlgroup ) {
$( document ).bind( "pagecreate create", function( e ){
$( ":jqmData(role='controlgroup')", e.target )
.jqmEnhanceable()
.controlgroup({ excludeInvisible: false });
});
}
// controlgroup should not exclude invisible children
// as long as it is not visible itself!
patch($.fn, "controlgroup", function(old, self, args) {
if (self.filter(":visible").length===0) {
var options = args[0] || {};
options.excludeInvisible = false;
return old.call(self, options);
}
return old.apply(self, args);
});
})(window.jQuery);
(function (angular) {
var ng = angular.module('ng');
ng.config(['$provide', function($provide) {
$provide.decorator('$rootScope', ['$delegate', function($rootScope) {
$rootScope.$disconnect = function() {
if (this.$root == this) return; // we can't disconnect the root node;
var parent = this.$parent;
this.$$disconnected = true;
// See Scope.$destroy
if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling;
if (this.$$nextSibling) this.$$nextSibling.$$prevSibling = this.$$prevSibling;
this.$$nextSibling = this.$$prevSibling = null;
};
$rootScope.$reconnect = function() {
if (this.$root == this) return; // we can't disconnect the root node;
var child = this;
if (!child.$$disconnected) {
return;
}
var parent = child.$parent;
child.$$disconnected = false;
// See Scope.$new for this logic...
child.$$prevSibling = parent.$$childTail;
if (parent.$$childHead) {
parent.$$childTail.$$nextSibling = child;
parent.$$childTail = child;
} else {
parent.$$childHead = parent.$$childTail = child;
}
};
return $rootScope;
}]);
}]);
})(window.angular);
(function (angular) {
var ng = angular.module('ng');
ng.config(['$provide', function ($provide) {
$provide.decorator('$rootScope', ['$delegate', function ($rootScope) {
var _apply = $rootScope.$apply;
$rootScope.$apply = function () {
if ($rootScope.$$phase) {
return $rootScope.$eval.apply(this, arguments);
}
return _apply.apply(this, arguments);
};
var refreshing = false;
var _digest = $rootScope.$digest;
$rootScope.$digest = function () {
if ($rootScope.$$phase) {
return;
}
var res = _digest.apply(this, arguments);
};
return $rootScope;
}]);
}]);
})(window.angular);
(function ($, angular) {
// Only digest the $.mobile.activePage when rootScope.$digest is called.
var ng = angular.module('ng');
$('div').live('pagebeforeshow', function (event, data) {
var page = $(event.target);
var currPageScope = page.scope();
if (currPageScope) {
currPageScope.$root.$digest();
}
});
ng.config(['$provide', function ($provide) {
$provide.decorator('$rootScope', ['$delegate', function ($rootScope) {
var _$digest = $rootScope.$digest;
var lastActiveScope;
$rootScope.$digest = function () {
if (this === $rootScope) {
var p = $.mobile.activePage;
var activeScope = p && p.scope();
if (lastActiveScope && lastActiveScope !== activeScope) {
lastActiveScope.$disconnect();
}
lastActiveScope = activeScope;
if (activeScope) {
activeScope.$reconnect();
}
}
var res = _$digest.apply(this, arguments);
if (this === $rootScope) {
var hasPages = lastCreatedPages.length;
while (lastCreatedPages.length) {
var pageScope = lastCreatedPages.shift();
// Detach the scope of the created pages from the normal $digest cycle.
// Needed so that only $.mobile.activePage gets digested when rootScope.$digest
// is called.
// However, allow one digest to process every page
// so that we can use ng-repeat also for jqm pages!
pageScope.$disconnect();
}
if (hasPages && !jqmInitialized) {
jqmInitialized = true;
$.mobile.initializePage();
}
}
return res;
};
return $rootScope;
}]);
}]);
var _execFlags = {};
function execWithFlag(flag, fn) {
if (!fn) {
return _execFlags[flag];
}
var old = _execFlags[flag];
_execFlags[flag] = true;
var res = fn();
_execFlags[flag] = old;
return res;
}
function preventJqmWidgetCreation(fn) {
return execWithFlag('preventJqmWidgetCreation', fn);
}
function markJqmWidgetCreation(fn) {
return execWithFlag('markJqmWidgetCreation', fn);
}
function createPagesWithoutPageCreateEvent(pages) {
preventJqmWidgetCreation(function () {
var oldPrefix = $.mobile.page.prototype.widgetEventPrefix;
$.mobile.page.prototype.widgetEventPrefix = 'noop';
pages.page();
$.mobile.page.prototype.widgetEventPrefix = oldPrefix;
});
}
$.mobile.autoInitializePage = false;
var jqmInitialized = false;
var lastCreatedPages = [];
/**
* This directive will preprocess the dom during compile.
* For this, the directive needs to have the highest priority possible,
* so that it is used even before ng-repeat.
*/
var preProcessDirective = {
restrict:'EA',
priority:100000,
compile:function (tElement, tAttrs) {
// Note: We cannot use tAttrs here, as this is also copied when
// angular uses a directive with the template-property and replace-mode.
if (tElement[0].preProcessDirective) {
return;
}
tElement[0].preProcessDirective = true;
// For page elements:
var roleAttr = tAttrs.role;
var isPage = roleAttr == 'page' || roleAttr == 'dialog';
// enhance non-widgets markup.
markJqmWidgetCreation(function () {
preventJqmWidgetCreation(function () {
if (isPage) {
// element contains pages.
// create temporary pages for the non widget markup, that we destroy afterwards.
// This is ok as non widget markup does not hold state, i.e. no permanent reference to the page.
tElement.page();
} else {
if (!tElement[0].jqmEnhanced) {
tElement.parent().trigger("create");
}
}
// Note: The page plugin also enhances child elements,
// so we tag the child elements also in that case.
// Note: We cannot use $.fn.data here, as this is also copied when
// angular uses a directive with the template-property.
var children = tElement[0].getElementsByTagName("*");
for (var i = 0; i < children.length; i++) {
children.item(i).jqmEnhanced = true;
}
tElement[0].jqmEnhanced = true;
});
});
// Destroy the temporary pages again
if (isPage) {
tElement.page("destroy");
}
}
};
/**
* This directive creates the jqm widgets.
*/
var widgetDirective = {
restrict:'EA',
// after the normal angular widgets like input, ngModel, ...
priority:0,
// This will be changed by the setWidgetScopeDirective...
scope:false,
require:['?ngModel'],
compile:function (tElement, tAttrs) {
// Note: We cannot use tAttrs here, as this is also copied when
// angular uses a directive with the template-property and replace-mode.
if (tElement[0].widgetDirective) {
return;
}
tElement[0].widgetDirective = true;
// For page elements:
var roleAttr = tAttrs["role"];
var isPage = roleAttr == 'page' || roleAttr == 'dialog';
var widgets = tElement.data("jqm-widgets");
var linkers = tElement.data("jqm-linkers");
return {
pre:function (scope, iElement, iAttrs) {
if (isPage) {
// Create the page widget without the pagecreate-Event.
// This does no dom transformation, so it's safe to call this in the prelink function.
createPagesWithoutPageCreateEvent(iElement);
lastCreatedPages.push(scope);
}
},
post:function (scope, iElement, iAttrs, ctrls) {
if (widgets && widgets.length) {
var widget;
for (var i = 0; i < widgets.length; i++) {
widget = widgets[i];
widget.create.apply(iElement, widget.args);
}
}
if (linkers) {
for (var i=0; i<linkers.length; i++) {
linkers[i].apply(this, arguments);
}
}
}
};
}
};
/**
* This widget sets or resets the properties of the actual widgetDirective.
* This is especially required for the scope property.
* We need this as angular does not (yet) allow us to create a widget for e.g. data-role="page",
* but not for data-role="content".
*/
var setWidgetScopeDirective = {
restrict:widgetDirective.restrict,
priority:widgetDirective.priority + 1,
compile:function (tElement, tAttrs) {
widgetDirective.scope = (tAttrs.role == 'page' || tAttrs.role == 'dialog');
}
};
/**
* Register our directives for all possible elements with jqm markup.
* Note: We cannot just create a widget for the jqm-widget attribute, that we create,
* as this would not work for the jqm widgets on the root element of the compile
* (angular calculates the directives to apply before calling the compile function of
* any of those directives).
* @param tagList
*/
function registerDirective(tagList) {
for (var i = 0; i < tagList.length; i++) {
ng.directive(tagList[i], function () {
return preProcessDirective;
});
ng.directive(tagList[i], function () {
return setWidgetScopeDirective;
});
ng.directive(tagList[i], function () {
return widgetDirective;
});
}
}
registerDirective(['div', 'role', 'input', 'select', 'button', 'textarea', 'fieldset']);
$.fn.orig = {};
function patchJq(fnName, callback) {
$.fn.orig[fnName] = $.fn.orig[fnName] || $.fn[fnName];
$.fn[fnName] = callback;
}
// If jqm loads a page from an external source, angular needs to compile it too!
ng.run(['$rootScope', '$compile', function ($rootScope, $compile) {
patchJq('page', function () {
if (!preventJqmWidgetCreation() && !this.data("page")) {
if (this.attr("data-" + $.mobile.ns + "external-page")) {
$compile(this)($rootScope);
}
}
return $.fn.orig.page.apply(this, arguments);
});
}]);
function patchJqmWidget(widgetName) {
patchJq(widgetName, function () {
if (markJqmWidgetCreation()) {
for (var k = 0; k < this.length; k++) {
var element = this.eq(k);
var widgetElement = element;
var linkElement = element;
var createData = {
widgetElement:widgetElement,
linkElement:linkElement,
create:$.fn.orig[widgetName]
};
if (jqmNgWidgets[widgetName].precompile) {
jqmNgWidgets[widgetName].precompile(createData);
// allow the precompile to change the element to which
// we add the data.
widgetElement = createData.widgetElement;
linkElement = createData.linkElement;
}
var jqmWidgets = widgetElement.data("jqm-widgets");
if (!jqmWidgets) {
jqmWidgets = [];
widgetElement.data("jqm-widgets", jqmWidgets);
}
var linkers = linkElement.data("jqm-linkers");
if (!linkers) {
linkers = [];
linkElement.data("jqm-linkers", linkers);
}
var widgetExists = false;
for (var i = 0; i < jqmWidgets.length; i++) {
if (jqmWidgets[i].name == widgetName) {
widgetExists = true;
break;
}
}
if (!widgetExists) {
jqmWidgets.push({name:widgetName, args:Array.prototype.slice.call(arguments), create:createData.create});
linkers.push(jqmNgWidgets[widgetName].link);
}
}
}
if (preventJqmWidgetCreation()) {
return false;
}
return $.fn.orig[widgetName].apply(this, arguments);
});
}
var jqmNgWidgets = {};
$.mobile.registerJqmNgWidget = function (widgetName, precompileFn, linkFn) {
jqmNgWidgets[widgetName] = {
precompile: precompileFn,
link:linkFn
};
patchJqmWidget(widgetName);
}
})(window.jQuery, window.angular);
(function (angular, $) {
var widgetConfig = {
button:{
handlers:[disabledHandler],
precompile:buttonPrecompile
},
collapsible:{
handlers:[disabledHandler]
},
textinput:{
handlers:[disabledHandler],
precompile:textinputPrecompile
},
checkboxradio:{
handlers:[disabledHandler, refreshAfterNgModelRender],
precompile:checkboxRadioPrecompile
},
slider:{
handlers:[disabledHandler, refreshAfterNgModelRender],
precompile:sliderPrecompile
},
listview:{
handlers:[refreshOnChildrenChange]
},
collapsibleset:{
handlers:[refreshOnChildrenChange]
},
selectmenu:{
handlers:[disabledHandler, refreshAfterNgModelRender, refreshOnChildrenChange],
precompile:selectmenuPrecompile
},
controlgroup:{
handlers:[refreshControlgroupOnChildrenChange]
},
navbar:{
handlers:[]
},
dialog:{
handlers:[]
},
fixedtoolbar:{
handlers:[]
}
};
function mergeHandlers(widgetName, list) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(widgetName);
for (var i = 0; i < list.length; i++) {
list[i].apply(this, args);
}
}
}
var config;
for (var widgetName in widgetConfig) {
config = widgetConfig[widgetName];
$.mobile.registerJqmNgWidget(widgetName, config.precompile, mergeHandlers(widgetName, config.handlers));
}
// -------------------
// precompile functions
// Checkboxradio wraps the input and label into a new element.
// The angular compiler does not like this, as it changes elements that are not
// in the subtree of the input element that is currently linked.
function checkboxRadioPrecompile(createData) {
var origElement = createData.widgetElement;
// Selectors: See the checkboxradio-Plugin in jqm.
var parentLabel = $(origElement).closest("label");
var label = parentLabel.length ? parentLabel : $(origElement).closest("form,fieldset,:jqmData(role='page'),:jqmData(role='dialog')").find("label").filter("[for='" + origElement[0].id + "']");
var wrapper = $("<div></div>").insertBefore(origElement).append(origElement).append(label);
createData.widgetElement = origElement.parent();
moveCloningDirectives(origElement, createData.widgetElement);
createData.create = function () {
var _wrapAll = $.fn.wrapAll;
var input = this.children("input");
var wrapper = this;
$.fn.wrapAll = function(container) {
if (this[0] === input[0]) {
$.fn.wrapAll = _wrapAll;
var tempContainer = $(container);
wrapper[0].className = tempContainer[0].className;
return origElement;
}
return _wrapAll.apply(this, arguments);
};
var res = $.fn.orig.checkboxradio.apply(input, arguments);
$.fn.wrapAll = _wrapAll;
return res;
}
}
// Slider appends a new element after the input/select element for which it was created.
// The angular compiler does not like this, so we wrap the two elements into a new parent node.
function sliderPrecompile(createData) {
var origElement = createData.widgetElement;
origElement.wrapAll("<div></div>");
var wrapper = createData.widgetElement = createData.widgetElement.parent();
moveCloningDirectives(origElement, wrapper);
createData.create = function () {
return $.fn.orig.slider.apply(this.children().eq(0), arguments);
};
}
// Button wraps itself into a new element.
// Angular does not like this, so we do it in advance.
function buttonPrecompile(createData) {
var origElement = createData.widgetElement;
var wrapper = $( "<div></div>" )
.text( origElement.text() || origElement.val() )
.insertBefore(origElement)
.append(origElement);
moveCloningDirectives(origElement, wrapper);
createData.widgetElement = wrapper;
createData.create = function () {
var wrapper = this;
var button = this.children().eq(0);
var _text = $.fn.text;
$.fn.text = function () {
if (arguments.length>0) {
// Only catch the first setter call
$.fn.text = _text;
return wrapper;
}
return _text.apply(this, arguments);
};
var _insertBefore = $.fn.insertBefore;
$.fn.insertBefore = function (element) {
if (this[0] === wrapper[0] && element[0] === button[0]) {
return wrapper;
}
return _insertBefore.apply(this, arguments);
};
var res = $.fn.orig.button.apply(button, arguments);
$.fn.text = _text;
$.fn.insertBefore = _insertBefore;
return res;
};
}
// selectmenu wraps itself into a new element.
// Angular does not like this, so we do it in advance.
function selectmenuPrecompile(createData) {
var origElement = createData.widgetElement;
var wrapper = $("<div></div>").insertBefore(origElement).append(origElement);
moveCloningDirectives(origElement, wrapper);
createData.widgetElement = wrapper;
createData.create = function () {
var wrapper = this;
var select = this.children().eq(0);
var _wrap = $.fn.wrap;
$.fn.wrap = function(container) {
if (this[0] === select[0]) {
$.fn.wrap = _wrap;
var tempContainer = $(container);
wrapper[0].className = tempContainer[0].className;
return select;
}
return _wrap.apply(this, arguments);
};
var res = $.fn.orig.selectmenu.apply(select, arguments);
$.fn.wrap = _wrap;
return res;
};
}
// textinput for input-type "search" wraps itself into a new element
function textinputPrecompile(createData) {
var origElement = createData.widgetElement;
if ( !origElement.is( "[type='search'],:jqmData(type='search')" ) ) {
return;
}
var wrapper = $("<div></div>").insertBefore(origElement).append(origElement);
moveCloningDirectives(origElement, wrapper);
createData.widgetElement = wrapper;
createData.create = function () {
var wrapper = this;
var select = this.children().eq(0);
var _wrap = $.fn.wrap;
$.fn.wrap = function(container) {
if (this[0] === select[0]) {
$.fn.wrap = _wrap;
var tempContainer = $(container);
wrapper[0].className = tempContainer[0].className;
return select;
}
return _wrap.apply(this, arguments);
};
var res = $.fn.orig.textinput.apply(select, arguments);
$.fn.wrap = _wrap;
return res;
};
}
var CLONING_DIRECTIVE_REGEXP = /(^|[\W])(repeat|switch-when|if)($|[\W])/;
function moveCloningDirectives(source, target) {
// iterate over the attributes
var cloningAttrNames = [];
var node = source[0];
var targetNode = target[0];
var nAttrs = node.attributes;
var attrCount = nAttrs && nAttrs.length;
if (attrCount) {
for (var attr, name,
j = attrCount - 1; j >= 0; j--) {
attr = nAttrs[j];
name = attr.name;
if (CLONING_DIRECTIVE_REGEXP.test(name)) {
node.removeAttributeNode(attr);
targetNode.setAttributeNode(attr);
}
}
}
// iterate over the class names.
var targetClassName = '';
var className = node.className;
var match;
if (className) {
className = className.replace(/[^;]+;?/, function (match) {
if (CLONING_DIRECTIVE_REGEXP.test(match)) {
targetClassName += match;
return '';
}
return match;
});
}
if (targetClassName) {
targetNode.className = targetClassName;
node.className = className;
}
}
// Expose for tests.
$.mobile.moveCloningDirectives = moveCloningDirectives;
// -------------------
// link handlers
function disabledHandler(widgetName, scope, iElement, iAttrs, ctrls) {
iAttrs.$observe("disabled", function (value) {
if (value) {
iElement[widgetName]("disable");
} else {
iElement[widgetName]("enable");
}
});
}
function addCtrlFunctionListener(ctrl, ctrlFnName, fn) {
var listenersName = "_listeners" + ctrlFnName;
if (!ctrl[listenersName]) {
ctrl[listenersName] = [];
var oldFn = ctrl[ctrlFnName];
ctrl[ctrlFnName] = function () {
var res = oldFn.apply(this, arguments);
for (var i = 0; i < ctrl[listenersName].length; i++) {
ctrl[listenersName][i]();
}
return res;
};
}
ctrl[listenersName].push(fn);
}
function refreshAfterNgModelRender(widgetName, scope, iElement, iAttrs, ctrls) {
var ngModelCtrl = ctrls[0];
if (ngModelCtrl) {
addCtrlFunctionListener(ngModelCtrl, "$render", function () {
triggerAsyncRefresh(widgetName, scope, iElement, "refresh");
});
}
}
function refreshControlgroupOnChildrenChange(widgetName, scope, iElement, iAttrs, ctrls) {
iElement.bind("$childrenChanged", function () {
triggerAsyncRefresh(widgetName, scope, iElement, {});
});
}
function refreshOnChildrenChange(widgetName, scope, iElement, iAttrs, ctrls) {
iElement.bind("$childrenChanged", function () {
triggerAsyncRefresh(widgetName, scope, iElement, "refresh");
});
}
function triggerAsyncRefresh(widgetName, scope, iElement, options) {
var prop = "_refresh" + widgetName;
scope[prop] = scope[prop] + 1 || 1;
scope.$evalAsync(function () {
scope[prop]--;
if (scope[prop] === 0) {
iElement[widgetName](options);
}
});
}
})(window.angular, window.jQuery);
(function (angular, $) {
/**
* Deactivate the url changing capabilities
* of angular, so we do not get into trouble with
* jquery mobile: angular saves the current url before a $digest
* and updates the url after the $digest.
* <p>
* This also replaces the hashListen implementation
* of angular by the jquery mobile impementation,
* so we do not have two polling functions, ...
* <p>
* Attention: By this, urls can no more be changed via angular's $location service!
*/
var ng = angular.module("ng");
function deactivateAngularLocationService($browser) {
$browser.onHashChange = function (handler) {
$(window).bind('hashchange', handler);
return handler;
};
var lastUrl = location.href;
$browser.url = function (url) {
if (url) {
lastUrl = url;
}
return lastUrl;
};
}
deactivateAngularLocationService.$inject = ['$browser'];
ng.run(deactivateAngularLocationService);
})(window.angular, window.jQuery);
(function ($, angular) {
// Patch for ng-repeat to fire an event whenever the children change.
// Only watching Scope create/destroy is not enough here, as ng-repeat
// caches the scopes during reordering.
function shallowEquals(collection1, collection2) {
if (!!collection1 ^ !!collection2) {
return false;
}
for (var x in collection1) {
if (collection2[x] !== collection1[x]) {
return false;
}
}
for (var x in collection2) {
if (collection2[x] !== collection1[x]) {
return false;
}
}
return true;
}
function shallowClone(collection) {
if (!collection) {
return collection;
}
var res;
if (collection.length) {
res = [];
} else {
res = {};
}
for (var x in collection) {
res[x] = collection[x];
}
return res;
}
var mod = angular.module('ng');
mod.directive('ngRepeat', function () {
return {
priority:1000, // same as original repeat
compile:function (element, attr, linker) {
return {
pre:function (scope, iterStartElement, attr) {
var expression = attr.ngRepeat;
var match = expression.match(/^.+in\s+(.*)\s*$/);
if (!match) {
throw Error("Expected ngRepeat in form of '_item_ in _collection_' but got '" +
expression + "'.");
}
var collectionExpr = match[1];
var lastCollection;
var changeCounter = 0;
scope.$watch(function () {
var collection = scope.$eval(collectionExpr);
if (!shallowEquals(collection, lastCollection)) {
lastCollection = shallowClone(collection);
changeCounter++;
}
return changeCounter;
}, function () {
// Note: need to be parent() as jquery cannot trigger events on comments
// (angular creates a comment node when using transclusion, as ng-repeat does).
iterStartElement.parent().trigger("$childrenChanged");
});
}
};
}
};
});
})(window.jQuery, window.angular);
(function ($, angular) {
// This is a copy of parts of angular's ngOptions directive to detect changes in the values
// of ngOptions (emits the $childrenChanged event on the scope).
// This is needed as ngOptions does not provide a way to listen to changes.
function sortedKeys(obj) {
var keys = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys.sort();
}
var NG_OPTIONS_REGEXP = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w\d]*)|(?:\(\s*([\$\w][\$\w\d]*)\s*,\s*([\$\w][\$\w\d]*)\s*\)))\s+in\s+(.*)$/;
var mod = angular.module('ng');
mod.directive('ngOptions', ['$parse', function ($parse) {
return {
require: ['select', '?ngModel'],
link:function (scope, element, attr, ctrls) {
// if ngModel is not defined, we don't need to do anything
if (!ctrls[1]) return;
var match;
var optionsExp = attr.ngOptions;
if (! (match = optionsExp.match(NG_OPTIONS_REGEXP))) {
throw Error(
"Expected ngOptions in form of '_select_ (as _label_)? for (_key_,)?_value_ in _collection_'" +
" but got '" + optionsExp + "'.");
}
var displayFn = $parse(match[2] || match[1]),
valueName = match[4] || match[6],
keyName = match[5],
groupByFn = $parse(match[3] || ''),
valueFn = $parse(match[2] ? match[1] : valueName),
valuesFn = $parse(match[7]);
scope.$watch(optionsModel, function() {
element.trigger("$childrenChanged");
}, true);
function optionsModel() {
var optionGroups = [], // Temporary location for the option groups before we render them
optionGroupName,
values = valuesFn(scope) || [],
keys = keyName ? sortedKeys(values) : values,
length,
index,
locals = {};
// We now build up the list of options we need (we merge later)
for (index = 0; length = keys.length, index < length; index++) {
var value = values[index];
locals[valueName] = values[keyName ? locals[keyName]=keys[index]:index];
optionGroupName = groupByFn(scope, locals);
optionGroups.push({
id: keyName ? keys[index] : index, // either the index into array or key from object
label: displayFn(scope, locals), // what will be seen by the user
optionGroup: optionGroupName
});
}
return optionGroups;
}
}
};
}]);
})(window.jQuery, window.angular);
(function (angular) {
var ng = angular.module("ng");
ng.directive('option', ['$interpolate', function ($interpolate) {
return {
restrict:'E',
compile:function (tElement, tAttrs) {
var textInterpolateFn = $interpolate(tElement.text(), true);
var valueInterpolateFn = $interpolate(tElement.attr('value'), true);
return function (scope, iElement, iAttrs) {
scope.$watch(textInterpolateFn, function () {
iElement.trigger("$childrenChanged");
});
scope.$watch(valueInterpolateFn, function () {
iElement.trigger("$childrenChanged");
});
}
}
};
}]);
})(window.angular);
(function (angular) {
var ng = angular.module("ng");
ng.directive('li', function() {
return {
restrict:'E',
compile:function (tElement, tAttrs) {
return function (scope, iElement, iAttrs) {
iElement.bind("$childrenChanged", function () {
iElement.removeClass("ui-li");
var buttonElements = iElement.data("buttonElements");
if (buttonElements) {
var text = buttonElements.text;
while (text.firstChild) {
iElement[0].appendChild(text.firstChild);
}
$(buttonElements.inner).remove();
}
iElement.removeData("buttonElements");
});
}
}
};
});
})(window.angular);
(function (angular) {
// Patch for ng-switch to fire an event whenever the children change.
var ng = angular.module("ng");
ng.directive("ngSwitch",
function () {
return {
restrict:'EA',
compile:function (element, attr) {
var watchExpr = attr.ngSwitch || attr.on;
return function (scope, element) {
scope.$watch(watchExpr, function (value) {
element.trigger("$childrenChanged");
});
}
}
}
});
})(window.angular);
(function (angular) {