-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.angular.js
1488 lines (1263 loc) · 52.3 KB
/
kendo.angular.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
(function(f, define){
define([ "./kendo.core" ], f);
})(function() {
var __meta__ = { // jshint ignore:line
id: "angular",
name: "AngularJS Directives",
category: "framework",
description: "Adds Kendo UI for AngularJS directives",
depends: [ "core" ],
defer: true
};
(function ($, angular, undefined) {
"use strict";
// Angular2 exposes a global angular object, but it does not have an injector...
if (!angular || !angular.injector) {
return;
}
/*jshint eqnull:true,loopfunc:true,-W052,-W028 */
var module = angular.module('kendo.directives', []),
$injector = angular.injector(['ng']),
$parse = $injector.get('$parse'),
$timeout = $injector.get('$timeout'),
$defaultCompile,
$log = $injector.get('$log');
function withoutTimeout(f) {
var save = $timeout;
try {
$timeout = function(f){ return f(); };
return f();
} finally {
$timeout = save;
}
}
var OPTIONS_NOW;
var createDataSource = (function() {
var types = {
TreeList : 'TreeListDataSource',
TreeView : 'HierarchicalDataSource',
Scheduler : 'SchedulerDataSource',
PivotGrid : 'PivotDataSource',
PivotConfigurator : 'PivotDataSource',
PanelBar : 'HierarchicalDataSource',
Menu : "$PLAIN",
ContextMenu : "$PLAIN"
};
var toDataSource = function(dataSource, type) {
if (type == '$PLAIN') {
return dataSource;
}
return kendo.data[type].create(dataSource);
};
return function(scope, element, role, source) {
var type = types[role] || 'DataSource';
var current = scope.$eval(source);
var ds = toDataSource(current, type);
scope.$watch(source, function(mew) {
var widget = kendoWidgetInstance(element);
if (widget && typeof widget.setDataSource == "function") {
if (mew !== current) {
var ds = toDataSource(mew, type);
widget.setDataSource(ds);
current = mew;
}
}
});
return ds;
};
}());
var ignoredAttributes = {
kDataSource : true,
kOptions : true,
kRebind : true,
kNgModel : true,
kNgDelay : true
};
var ignoredOwnProperties = {
// XXX: other names to ignore here?
name : true,
title : true,
style : true
};
function createWidget(scope, element, attrs, widget, origAttr, controllers) {
/* jshint latedef: false */
if (!(element instanceof jQuery)) {
throw new Error("The Kendo UI directives require jQuery to be available before AngularJS. Please include jquery before angular in the document.");
}
var kNgDelay = attrs.kNgDelay,
delayValue = scope.$eval(kNgDelay);
controllers = controllers || [];
var ngModel = controllers[0],
ngForm = controllers[1];
var ctor = $(element)[widget];
if (!ctor) {
window.console.error("Could not find: " + widget);
return null;
}
var parsed = parseOptions(scope, element, attrs, widget, ctor);
var options = parsed.options;
if (parsed.unresolved.length) {
var promises = [];
for (var i = 0, len = parsed.unresolved.length; i < len; i++) {
var unresolved = parsed.unresolved[i];
var promise = $.Deferred(function(d) {
var unwatch = scope.$watch(unresolved.path, function(newValue) {
if (newValue !== undefined) {
unwatch();
d.resolve();
}
});
}).promise();
promises.push(promise);
}
$.when.apply(null, promises).then(createIt);
return;
}
if (kNgDelay && !delayValue) {
var root = scope.$root || scope;
var register = function() {
var unregister = scope.$watch(kNgDelay, function(newValue) {
if (newValue !== undefined) {
unregister();
// remove subsequent delays, to make ng-rebind work
element.removeAttr(attrs.$attr.kNgDelay);
kNgDelay = null;
$timeout(createIt); // XXX: won't work without `timeout` ;-\
}
});
};
// WARNING: the watchers should be registered in the digest cycle.
// the fork here is for the timeout/non-timeout initiated widgets.
if (/^\$(digest|apply)$/.test(root.$$phase)) {
register();
} else {
scope.$apply(register);
}
return;
} else {
return createIt();
}
function createIt() {
var originalElement;
if (attrs.kRebind) {
originalElement = $($(element)[0].cloneNode(true));
}
// re-parse the options here.
options = parseOptions(scope, element, attrs, widget, ctor).options;
if (element.is("select")) {
(function(options){
if (options.length > 0) {
var first = $(options[0]);
if (!/\S/.test(first.text()) && /^\?/.test(first.val())) {
first.remove();
}
}
}(element[0].options));
}
var object = ctor.call(element, OPTIONS_NOW = options).data(widget);
exposeWidget(object, scope, attrs, widget, origAttr);
scope.$emit("kendoWidgetCreated", object);
var destroyRegister = destroyWidgetOnScopeDestroy(scope, object);
if (attrs.kRebind) {
setupRebind(object, scope, element, originalElement, attrs.kRebind, destroyRegister, attrs);
}
if (attrs.kNgDisabled) {
var kNgDisabled = attrs.kNgDisabled;
var isDisabled = scope.$eval(kNgDisabled);
if (isDisabled) {
object.enable(!isDisabled);
}
bindToKNgDisabled(object, scope, element, kNgDisabled);
}
if (attrs.kNgReadonly) {
var kNgReadonly = attrs.kNgReadonly;
var isReadonly = scope.$eval(kNgReadonly);
if (isReadonly) {
object.readonly(isReadonly);
}
bindToKNgReadonly(object, scope, element, kNgReadonly);
}
// kNgModel is used for the "logical" value
if (attrs.kNgModel) {
bindToKNgModel(object, scope, attrs.kNgModel);
}
// 2 way binding: ngModel <-> widget.value()
if (ngModel) {
bindToNgModel(object, scope, element, ngModel, ngForm);
}
if (object) {
propagateClassToWidgetWrapper(object, element);
}
return object;
}
}
function parseOptions(scope, element, attrs, widget, ctor) {
var role = widget.replace(/^kendo/, '');
var unresolved = [];
var optionsPath = attrs.kOptions || attrs.options;
var optionsValue = scope.$eval(optionsPath);
if (optionsPath && optionsValue === undefined) {
unresolved.push({ option: "options", path: optionsPath });
}
var options = angular.extend({}, attrs.defaultOptions, optionsValue);
function addOption(name, value) {
var scopeValue = angular.copy(scope.$eval(value));
if (scopeValue === undefined) {
unresolved.push({ option: name, path: value });
} else {
options[name] = scopeValue;
}
}
var widgetOptions = ctor.widget.prototype.options;
var widgetEvents = ctor.widget.prototype.events;
$.each(attrs, function(name, value) {
if (name === "source" || name === "kDataSource" || name === "kScopeField" || name === "scopeField") {
return;
}
var dataName = "data" + name.charAt(0).toUpperCase() + name.slice(1);
if (name.indexOf("on") === 0) { // let's search for such event.
var eventKey = name.replace(/^on./, function(prefix) {
return prefix.charAt(2).toLowerCase();
});
if (widgetEvents.indexOf(eventKey) > -1) {
options[eventKey] = value;
}
} // don't elsif here - there are on* options
if (widgetOptions.hasOwnProperty(dataName)) {
addOption(dataName, value);
} else if (widgetOptions.hasOwnProperty(name) && !ignoredOwnProperties[name]) {
addOption(name, value);
} else if (!ignoredAttributes[name]) {
var match = name.match(/^k(On)?([A-Z].*)/);
if (match) {
var optionName = match[2].charAt(0).toLowerCase() + match[2].slice(1);
if (match[1] && name != "kOnLabel" // XXX: k-on-label can be used on MobileSwitch :-\
) {
options[optionName] = value;
} else {
if (name == "kOnLabel") {
optionName = "onLabel"; // XXX: that's awful.
}
addOption(optionName, value);
}
}
}
});
// parse the datasource attribute
var dataSource = attrs.kDataSource || attrs.source;
if (dataSource) {
options.dataSource = createDataSource(scope, element, role, dataSource);
}
// deepExtend in kendo.core (used in Editor) will fail with stack
// overflow if we don't put it in an array :-\
options.$angular = [ scope ];
return {
options: options,
unresolved: unresolved
};
}
function bindToKNgDisabled(widget, scope, element, kNgDisabled) {
if ((kendo.ui.PanelBar && widget instanceof kendo.ui.PanelBar) || (kendo.ui.Menu && widget instanceof kendo.ui.Menu)) {
$log.warn("k-ng-disabled specified on a widget that does not have the enable() method: " + (widget.options.name));
return;
}
scope.$watch(kNgDisabled, function(newValue, oldValue) {
if (newValue != oldValue) {
widget.enable(!newValue);
}
});
}
function bindToKNgReadonly(widget, scope, element, kNgReadonly) {
if (typeof widget.readonly != "function") {
$log.warn("k-ng-readonly specified on a widget that does not have the readonly() method: " + (widget.options.name));
return;
}
scope.$watch(kNgReadonly, function(newValue, oldValue) {
if (newValue != oldValue) {
widget.readonly(newValue);
}
});
}
function exposeWidget(widget, scope, attrs, kendoWidget, origAttr) {
if (attrs[origAttr]) {
var set = $parse(attrs[origAttr]).assign;
if (set) {
// set the value of the expression to the kendo widget object to expose its api
set(scope, widget);
} else {
throw new Error(origAttr + ' attribute used but expression in it is not assignable: ' + attrs[kendoWidget]);
}
}
}
function formValue(element) {
if (/checkbox|radio/i.test(element.attr("type"))) {
return element.prop("checked");
}
return element.val();
}
var formRegExp = /^(input|select|textarea)$/i;
function isForm(element) {
return formRegExp.test(element[0].tagName);
}
function bindToNgModel(widget, scope, element, ngModel, ngForm) {
if (!widget.value) {
return;
}
var value;
// Some widgets trigger "change" on the input field
// and this would result in two events sent (#135)
var haveChangeOnElement = false;
if (isForm(element)) {
value = function() {
return formValue(element);
};
} else {
value = function() {
return widget.value();
};
}
// Angular will invoke $render when the view needs to be updated with the view value.
var viewRender = function() {
// Update the widget with the view value.
// delaying with setTimout for cases where the datasource is set thereafter.
// https://github.com/kendo-labs/angular-kendo/issues/304
var val = ngModel.$viewValue;
if (val === undefined) {
val = ngModel.$modelValue;
}
if (val === undefined) {
val = null;
}
haveChangeOnElement = true;
setTimeout(function(){
haveChangeOnElement = false;
if (widget) { // might have been destroyed in between. :-(
var kNgModel = scope[widget.element.attr("k-ng-model")];
if (kNgModel) {
val = kNgModel;
}
if (widget.options.autoBind === false && !widget.listView.bound()) {
if (val) {
widget.value(val);
}
} else {
widget.value(val);
}
}
}, 0);
};
ngModel.$render = viewRender;
setTimeout(function() {
if (ngModel.$render !== viewRender) {
ngModel.$render = viewRender;
ngModel.$render();
}
});
if (isForm(element)) {
element.on("change", function() {
haveChangeOnElement = true;
});
}
var onChange = function(pristine) {
return function() {
var formPristine;
if (haveChangeOnElement && !element.is("select")) {
return;
}
if (pristine && ngForm) {
formPristine = ngForm.$pristine;
}
ngModel.$setViewValue(value());
if (pristine) {
ngModel.$setPristine();
if (formPristine) {
ngForm.$setPristine();
}
}
digest(scope);
};
};
widget.first("change", onChange(false));
widget.first("spin", onChange(false));
if (!(kendo.ui.AutoComplete && widget instanceof kendo.ui.AutoComplete)) {
widget.first("dataBound", onChange(true));
}
var currentVal = value();
// if the model value is undefined, then we set the widget value to match ( == null/undefined )
// In telerik/kendo-ui-core#1027 we discovered that after the timeout the $viewValue arives as NaN in some weird, default form.
// Hence the check below.
if (!isNaN(ngModel.$viewValue) && currentVal != ngModel.$viewValue) {
if (!ngModel.$isEmpty(ngModel.$viewValue)) {
widget.value(ngModel.$viewValue);
} else if (currentVal != null && currentVal !== "" && currentVal != ngModel.$viewValue) {
ngModel.$setViewValue(currentVal);
}
}
ngModel.$setPristine();
}
function bindToKNgModel(widget, scope, kNgModel) {
if (typeof widget.value != "function") {
$log.warn("k-ng-model specified on a widget that does not have the value() method: " + (widget.options.name));
return;
}
var form = $(widget.element).parents("form");
var ngForm = kendo.getter(form.attr("name"), true)(scope);
var getter = $parse(kNgModel);
var setter = getter.assign;
var updating = false;
var valueIsCollection = kendo.ui.MultiSelect && widget instanceof kendo.ui.MultiSelect;
var length = function(value) {
//length is irrelevant when value is not collection
return value && valueIsCollection ? value.length : 0;
};
var currentValueLength = length(getter(scope));
widget.$angular_setLogicValue(getter(scope));
// keep in sync
var watchHandler = function(newValue, oldValue) {
if (newValue === undefined) {
// because widget's value() method usually checks if the new value is undefined,
// in which case it returns the current value rather than clearing the field.
// https://github.com/telerik/kendo-ui-core/issues/299
newValue = null;
}
//compare values by reference if a collection
if (updating || (newValue == oldValue && length(newValue) == currentValueLength)) {
return;
}
currentValueLength = length(newValue);
widget.$angular_setLogicValue(newValue);
};
if (valueIsCollection) {
scope.$watchCollection(kNgModel, watchHandler);
} else {
scope.$watch(kNgModel, watchHandler);
}
var changeHandler = function() {
updating = true;
if (ngForm && ngForm.$pristine) {
ngForm.$setDirty();
}
digest(scope, function(){
setter(scope, widget.$angular_getLogicValue());
currentValueLength = length(getter(scope));
});
updating = false;
};
widget.first("change", changeHandler);
widget.first("spin", changeHandler);
}
function destroyWidgetOnScopeDestroy(scope, widget) {
var deregister = scope.$on("$destroy", function() {
deregister();
if (widget) {
kendo.destroy(widget.element);
widget = null;
}
});
return deregister;
}
// mutation observers - propagate the original
// element's class to the widget wrapper.
function propagateClassToWidgetWrapper(widget, element) {
if (!(window.MutationObserver && widget.wrapper)) {
return;
}
var prevClassList = [].slice.call($(element)[0].classList);
var mo = new MutationObserver(function(changes){
suspend(); // make sure we don't trigger a loop
if (!widget) {
return;
}
changes.forEach(function(chg){
var w = $(widget.wrapper)[0];
switch (chg.attributeName) {
case "class":
// sync classes to the wrapper element
var currClassList = [].slice.call(chg.target.classList);
currClassList.forEach(function(cls){
if (prevClassList.indexOf(cls) < 0) {
w.classList.add(cls);
if (kendo.ui.ComboBox && widget instanceof kendo.ui.ComboBox) { // https://github.com/kendo-labs/angular-kendo/issues/356
widget.input[0].classList.add(cls);
}
}
});
prevClassList.forEach(function(cls){
if (currClassList.indexOf(cls) < 0) {
w.classList.remove(cls);
if (kendo.ui.ComboBox && widget instanceof kendo.ui.ComboBox) { // https://github.com/kendo-labs/angular-kendo/issues/356
widget.input[0].classList.remove(cls);
}
}
});
prevClassList = currClassList;
break;
case "disabled":
if (typeof widget.enable == "function" && !widget.element.attr("readonly")) {
widget.enable(!$(chg.target).attr("disabled"));
}
break;
case "readonly":
if (typeof widget.readonly == "function" && !widget.element.attr("disabled")) {
widget.readonly(!!$(chg.target).attr("readonly"));
}
break;
}
});
resume();
});
function suspend() {
mo.disconnect();
}
function resume() {
mo.observe($(element)[0], { attributes: true });
}
resume();
widget.first("destroy", suspend);
}
function setupRebind(widget, scope, element, originalElement, rebindAttr, destroyRegister, attrs) {
// watch for changes on the expression passed in the k-rebind attribute
var unregister = scope.$watch(rebindAttr, function(newValue, oldValue) {
if (!widget._muteRebind && newValue !== oldValue) {
unregister(); // this watcher will be re-added if we compile again!
if (attrs._cleanUp) {
attrs._cleanUp();
}
var templateOptions = WIDGET_TEMPLATE_OPTIONS[widget.options.name];
if (templateOptions) {
templateOptions.forEach(function(name) {
var templateContents = scope.$eval(attrs["k" + name]);
if (templateContents) {
originalElement.append($(templateContents).attr(kendo.toHyphens("k" + name), ""));
}
});
}
var _wrapper = $(widget.wrapper)[0];
var _element = $(widget.element)[0];
var isUpload = widget.options.name === "Upload";
if (isUpload) {
element = $(_element);
}
var compile = element.injector().get("$compile");
widget._destroy();
if (destroyRegister) {
destroyRegister();
}
widget = null;
if (_element) {
if (_wrapper) {
_wrapper.parentNode.replaceChild(_element, _wrapper);
}
$(element).replaceWith(originalElement);
}
compile(originalElement)(scope);
}
}, true); // watch for object equality. Use native or simple values.
digest(scope);
}
function bind(f, obj) {
return function(a, b) {
return f.call(obj, a, b);
};
}
function setTemplate(key, value) {
this[key] = kendo.stringify(value); // jshint ignore:line
}
module.factory('directiveFactory', [ '$compile', function(compile) {
var kendoRenderedTimeout;
var RENDERED = false;
// caching $compile for the dirty hack upstairs. This is awful, but we happen to have elements outside of the bootstrapped root :(.
$defaultCompile = compile;
var create = function(role, origAttr) {
return {
// Parse the directive for attributes and classes
restrict: "AC",
require: [ "?ngModel", "^?form" ],
scope: false,
controller: [ '$scope', '$attrs', '$element', function($scope, $attrs) {
this.template = bind(setTemplate, $attrs);
$attrs._cleanUp = bind(function(){
this.template = null;
$attrs._cleanUp = null;
}, this);
}],
link: function(scope, element, attrs, controllers) {
var $element = $(element);
// we must remove data-kendo-widget-name attribute because
// it breaks kendo.widgetInstance; can generate all kinds
// of funny issues like
//
// https://github.com/kendo-labs/angular-kendo/issues/167
//
// but we still keep the attribute without the
// `data-` prefix, so k-rebind would work.
var roleattr = role.replace(/([A-Z])/g, "-$1");
$element.attr(roleattr, $element.attr("data-" + roleattr));
$element[0].removeAttribute("data-" + roleattr);
var widget = createWidget(scope, element, attrs, role, origAttr, controllers);
if (!widget) {
return;
}
if (kendoRenderedTimeout) {
clearTimeout(kendoRenderedTimeout);
}
kendoRenderedTimeout = setTimeout(function() {
scope.$emit("kendoRendered");
if (!RENDERED) {
RENDERED = true;
$("form").each(function(){
var form = $(this).controller("form");
if (form) {
form.$setPristine();
}
});
}
});
}
};
};
return {
create: create
};
}]);
var TAGNAMES = {
Editor : "textarea",
NumericTextBox : "input",
DatePicker : "input",
DateTimePicker : "input",
TimePicker : "input",
AutoComplete : "input",
ColorPicker : "input",
MaskedTextBox : "input",
MultiSelect : "input",
Upload : "input",
Validator : "form",
Button : "button",
MobileButton : "a",
MobileBackButton : "a",
MobileDetailButton : "a",
ListView : "ul",
MobileListView: "ul",
PanelBar : "ul",
TreeView : "ul",
Menu : "ul",
ContextMenu : "ul",
ActionSheet : "ul"
};
var SKIP_SHORTCUTS = [
'MobileView',
'MobileDrawer',
'MobileLayout',
'MobileSplitView',
'MobilePane',
'MobileModalView'
];
var MANUAL_DIRECTIVES = [
'MobileApplication',
'MobileView',
'MobileModalView',
'MobileLayout',
'MobileActionSheet',
'MobileDrawer',
'MobileSplitView',
'MobilePane',
'MobileScrollView',
'MobilePopOver'
];
angular.forEach(['MobileNavBar', 'MobileButton', 'MobileBackButton', 'MobileDetailButton', 'MobileTabStrip', 'MobileScrollView', 'MobileScroller'], function(widget) {
MANUAL_DIRECTIVES.push(widget);
widget = "kendo" + widget;
module.directive(widget, function() {
return {
restrict: "A",
link: function(scope, element, attrs) {
createWidget(scope, element, attrs, widget, widget);
}
};
});
});
function createDirectives(klass, isMobile) {
function make(directiveName, widgetName) {
module.directive(directiveName, [
"directiveFactory",
function(directiveFactory) {
return directiveFactory.create(widgetName, directiveName);
}
]);
}
var name = isMobile ? "Mobile" : "";
name += klass.fn.options.name;
var className = name;
var shortcut = "kendo" + name.charAt(0) + name.substr(1).toLowerCase();
name = "kendo" + name;
// <kendo-numerictextbox>-type directives
var dashed = name.replace(/([A-Z])/g, "-$1");
if (SKIP_SHORTCUTS.indexOf(name.replace("kendo", "")) == -1) {
var names = name === shortcut ? [ name ] : [ name, shortcut ];
angular.forEach(names, function(directiveName) {
module.directive(directiveName, function(){
return {
restrict : "E",
replace : true,
template : function(element, attributes) {
var tag = TAGNAMES[className] || "div";
var scopeField = attributes.kScopeField || attributes.scopeField;
return "<" + tag + " " + dashed + (scopeField ? ('="' + scopeField + '"') : "") + ">" + element.html() + "</" + tag + ">";
}
};
});
});
}
if (MANUAL_DIRECTIVES.indexOf(name.replace("kendo", "")) > -1) {
return;
}
// here name should be like kendoMobileListView so kendo-mobile-list-view works,
// and shortcut like kendoMobilelistview, for kendo-mobilelistview
make(name, name);
if (shortcut != name) {
make(shortcut, name);
}
}
/* -----[ utils ]----- */
function kendoWidgetInstance(el) {
el = $(el);
return kendo.widgetInstance(el, kendo.ui) ||
kendo.widgetInstance(el, kendo.mobile.ui) ||
kendo.widgetInstance(el, kendo.dataviz.ui);
}
function digest(scope, func) {
var root = scope.$root || scope;
var isDigesting = /^\$(digest|apply)$/.test(root.$$phase);
if (func) {
if (isDigesting) {
func();
} else {
root.$apply(func);
}
} else if (!isDigesting) {
root.$digest();
}
}
function destroyScope(scope, el) {
scope.$destroy();
if (el) {
// prevent leaks. https://github.com/kendo-labs/angular-kendo/issues/237
$(el)
.removeData("$scope")
.removeData("$$kendoScope")
.removeData("$isolateScope")
.removeData("$isolateScopeNoTemplate")
.removeClass("ng-scope");
}
}
var pendingPatches = [];
// defadvice will patch a class' method with another function. That
// function will be called in a context containing `next` (to call
// the next method) and `object` (a reference to the original
// object).
function defadvice(klass, methodName, func) {
if ($.isArray(klass)) {
return angular.forEach(klass, function(klass){
defadvice(klass, methodName, func);
});
}
if (typeof klass == "string") {
var a = klass.split(".");
var x = kendo;
while (x && a.length > 0) {
x = x[a.shift()];
}
if (!x) {
pendingPatches.push([ klass, methodName, func ]);
return false;
}
klass = x.prototype;
}
var origMethod = klass[methodName];
klass[methodName] = function() {
var self = this, args = arguments;
return func.apply({
self: self,
next: function() {
return origMethod.apply(self, arguments.length > 0 ? arguments : args);
}
}, args);
};
return true;
}
kendo.onWidgetRegistered(function(entry){
pendingPatches = $.grep(pendingPatches, function(args){
return !defadvice.apply(null, args);
});
createDirectives(entry.widget, entry.prefix == "Mobile");
});
/* -----[ Customize widgets for Angular ]----- */
defadvice([ "ui.Widget", "mobile.ui.Widget" ], "angular", function(cmd, arg){
var self = this.self;
if (cmd == "init") {
// `arg` here should be the widget options.
// the Chart doesn't send the options to Widget::init in constructor
// hence the OPTIONS_NOW hack (initialized in createWidget).
if (!arg && OPTIONS_NOW) {
arg = OPTIONS_NOW;
}
OPTIONS_NOW = null;
if (arg && arg.$angular) {
self.$angular_scope = arg.$angular[0];
self.$angular_init(self.element, arg);
}
return;
}
var scope = self.$angular_scope;
if (scope) {
withoutTimeout(function(){
var x = arg(), elements = x.elements, data = x.data;
if (elements.length > 0) {
switch (cmd) {
case "cleanup":
angular.forEach(elements, function(el){
var itemScope = $(el).data("$$kendoScope");
if (itemScope && itemScope !== scope && itemScope.$$kendoScope) {
destroyScope(itemScope, el);
}
});
break;
case "compile":
var injector = self.element.injector();
var compile = injector ? injector.get("$compile") : $defaultCompile;
angular.forEach(elements, function(el, i){
var itemScope;
if (x.scopeFrom) {
itemScope = x.scopeFrom;