source: ether_statistics/web/resources/js/Wijmo.2.2.2/Wijmo-Open/development-bundle/external/knockout.wijmo.js @ 622

Last change on this file since 622 was 622, checked in by vmipsl, 12 years ago

wijmo

File size: 28.3 KB
Line 
1/*
2*
3* Wijmo KnockoutJS Binding Library 2.2.0
4* http://wijmo.com/
5*
6* Copyright(c) GrapeCity, Inc.  All rights reserved.
7*
8* Dual licensed under the MIT or GPL Version 2 licenses.
9* licensing@wijmo.com
10* http://wijmo.com/license
11*
12*
13* * Wijmo KnockoutJS Binding Factory.
14*
15* Depends:
16*  knockoutjs.js
17*
18*/
19(function ($, ko) {
20
21    //extend ko.numericObservable
22    ko.numericObservable = function (initialValue) {
23        var _actual = ko.observable(initialValue);
24
25        var result = ko.dependentObservable({
26            read: function () {
27                return _actual();
28            },
29            write: function (newValue) {
30                var parsedValue = parseFloat(newValue);
31                _actual(isNaN(parsedValue) ? newValue : parsedValue);
32            }
33        });
34
35        return result;
36    };
37
38    ko.wijmo = ko.wijmo || {};
39
40    ko.wijmo.customBindingFactory = function () {
41        var self = this;
42
43        self.customBinding = function (options) {
44            var binding = {},
45                widgetName = options.widgetName,
46                widget,
47                vAccessor,
48                updatingFromEvents = false,
49                updatingFromOtherObservables = false;
50
51            binding.init = function (element, valueAccessor, allBindingAccessor, viewModel) {
52                //element: The DOM element involved in this binding
53                //valueAccessor: A JavaScript function that you can call to get the current model property
54                //      that is involved in this binding. Call this without passing any parameters
55                //      (i.e., call valueAccessor()) to get the current model property value.
56                //allBindingsAccessor: A JavaScript function that you can call to get all the model properties
57                //      bound to this DOM element. Like valueAccessor, call it without any parameters to get the
58                //      current bound model properties.
59                //viewModel: The view model object that was passed to ko.applyBindings.
60                //      Inside a nested binding context, this parameter will be set to the current data item
61                //      (e.g., inside a with: person binding, viewModel will be set to person).
62                var va = ko.utils.unwrapObservable(valueAccessor()),
63                    opts;
64                //init widget
65                var opts = ko.toJS(va);
66                widget = $(element)[widgetName](opts).data(widgetName);
67
68                $.each(va, function (key, value) {
69                    if (!options.observableOptions || !options.observableOptions[key]) {
70                        return true;
71                    }
72                    var observableOption = options.observableOptions[key],
73                        optType = observableOption.type;
74                    if (!ko.isObservable(value)) {
75                        return true;
76                    }
77                    //attach event.
78                    var attachEvents = observableOption.attachEvents;
79                    if (attachEvents) {
80                        $.each(attachEvents, function (idx, ev) {
81                            ko.utils.registerEventHandler(element, widgetName + ev.toLowerCase(), function () {
82                                // add vAccessor and update it in update event, because sometimes the reference of
83                                // value accessor  will be updated by customer.
84                                var v = vAccessor[key],
85                                    newVal;
86                                if (updatingFromOtherObservables) {
87                                    return;
88                                }
89                                updatingFromEvents = true;
90                                if ($.isFunction(observableOption.onChange)) {
91                                    if (v) {
92                                        observableOption.onChange.call(observableOption, widget, v, arguments);
93                                    } else {
94                                        observableOption.onChange.call(observableOption, widget, value, arguments);
95                                    }
96                                } else {
97                                    newVal = $(element)[widgetName]("option", key);
98                                    //TODO: If newVal is reference type, we should extend it before assignment
99                                    if (v) {
100                                        v(newVal);
101                                    } else {
102                                        value(newVal);
103                                    }
104                                }
105
106                                updatingFromEvents = false;
107                            });
108                        });
109                    }
110                });
111            };
112
113            binding.update = function (element, valueAccessor, allBindingAccessor, viewModel) {
114                //element: The DOM element involved in this binding
115                //valueAccessor: A JavaScript function that you can call to get the current model property
116                //      that is involved in this binding. Call this without passing any parameters
117                //      (i.e., call valueAccessor()) to get the current model property value.
118                //allBindingsAccessor: A JavaScript function that you can call to get all the model properties
119                //      bound to this DOM element. Like valueAccessor, call it without any parameters to get the
120                //      current bound model properties.
121                //viewModel: The view model object that was passed to ko.applyBindings.
122                //      Inside a nested binding context, this parameter will be set to the current data item
123                //      (e.g., inside a with: person binding, viewModel will be set to person).
124
125                var valueUnwrapped = ko.utils.unwrapObservable(valueAccessor());
126                vAccessor = valueUnwrapped;
127                $.each(valueUnwrapped, function (key, value) {
128                    //The observable can be used like following: style: { width: percentMax() * 100 + '%' },
129                    //the style.width is not an observable value and cannot be observed in ko.computed.
130                    //So we need to check if the value is updated in binding.update.
131                    var observableOption = options.observableOptions[key];
132                    if (observableOption) {
133                        var optType = observableOption.type,
134                           val = ko.toJS(ko.utils.unwrapObservable(value)),
135                           hash = $(element).data(widgetName + '_ko'),
136                           widgetVal = hash && (key in hash)
137                               ? hash[key]
138                               : $(element)[widgetName]("option", key);
139
140                        if (updatingFromEvents) {
141                            return true;
142                        }
143                        if (optType && optType === 'numeric') {
144                            var parsedVal = parseFloat(val);
145                            val = isNaN(parsedVal) ? val : parsedVal;
146                        }
147                        if (!equals(val, widgetVal)) {
148                            updateOptions(element, widgetName, key, val);
149                        }
150                    }
151
152                });
153            };
154
155            executeOptions = function (element, widgetName) {
156                var data = $(element).data(widgetName + '_ko'), hash = (data) ? data : {};
157                if (!$.isEmptyObject(hash)) {
158                    $(element).data(widgetName + '_ko', 0);
159                    for (var key in hash) {
160                        var val = hash[key];
161                        $(element)[widgetName]("option", key, val);
162                    }
163                }
164            };
165
166            updateOptions = function (element, widgetName, key, val) {
167                var data = $(element).data(widgetName + '_ko'), hash = (data) ? data : {};
168                hash[key] = val;
169                $(element).data(widgetName + '_ko', hash);
170                setTimeout(function () {
171                    updatingFromOtherObservables = true;
172                    executeOptions(element, widgetName);
173                    updatingFromOtherObservables = false;
174                }, 100);
175
176            };
177
178            equals = function (sourceValue, targetValue) {
179                var equal = false;
180                if ((sourceValue === undefined) || (sourceValue === null)) {
181                    return false;
182                }
183                if (sourceValue === targetValue) {
184                    return true;
185                }
186                if ((targetValue === undefined) || (targetValue === null) || (sourceValue.constructor !== targetValue.constructor)) {
187                    return false;
188                }
189                if ($.isPlainObject(sourceValue)) {
190                    equal = true;
191                    $.each(sourceValue, function (key, val) {
192                        if (typeof targetValue[key] === 'undefined') {
193                            equal = false;
194                            return false;
195                        }
196                        if (!equals(val, targetValue[key])) {
197                            equal = false;
198                            return false;
199                        }
200                    });
201                } else if ($.isArray(sourceValue)) {
202                    if (sourceValue.length !== targetValue.length) {
203                        return false;
204                    }
205                    equal = true;
206                    $.each(sourceValue, function (idx, val) {
207                        if (!equals(val, targetValue[idx])) {
208                            equal = false;
209                            return false;
210                        }
211                    });
212                } else if (isDate(sourceValue)) {
213                    return sourceValue == targetValue;
214                }
215                return equal;
216            };
217
218            isDate = function (obj) {
219                if (!obj) {
220                    return false;
221                }
222                return (typeof obj === 'object') && obj.constructor === Date;
223            };
224
225            ko.bindingHandlers[options.widgetName] = binding;
226        };
227    };
228
229    ko.wijmo.customBindingFactory = new ko.wijmo.customBindingFactory();
230
231    var createCustomBinding = ko.wijmo.customBindingFactory.customBinding.bind(ko.wijmo.customBindingFactory);
232
233    //Wijmo Bindings
234    createCustomBinding({
235        widgetName: "wijbarchart",
236        observableOptions: {
237            disabled: {},
238            stacked: {},
239            header: {
240            },
241            dataSource: {},
242            seriesList: {
243                type: 'array',
244                attachEvents: ['serieschanged']
245            },
246            seriesStyles: {
247                type: 'array'
248            },
249            seriesHoverStyles: {
250                type: 'array'
251            }
252        }
253    });
254
255    createCustomBinding({
256        widgetName: "wijbubblechart",
257        observableOptions: {
258            disabled: {},
259            dataSource: {},
260            seriesList: {
261                type: 'array',
262                attachEvents: ['serieschanged']
263            },
264            seriesStyles: {
265                type: 'array'
266            },
267            seriesHoverStyles: {
268                type: 'array'
269            }
270        }
271    });
272
273    createCustomBinding({
274        widgetName: "wijcompositechart",
275        observableOptions: {
276            disabled: {},
277            dataSource: {},
278            seriesList: {
279                type: 'array',
280                attachEvents: ['serieschanged']
281            },
282            seriesStyles: {
283                type: 'array'
284            },
285            seriesHoverStyles: {
286                type: 'array'
287            }
288        }
289    });
290
291    createCustomBinding({
292        widgetName: "wijlinechart",
293        observableOptions: {
294            disabled: {},
295            dataSource: {},
296            seriesList: {
297                type: 'array',
298                attachEvents: ['serieschanged']
299            },
300            seriesStyles: {
301                type: 'array'
302            },
303            seriesHoverStyles: {
304                type: 'array'
305            }
306        }
307    });
308
309    createCustomBinding({
310        widgetName: "wijpiechart",
311        observableOptions: {
312            disabled: {},
313            dataSource: {},
314            seriesList: {
315                type: 'array',
316                attachEvents: ['serieschanged']
317            },
318            seriesStyles: {
319                type: 'array'
320            },
321            seriesHoverStyles: {
322                type: 'array'
323            }
324        }
325    });
326
327    createCustomBinding({
328        widgetName: "wijscatterchart",
329        observableOptions: {
330            disabled: {},
331            dataSource: {},
332            seriesList: {
333                type: 'array',
334                attachEvents: ['serieschanged']
335            },
336            seriesStyles: {
337                type: 'array'
338            },
339            seriesHoverStyles: {
340                type: 'array'
341            }
342        }
343    });
344
345    createCustomBinding({
346        widgetName: "wijlineargauge",
347        observableOptions: {
348            disabled: {},
349            min: {
350                type: 'numeric'
351            },
352            max: {
353                type: 'numeric'
354            },
355            value: {
356                type: 'numeric'
357            },
358            face: {},
359            pointer: {},
360            labels: {},
361            disabled: {},
362            ranges: {
363                type: 'array'
364            }
365        }
366    });
367
368    createCustomBinding({
369        widgetName: "wijradialgauge",
370        observableOptions: {
371            disabled: {},
372            face: {},
373            pointer: {},
374            cap: {},
375            labels: {},
376            min: {
377                type: 'numeric'
378            },
379            max: {
380                type: 'numeric'
381            },
382            value: {
383                type: 'numeric'
384            },
385            ranges: {
386                type: 'array'
387            }
388        }
389    });
390
391    createCustomBinding({
392        widgetName: "wijslider",
393        observableOptions: {
394            disabled: {},
395            animate: {},
396            max: {
397                type: 'numeric'
398            },
399            min: {
400                type: 'numeric'
401            },
402            orientation: {},
403            range: {},
404            step: {
405                type: 'numeric'
406            },
407            value: {
408                type: 'numeric',
409                attachEvents: ['change', 'slide']
410            },
411            values: {
412                type: 'array',
413                attachEvents: ['change', 'slide']
414            },
415            dragFill: {},
416            minRange: {
417                type: 'numeric'
418            }
419        }
420    });
421
422    createCustomBinding({
423        widgetName: "wijprogressbar",
424        observableOptions: {
425            disabled: {},
426            value: {
427                type: 'numeric',
428                attachEvents: ['change']
429            },
430            labelAlign: {},
431            maxValue: {
432                type: 'numeric'
433            },
434            minValue: {
435                type: 'numeric'
436            },
437            fillDirection: {},
438            orientation: {},
439            labelFormatString: {},
440            toolTipFormatString: {},
441            indicatorIncrement: {
442                type: 'numeric'
443            },
444            indicatorImage: {},
445            animationDelay: {
446                type: 'numeric'
447            },
448            animationOptions: {}
449        }
450    });
451
452    createCustomBinding({
453        widgetName: "wijrating",
454        observableOptions: {
455            disabled: {},
456            min: {
457                type: 'numeric'
458            },
459            max: {
460                type: 'numeric'
461            },
462            value: {
463                type: 'numeric',
464                attachEvents: ['rated', 'reset']
465            },
466            count: {
467                type: 'numeric'
468            },
469            totalValue: {
470                type: 'numeric'
471            },
472            split: {
473                type: 'numeric'
474            }
475        }
476    });
477
478    createCustomBinding({
479        widgetName: "wijgallery",
480        observableOptions: {
481            disabled: {},
482            autoPlay: {},
483            showTimer: {},
484            interval: {
485                type: 'numeric'
486            },
487            showCaption: {},
488            showCounter: {},
489            showPager: {},
490            thumbnails: {},
491            thumbsDisplay: {
492                type: 'numeric'
493            }
494        }
495    });
496
497    createCustomBinding({
498        widgetName: "wijcarousel",
499        observableOptions: {
500            disabled: {},
501            auto: {},
502            showTimer: {},
503            interval: {
504                type: 'numeric'
505            },
506            loop: {},
507            showPager: {},
508            showCaption: {},
509            display: {
510                type: 'numeric'
511            },
512            preview: {},
513            step: {
514                type: 'numeric'
515            }
516        }
517    });
518
519    createCustomBinding({
520        widgetName: "wijsplitter",
521        observableOptions: {
522            disabled: {},
523            showExpander: {},
524            splitterDistance: {
525                type: 'numeric',
526                attachEvents: ['sized']
527            },
528            fullSplit: {}
529        }
530    });
531
532    createCustomBinding({
533        widgetName: "wijsuperpanel",
534        observableOptions: {
535            disabled: {},
536            allowResize: {},
537            autoRefresh: {},
538            mouseWheelSupport: {},
539            showRounder: {}
540        }
541    });
542
543    createCustomBinding({
544        widgetName: "wijtooltip",
545        observableOptions: {
546            disabled: {},
547            closeBehavior: {},
548            mouseTrailing: {},
549            showCallout: {},
550            showDelay: {
551                type: 'numeric'
552            },
553            hideDelay: {
554                type: 'numeric'
555            },
556            calloutFilled: {},
557            modal: {},
558            triggers: {}
559        }
560    });
561
562    createCustomBinding({
563        widgetName: "wijvideo",
564        observableOptions: {
565            disabled: {},
566            fullScreenButtonVisible: {},
567            showControlsOnHover: {}
568        }
569    });
570
571    createCustomBinding({
572        widgetName: "wijtabs",
573        observableOptions: {
574            disabled: {},
575            collapsible: {}
576        }
577    });
578
579    createCustomBinding({
580        widgetName: "wijexpander",
581        observableOptions: {
582            disabled: {},
583            allowExpand: {},
584            expanded: {
585                attachEvents: ['aftercollapse', 'afterexpand']
586            },
587            expandDirection: {}
588        }
589    });
590
591    createCustomBinding({
592        widgetName: "wijdialog",
593        observableOptions: {
594            disabled: {},
595            autoOpen: {},
596            draggable: {},
597            modal: {},
598            resizable: {}
599        }
600    });
601
602    createCustomBinding({
603        widgetName: "wijcalendar",
604        observableOptions: {
605            disabled: {},
606            showTitle: {},
607            showWeekDays: {},
608            showWeekNumbers: {},
609            showOtherMonthDays: {},
610            showDayPadding: {},
611            allowPreview: {},
612            allowQuciPick: {},
613            popupMode: {},
614            selectedDates: {
615                type: 'array',
616                attachEvents: ['selecteddateschanged'],
617                onChange: function (widgetInstance, viewModelValue, originalEventArgs) {
618                    var dates = originalEventArgs[1].dates;
619                    if (ko.isObservable(viewModelValue)) {
620                        viewModelValue(dates);
621                    } else {
622                        viewModelValue = dates;
623                    }
624                }
625            }
626        }
627    });
628
629    createCustomBinding({
630        widgetName: "wijaccordion",
631        observableOptions: {
632            disabled: {},
633            requireOpenedPane: {},
634            selectedIndex: {
635                attachEvents: ['selectedindexchanged']
636            }
637        }
638    });
639
640    createCustomBinding({
641        widgetName: "wijtree",
642        observableOptions: {
643            disabled: {},
644            allowTriState: {},
645            autoCheckNodes: {},
646            autoCollapse: {},
647            showCheckBoxes: {},
648            showExpandCollapse: {},
649            nodes: {
650                type: "array",
651                attachEvents: ['nodeCheckChanged', 'nodeCollapsed', 'nodeExpanded',
652                               'nodeTextChanged', 'selectedNodeChanged']
653            }
654        }
655    });
656
657    createCustomBinding({
658        widgetName: "wijgrid",
659        observableOptions: {
660            disabled: {},
661            pageIndex: {
662                type: 'numeric'
663            },
664            pageSize: {
665                type: 'numeric'
666            },
667            totalRows: {
668                type: 'numeric'
669            },
670            data: {
671                type: 'array',
672                attachEvents: ['aftercelledit'],
673                onChange: function (widgetInstance, viewModelValue, originalEventArgs) {
674                    var cell = originalEventArgs[1].cell,
675                        rowIndex = cell.row().dataItemIndex,
676                        dataKey = cell.column().dataKey,
677                        newValue = cell.value(),
678                        rowToUpdate = ko.isObservable(viewModelValue)
679                            ? viewModelValue()[rowIndex]
680                            : viewModelValue[rowIndex];
681
682
683                    if ($.isFunction(rowToUpdate[dataKey])) {
684                        rowToUpdate[dataKey](newValue);
685                    } else {
686                        rowToUpdate[dataKey] = newValue;
687                    }
688                }
689            }
690        }
691    });
692
693    createCustomBinding({
694        widgetName: "wijevcal",
695        observableOptions: {
696            disabled: {},
697            eventsData: {
698                type: 'array',
699                attachEvents: ['eventsdatachanged']
700            },
701            appointments: {
702                type: 'array',
703                attachEvents: ['eventsdatachanged']
704            }
705        }
706    });
707
708    createCustomBinding({
709        widgetName: "wijpager",
710        observableOptions: {
711            disabled: {},
712            pageCount: { type: "numeric" },
713            pageIndex: {
714                type: "numeric",
715                attachEvents: ['pageindexchanged']
716            }
717        }
718    });
719
720    createCustomBinding({
721        widgetName: "wijeditor",
722        observableOptions: {
723            disabled: {},
724            editorMode: {},
725            showPathSelector: {},
726            mode: {},
727            showFooter: {},
728            text: {
729                type: 'string',
730                attachEvents: ['textChanged']
731            }
732        }
733    });
734
735    createCustomBinding({
736        widgetName: "wijlist",
737        observableOptions: {
738            disabled: {},
739            listItems: {
740                type: 'array'
741            },
742            selectionMode: {},
743            autoSize: {},
744            maxItemsCount: {
745                type: 'numeric'
746            },
747            addHoverItemClass: {},
748            keepHightlightOnMouseLeave: {}
749        }
750    });
751
752    createCustomBinding({
753        widgetName: "wijcombobox",
754        observableOptions: {
755            disabled: {},
756            data: {
757                type: 'array'
758            },
759            labelText: {},
760            showTrigger: {},
761            triggerPosition: {},
762            autoFilter: {},
763            autoComplete: {},
764            highlightMatching: {},
765            selectionMode: {},
766            isEditable: {}, 
767            selectedIndex: {
768                type: 'numeric',
769                attachEvents: ['changed']
770            },
771            selectedValue: {
772                attachEvents: ['changed']
773            },
774            inputTextInDropDownList: {
775                attachEvents: ['changed']
776            }
777        }
778    });
779
780    createCustomBinding({
781        widgetName: "wijmenu",
782        observableOptions: {
783            disabled: {},
784            trigger: {},
785            triggerEvent: {},
786            mode: {},
787            checkable: {},
788            orientation: {}
789        }
790    });
791
792    createCustomBinding({
793        widgetName: "wijtextbox",
794        observableOptions: {
795            disabled: {}
796        }
797    });
798
799    createCustomBinding({
800        widgetName: "wijdropdown",
801        observableOptions: {
802            disabled: {}
803        }
804    });
805
806    createCustomBinding({
807        widgetName: "wijcheckbox",
808        observableOptions: {
809            disabled: {},
810            checked: {
811                type: 'bool',
812                attachEvents: ['changed']
813            }
814        }
815    });
816
817    createCustomBinding({
818        widgetName: "wijradio",
819        observableOptions: {
820            disabled: {},
821            checked: {
822                type: 'bool',
823                attachEvents: ['changed']
824            }
825        }
826    });
827
828    createCustomBinding({
829        widgetName: "wijribbon",
830        observableOptions: {
831            disabled: {}
832        }
833    });
834
835    createCustomBinding({
836        widgetName: "wijinputdate",
837        observableOptions: {
838            disabled: {},
839            date: {
840                attachEvents: ['dateChanged', 'textChanged']
841            }
842        }
843    });
844
845    createCustomBinding({
846        widgetName: "wijinputmask",
847        observableOptions: {
848            disabled: {},
849            text: {
850                attachEvents: ['textChanged']
851            }
852        }
853    });
854
855    createCustomBinding({
856        widgetName: "wijinputnumber",
857        observableOptions: {
858            disabled: {},
859            maxValue: {
860                type: 'numeric'
861            },
862            minValue: {
863                type: 'numeric'
864            },
865            value: {
866                type: 'numeric',
867                attachEvents: ['valueChanged', 'textChanged']
868            }
869        }
870    });
871
872    //jQuery UI Bindings
873
874    createCustomBinding({
875        widgetName: "accordion",
876        observableOptions: {
877            disabled: {}
878        }
879    });
880
881    createCustomBinding({
882        widgetName: "autocomplete",
883        observableOptions: {
884            disabled: {},
885            source: {}
886        }
887    });
888
889    createCustomBinding({
890        widgetName: "button",
891        observableOptions: {
892            disabled: {},
893            label: {}
894        }
895    });
896
897    createCustomBinding({
898        widgetName: "datepicker",
899        observableOptions: {
900            disabled: {}
901        }
902    });
903
904    createCustomBinding({
905        widgetName: "dialog",
906        observableOptions: {
907            disabled: {},
908            autoOpen: {},
909            draggable: {},
910            modal: {},
911            resizable: {}
912        }
913    });
914
915    createCustomBinding({
916        widgetName: "progressbar",
917        observableOptions: {
918            disabled: {},
919            value: {
920                type: 'numeric'
921            }
922        }
923    });
924
925    createCustomBinding({
926        widgetName: "slider",
927        observableOptions: {
928            disabled: {},
929            value: {
930                type: 'numeric',
931                attachEvents: ['change']
932            },
933            min: {
934                type: 'numeric'
935            },
936            max: {
937                type: 'numeric'
938            },
939            values: {
940                type: 'array',
941                attachEvents: ['change']
942            }
943        }
944    });
945
946    createCustomBinding({
947        widgetName: "tabs",
948        observableOptions: {
949            disabled: {},
950            selected: {
951                type: 'numeric'
952            }
953        }
954    });
955
956} (jQuery, ko));
Note: See TracBrowser for help on using the repository browser.