nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / jqTree / lib / tree.jquery.js
1 var $, BorderDropHint, DragAndDropHandler, DragElement, ElementsRenderer, FolderElement, GhostDropHint, HitAreasGenerator, JqTreeWidget, KeyHandler, MouseWidget, Node, NodeElement, Position, SaveStateHandler, ScrollHandler, SelectNodeHandler, SimpleWidget, __version__, drag_and_drop_handler, isFunction, node_module, ref, util_module,
2   extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
3   hasProp = {}.hasOwnProperty;
4
5 __version__ = require('./version');
6
7 drag_and_drop_handler = require('./drag_and_drop_handler');
8
9 ElementsRenderer = require('./elements_renderer');
10
11 KeyHandler = require('./key_handler');
12
13 MouseWidget = require('./mouse.widget');
14
15 SaveStateHandler = require('./save_state_handler');
16
17 ScrollHandler = require('./scroll_handler');
18
19 SelectNodeHandler = require('./select_node_handler');
20
21 SimpleWidget = require('./simple.widget');
22
23 node_module = require('./node');
24
25 Node = node_module.Node;
26
27 Position = node_module.Position;
28
29 util_module = require('./util');
30
31 isFunction = util_module.isFunction;
32
33 ref = require('./node_element'), BorderDropHint = ref.BorderDropHint, FolderElement = ref.FolderElement, GhostDropHint = ref.GhostDropHint, NodeElement = ref.NodeElement;
34
35 DragAndDropHandler = drag_and_drop_handler.DragAndDropHandler, DragElement = drag_and_drop_handler.DragElement, HitAreasGenerator = drag_and_drop_handler.HitAreasGenerator;
36
37 $ = jQuery;
38
39 JqTreeWidget = (function(superClass) {
40   extend(JqTreeWidget, superClass);
41
42   function JqTreeWidget() {
43     return JqTreeWidget.__super__.constructor.apply(this, arguments);
44   }
45
46   JqTreeWidget.prototype.BorderDropHint = BorderDropHint;
47
48   JqTreeWidget.prototype.DragElement = DragElement;
49
50   JqTreeWidget.prototype.DragAndDropHandler = DragAndDropHandler;
51
52   JqTreeWidget.prototype.ElementsRenderer = ElementsRenderer;
53
54   JqTreeWidget.prototype.GhostDropHint = GhostDropHint;
55
56   JqTreeWidget.prototype.HitAreasGenerator = HitAreasGenerator;
57
58   JqTreeWidget.prototype.Node = Node;
59
60   JqTreeWidget.prototype.SaveStateHandler = SaveStateHandler;
61
62   JqTreeWidget.prototype.ScrollHandler = ScrollHandler;
63
64   JqTreeWidget.prototype.SelectNodeHandler = SelectNodeHandler;
65
66   JqTreeWidget.prototype.defaults = {
67     autoOpen: false,
68     saveState: false,
69     dragAndDrop: false,
70     selectable: true,
71     useContextMenu: true,
72     onCanSelectNode: null,
73     onSetStateFromStorage: null,
74     onGetStateFromStorage: null,
75     onCreateLi: null,
76     onIsMoveHandle: null,
77     onCanMove: null,
78     onCanMoveTo: null,
79     onLoadFailed: null,
80     autoEscape: true,
81     dataUrl: null,
82     closedIcon: null,
83     openedIcon: '▼',
84     slide: true,
85     nodeClass: Node,
86     dataFilter: null,
87     keyboardSupport: true,
88     openFolderDelay: 500,
89     rtl: null,
90     onDragMove: null,
91     onDragStop: null,
92     buttonLeft: true,
93     onLoading: null
94   };
95
96   JqTreeWidget.prototype.toggle = function(node, slide) {
97     if (slide == null) {
98       slide = null;
99     }
100     if (slide === null) {
101       slide = this.options.slide;
102     }
103     if (node.is_open) {
104       this.closeNode(node, slide);
105     } else {
106       this.openNode(node, slide);
107     }
108     return this.element;
109   };
110
111   JqTreeWidget.prototype.getTree = function() {
112     return this.tree;
113   };
114
115   JqTreeWidget.prototype.selectNode = function(node) {
116     this._selectNode(node, false);
117     return this.element;
118   };
119
120   JqTreeWidget.prototype._selectNode = function(node, must_toggle) {
121     var canSelect, deselected_node, openParents, saveState;
122     if (must_toggle == null) {
123       must_toggle = false;
124     }
125     if (!this.select_node_handler) {
126       return;
127     }
128     canSelect = (function(_this) {
129       return function() {
130         if (_this.options.onCanSelectNode) {
131           return _this.options.selectable && _this.options.onCanSelectNode(node);
132         } else {
133           return _this.options.selectable;
134         }
135       };
136     })(this);
137     openParents = (function(_this) {
138       return function() {
139         var parent;
140         parent = node.parent;
141         if (parent && parent.parent && !parent.is_open) {
142           return _this.openNode(parent, false);
143         }
144       };
145     })(this);
146     saveState = (function(_this) {
147       return function() {
148         if (_this.options.saveState) {
149           return _this.save_state_handler.saveState();
150         }
151       };
152     })(this);
153     if (!node) {
154       this._deselectCurrentNode();
155       saveState();
156       return;
157     }
158     if (!canSelect()) {
159       return;
160     }
161     if (this.select_node_handler.isNodeSelected(node)) {
162       if (must_toggle) {
163         this._deselectCurrentNode();
164         this._triggerEvent('tree.select', {
165           node: null,
166           previous_node: node
167         });
168       }
169     } else {
170       deselected_node = this.getSelectedNode();
171       this._deselectCurrentNode();
172       this.addToSelection(node);
173       this._triggerEvent('tree.select', {
174         node: node,
175         deselected_node: deselected_node
176       });
177       openParents();
178     }
179     return saveState();
180   };
181
182   JqTreeWidget.prototype.getSelectedNode = function() {
183     if (this.select_node_handler) {
184       return this.select_node_handler.getSelectedNode();
185     } else {
186       return null;
187     }
188   };
189
190   JqTreeWidget.prototype.toJson = function() {
191     return JSON.stringify(this.tree.getData());
192   };
193
194   JqTreeWidget.prototype.loadData = function(data, parent_node) {
195     this._loadData(data, parent_node);
196     return this.element;
197   };
198
199
200   /*
201   signatures:
202   - loadDataFromUrl(url, parent_node=null, on_finished=null)
203       loadDataFromUrl('/my_data');
204       loadDataFromUrl('/my_data', node1);
205       loadDataFromUrl('/my_data', node1, function() { console.log('finished'); });
206       loadDataFromUrl('/my_data', null, function() { console.log('finished'); });
207   
208   - loadDataFromUrl(parent_node=null, on_finished=null)
209       loadDataFromUrl();
210       loadDataFromUrl(node1);
211       loadDataFromUrl(null, function() { console.log('finished'); });
212       loadDataFromUrl(node1, function() { console.log('finished'); });
213    */
214
215   JqTreeWidget.prototype.loadDataFromUrl = function(param1, param2, param3) {
216     if ($.type(param1) === 'string') {
217       this._loadDataFromUrl(param1, param2, param3);
218     } else {
219       this._loadDataFromUrl(null, param1, param2);
220     }
221     return this.element;
222   };
223
224   JqTreeWidget.prototype.reload = function(on_finished) {
225     this._loadDataFromUrl(null, null, on_finished);
226     return this.element;
227   };
228
229   JqTreeWidget.prototype._loadDataFromUrl = function(url_info, parent_node, on_finished) {
230     var $el, addLoadingClass, handeLoadData, handleError, handleSuccess, loadDataFromUrlInfo, parseUrlInfo, removeLoadingClass;
231     $el = null;
232     addLoadingClass = (function(_this) {
233       return function() {
234         if (parent_node) {
235           $el = $(parent_node.element);
236         } else {
237           $el = _this.element;
238         }
239         $el.addClass('jqtree-loading');
240         return _this._notifyLoading(true, parent_node, $el);
241       };
242     })(this);
243     removeLoadingClass = (function(_this) {
244       return function() {
245         if ($el) {
246           $el.removeClass('jqtree-loading');
247           return _this._notifyLoading(false, parent_node, $el);
248         }
249       };
250     })(this);
251     parseUrlInfo = function() {
252       if ($.type(url_info) === 'string') {
253         return {
254           url: url_info
255         };
256       }
257       if (!url_info.method) {
258         url_info.method = 'get';
259       }
260       return url_info;
261     };
262     handeLoadData = (function(_this) {
263       return function(data) {
264         removeLoadingClass();
265         _this._loadData(data, parent_node);
266         if (on_finished && $.isFunction(on_finished)) {
267           return on_finished();
268         }
269       };
270     })(this);
271     handleSuccess = (function(_this) {
272       return function(response) {
273         var data;
274         if ($.isArray(response) || typeof response === 'object') {
275           data = response;
276         } else if (data != null) {
277           data = $.parseJSON(response);
278         } else {
279           data = [];
280         }
281         if (_this.options.dataFilter) {
282           data = _this.options.dataFilter(data);
283         }
284         return handeLoadData(data);
285       };
286     })(this);
287     handleError = (function(_this) {
288       return function(response) {
289         removeLoadingClass();
290         if (_this.options.onLoadFailed) {
291           return _this.options.onLoadFailed(response);
292         }
293       };
294     })(this);
295     loadDataFromUrlInfo = function() {
296       url_info = parseUrlInfo();
297       return $.ajax($.extend({}, url_info, {
298         method: url_info.method != null ? url_info.method.toUpperCase() : 'GET',
299         cache: false,
300         dataType: 'json',
301         success: handleSuccess,
302         error: handleError
303       }));
304     };
305     if (!url_info) {
306       url_info = this._getDataUrlInfo(parent_node);
307     }
308     addLoadingClass();
309     if (!url_info) {
310       removeLoadingClass();
311     } else if ($.isArray(url_info)) {
312       handeLoadData(url_info);
313     } else {
314       loadDataFromUrlInfo();
315     }
316   };
317
318   JqTreeWidget.prototype._loadData = function(data, parent_node) {
319     var deselectNodes, loadSubtree;
320     if (parent_node == null) {
321       parent_node = null;
322     }
323     deselectNodes = (function(_this) {
324       return function() {
325         var i, len, n, selected_nodes_under_parent;
326         if (_this.select_node_handler) {
327           selected_nodes_under_parent = _this.select_node_handler.getSelectedNodesUnder(parent_node);
328           for (i = 0, len = selected_nodes_under_parent.length; i < len; i++) {
329             n = selected_nodes_under_parent[i];
330             _this.select_node_handler.removeFromSelection(n);
331           }
332         }
333         return null;
334       };
335     })(this);
336     loadSubtree = (function(_this) {
337       return function() {
338         parent_node.loadFromData(data);
339         parent_node.load_on_demand = false;
340         parent_node.is_loading = false;
341         return _this._refreshElements(parent_node);
342       };
343     })(this);
344     if (!data) {
345       return;
346     }
347     this._triggerEvent('tree.load_data', {
348       tree_data: data
349     });
350     if (!parent_node) {
351       this._initTree(data);
352     } else {
353       deselectNodes();
354       loadSubtree();
355     }
356     if (this.isDragging()) {
357       return this.dnd_handler.refresh();
358     }
359   };
360
361   JqTreeWidget.prototype.getNodeById = function(node_id) {
362     return this.tree.getNodeById(node_id);
363   };
364
365   JqTreeWidget.prototype.getNodeByName = function(name) {
366     return this.tree.getNodeByName(name);
367   };
368
369   JqTreeWidget.prototype.getNodesByProperty = function(key, value) {
370     return this.tree.getNodesByProperty(key, value);
371   };
372
373   JqTreeWidget.prototype.getNodeByHtmlElement = function(element) {
374     return this._getNode($(element));
375   };
376
377   JqTreeWidget.prototype.getNodeByCallback = function(callback) {
378     return this.tree.getNodeByCallback(callback);
379   };
380
381   JqTreeWidget.prototype.openNode = function(node, slide_param, on_finished_param) {
382     var on_finished, parseParams, ref1, slide;
383     if (slide_param == null) {
384       slide_param = null;
385     }
386     if (on_finished_param == null) {
387       on_finished_param = null;
388     }
389     parseParams = (function(_this) {
390       return function() {
391         var on_finished, slide;
392         if (isFunction(slide_param)) {
393           on_finished = slide_param;
394           slide = null;
395         } else {
396           slide = slide_param;
397           on_finished = on_finished_param;
398         }
399         if (slide === null) {
400           slide = _this.options.slide;
401         }
402         return [slide, on_finished];
403       };
404     })(this);
405     ref1 = parseParams(), slide = ref1[0], on_finished = ref1[1];
406     if (node) {
407       this._openNode(node, slide, on_finished);
408     }
409     return this.element;
410   };
411
412   JqTreeWidget.prototype._openNode = function(node, slide, on_finished) {
413     var doOpenNode, parent;
414     if (slide == null) {
415       slide = true;
416     }
417     doOpenNode = (function(_this) {
418       return function(_node, _slide, _on_finished) {
419         var folder_element;
420         folder_element = new FolderElement(_node, _this);
421         return folder_element.open(_on_finished, _slide);
422       };
423     })(this);
424     if (node.isFolder()) {
425       if (node.load_on_demand) {
426         return this._loadFolderOnDemand(node, slide, on_finished);
427       } else {
428         parent = node.parent;
429         while (parent) {
430           if (parent.parent) {
431             doOpenNode(parent, false, null);
432           }
433           parent = parent.parent;
434         }
435         doOpenNode(node, slide, on_finished);
436         return this._saveState();
437       }
438     }
439   };
440
441   JqTreeWidget.prototype._loadFolderOnDemand = function(node, slide, on_finished) {
442     if (slide == null) {
443       slide = true;
444     }
445     node.is_loading = true;
446     return this._loadDataFromUrl(null, node, (function(_this) {
447       return function() {
448         return _this._openNode(node, slide, on_finished);
449       };
450     })(this));
451   };
452
453   JqTreeWidget.prototype.closeNode = function(node, slide) {
454     if (slide == null) {
455       slide = null;
456     }
457     if (slide === null) {
458       slide = this.options.slide;
459     }
460     if (node.isFolder()) {
461       new FolderElement(node, this).close(slide);
462       this._saveState();
463     }
464     return this.element;
465   };
466
467   JqTreeWidget.prototype.isDragging = function() {
468     if (this.dnd_handler) {
469       return this.dnd_handler.is_dragging;
470     } else {
471       return false;
472     }
473   };
474
475   JqTreeWidget.prototype.refreshHitAreas = function() {
476     this.dnd_handler.refresh();
477     return this.element;
478   };
479
480   JqTreeWidget.prototype.addNodeAfter = function(new_node_info, existing_node) {
481     var new_node;
482     new_node = existing_node.addAfter(new_node_info);
483     this._refreshElements(existing_node.parent);
484     return new_node;
485   };
486
487   JqTreeWidget.prototype.addNodeBefore = function(new_node_info, existing_node) {
488     var new_node;
489     new_node = existing_node.addBefore(new_node_info);
490     this._refreshElements(existing_node.parent);
491     return new_node;
492   };
493
494   JqTreeWidget.prototype.addParentNode = function(new_node_info, existing_node) {
495     var new_node;
496     new_node = existing_node.addParent(new_node_info);
497     this._refreshElements(new_node.parent);
498     return new_node;
499   };
500
501   JqTreeWidget.prototype.removeNode = function(node) {
502     var parent;
503     parent = node.parent;
504     if (parent) {
505       this.select_node_handler.removeFromSelection(node, true);
506       node.remove();
507       this._refreshElements(parent);
508     }
509     return this.element;
510   };
511
512   JqTreeWidget.prototype.appendNode = function(new_node_info, parent_node) {
513     var node;
514     parent_node = parent_node || this.tree;
515     node = parent_node.append(new_node_info);
516     this._refreshElements(parent_node);
517     return node;
518   };
519
520   JqTreeWidget.prototype.prependNode = function(new_node_info, parent_node) {
521     var node;
522     if (!parent_node) {
523       parent_node = this.tree;
524     }
525     node = parent_node.prepend(new_node_info);
526     this._refreshElements(parent_node);
527     return node;
528   };
529
530   JqTreeWidget.prototype.updateNode = function(node, data) {
531     var id_is_changed;
532     id_is_changed = data.id && data.id !== node.id;
533     if (id_is_changed) {
534       this.tree.removeNodeFromIndex(node);
535     }
536     node.setData(data);
537     if (id_is_changed) {
538       this.tree.addNodeToIndex(node);
539     }
540     if (typeof data === 'object' && data.children) {
541       node.removeChildren();
542       if (data.children.length) {
543         node.loadFromData(data.children);
544       }
545     }
546     this.renderer.renderFromNode(node);
547     this._selectCurrentNode();
548     return this.element;
549   };
550
551   JqTreeWidget.prototype.moveNode = function(node, target_node, position) {
552     var position_index;
553     position_index = Position.nameToIndex(position);
554     this.tree.moveNode(node, target_node, position_index);
555     this._refreshElements();
556     return this.element;
557   };
558
559   JqTreeWidget.prototype.getStateFromStorage = function() {
560     return this.save_state_handler.getStateFromStorage();
561   };
562
563   JqTreeWidget.prototype.addToSelection = function(node) {
564     if (node) {
565       this.select_node_handler.addToSelection(node);
566       this._getNodeElementForNode(node).select();
567       this._saveState();
568     }
569     return this.element;
570   };
571
572   JqTreeWidget.prototype.getSelectedNodes = function() {
573     return this.select_node_handler.getSelectedNodes();
574   };
575
576   JqTreeWidget.prototype.isNodeSelected = function(node) {
577     return this.select_node_handler.isNodeSelected(node);
578   };
579
580   JqTreeWidget.prototype.removeFromSelection = function(node) {
581     this.select_node_handler.removeFromSelection(node);
582     this._getNodeElementForNode(node).deselect();
583     this._saveState();
584     return this.element;
585   };
586
587   JqTreeWidget.prototype.scrollToNode = function(node) {
588     var $element, top;
589     $element = $(node.element);
590     top = $element.offset().top - this.$el.offset().top;
591     this.scroll_handler.scrollTo(top);
592     return this.element;
593   };
594
595   JqTreeWidget.prototype.getState = function() {
596     return this.save_state_handler.getState();
597   };
598
599   JqTreeWidget.prototype.setState = function(state) {
600     this.save_state_handler.setInitialState(state);
601     this._refreshElements();
602     return this.element;
603   };
604
605   JqTreeWidget.prototype.setOption = function(option, value) {
606     this.options[option] = value;
607     return this.element;
608   };
609
610   JqTreeWidget.prototype.moveDown = function() {
611     if (this.key_handler) {
612       this.key_handler.moveDown();
613     }
614     return this.element;
615   };
616
617   JqTreeWidget.prototype.moveUp = function() {
618     if (this.key_handler) {
619       this.key_handler.moveUp();
620     }
621     return this.element;
622   };
623
624   JqTreeWidget.prototype.getVersion = function() {
625     return __version__;
626   };
627
628   JqTreeWidget.prototype._init = function() {
629     JqTreeWidget.__super__._init.call(this);
630     this.element = this.$el;
631     this.mouse_delay = 300;
632     this.is_initialized = false;
633     this.options.rtl = this._getRtlOption();
634     if (!this.options.closedIcon) {
635       this.options.closedIcon = this._getDefaultClosedIcon();
636     }
637     this.renderer = new ElementsRenderer(this);
638     if (SaveStateHandler != null) {
639       this.save_state_handler = new SaveStateHandler(this);
640     } else {
641       this.options.saveState = false;
642     }
643     if (SelectNodeHandler != null) {
644       this.select_node_handler = new SelectNodeHandler(this);
645     }
646     if (DragAndDropHandler != null) {
647       this.dnd_handler = new DragAndDropHandler(this);
648     } else {
649       this.options.dragAndDrop = false;
650     }
651     if (ScrollHandler != null) {
652       this.scroll_handler = new ScrollHandler(this);
653     }
654     if ((KeyHandler != null) && (SelectNodeHandler != null)) {
655       this.key_handler = new KeyHandler(this);
656     }
657     this._initData();
658     this.element.click($.proxy(this._click, this));
659     this.element.dblclick($.proxy(this._dblclick, this));
660     if (this.options.useContextMenu) {
661       return this.element.bind('contextmenu', $.proxy(this._contextmenu, this));
662     }
663   };
664
665   JqTreeWidget.prototype._deinit = function() {
666     this.element.empty();
667     this.element.unbind();
668     if (this.key_handler) {
669       this.key_handler.deinit();
670     }
671     this.tree = null;
672     return JqTreeWidget.__super__._deinit.call(this);
673   };
674
675   JqTreeWidget.prototype._initData = function() {
676     var data_url;
677     if (this.options.data) {
678       return this._loadData(this.options.data);
679     } else {
680       data_url = this._getDataUrlInfo();
681       if (data_url) {
682         return this._loadDataFromUrl();
683       } else {
684         return this._loadData([]);
685       }
686     }
687   };
688
689   JqTreeWidget.prototype._getDataUrlInfo = function(node) {
690     var data_url, getUrlFromString;
691     data_url = this.options.dataUrl || this.element.data('url');
692     getUrlFromString = (function(_this) {
693       return function() {
694         var data, selected_node_id, url_info;
695         url_info = {
696           url: data_url
697         };
698         if (node && node.id) {
699           data = {
700             node: node.id
701           };
702           url_info['data'] = data;
703         } else {
704           selected_node_id = _this._getNodeIdToBeSelected();
705           if (selected_node_id) {
706             data = {
707               selected_node: selected_node_id
708             };
709             url_info['data'] = data;
710           }
711         }
712         return url_info;
713       };
714     })(this);
715     if ($.isFunction(data_url)) {
716       return data_url(node);
717     } else if ($.type(data_url) === 'string') {
718       return getUrlFromString();
719     } else {
720       return data_url;
721     }
722   };
723
724   JqTreeWidget.prototype._getNodeIdToBeSelected = function() {
725     if (this.options.saveState) {
726       return this.save_state_handler.getNodeIdToBeSelected();
727     } else {
728       return null;
729     }
730   };
731
732   JqTreeWidget.prototype._initTree = function(data) {
733     var doInit, must_load_on_demand;
734     doInit = (function(_this) {
735       return function() {
736         if (!_this.is_initialized) {
737           _this.is_initialized = true;
738           return _this._triggerEvent('tree.init');
739         }
740       };
741     })(this);
742     this.tree = new this.options.nodeClass(null, true, this.options.nodeClass);
743     if (this.select_node_handler) {
744       this.select_node_handler.clear();
745     }
746     this.tree.loadFromData(data);
747     must_load_on_demand = this._setInitialState();
748     this._refreshElements();
749     if (!must_load_on_demand) {
750       return doInit();
751     } else {
752       return this._setInitialStateOnDemand(doInit);
753     }
754   };
755
756   JqTreeWidget.prototype._setInitialState = function() {
757     var autoOpenNodes, is_restored, must_load_on_demand, ref1, restoreState;
758     restoreState = (function(_this) {
759       return function() {
760         var must_load_on_demand, state;
761         if (!(_this.options.saveState && _this.save_state_handler)) {
762           return [false, false];
763         } else {
764           state = _this.save_state_handler.getStateFromStorage();
765           if (!state) {
766             return [false, false];
767           } else {
768             must_load_on_demand = _this.save_state_handler.setInitialState(state);
769             return [true, must_load_on_demand];
770           }
771         }
772       };
773     })(this);
774     autoOpenNodes = (function(_this) {
775       return function() {
776         var max_level, must_load_on_demand;
777         if (_this.options.autoOpen === false) {
778           return false;
779         }
780         max_level = _this._getAutoOpenMaxLevel();
781         must_load_on_demand = false;
782         _this.tree.iterate(function(node, level) {
783           if (node.load_on_demand) {
784             must_load_on_demand = true;
785             return false;
786           } else if (!node.hasChildren()) {
787             return false;
788           } else {
789             node.is_open = true;
790             return level !== max_level;
791           }
792         });
793         return must_load_on_demand;
794       };
795     })(this);
796     ref1 = restoreState(), is_restored = ref1[0], must_load_on_demand = ref1[1];
797     if (!is_restored) {
798       must_load_on_demand = autoOpenNodes();
799     }
800     return must_load_on_demand;
801   };
802
803   JqTreeWidget.prototype._setInitialStateOnDemand = function(cb_finished) {
804     var autoOpenNodes, restoreState;
805     restoreState = (function(_this) {
806       return function() {
807         var state;
808         if (!(_this.options.saveState && _this.save_state_handler)) {
809           return false;
810         } else {
811           state = _this.save_state_handler.getStateFromStorage();
812           if (!state) {
813             return false;
814           } else {
815             _this.save_state_handler.setInitialStateOnDemand(state, cb_finished);
816             return true;
817           }
818         }
819       };
820     })(this);
821     autoOpenNodes = (function(_this) {
822       return function() {
823         var loadAndOpenNode, loading_count, max_level, openNodes;
824         max_level = _this._getAutoOpenMaxLevel();
825         loading_count = 0;
826         loadAndOpenNode = function(node) {
827           loading_count += 1;
828           return _this._openNode(node, false, function() {
829             loading_count -= 1;
830             return openNodes();
831           });
832         };
833         openNodes = function() {
834           _this.tree.iterate(function(node, level) {
835             if (node.load_on_demand) {
836               if (!node.is_loading) {
837                 loadAndOpenNode(node);
838               }
839               return false;
840             } else {
841               _this._openNode(node, false);
842               return level !== max_level;
843             }
844           });
845           if (loading_count === 0) {
846             return cb_finished();
847           }
848         };
849         return openNodes();
850       };
851     })(this);
852     if (!restoreState()) {
853       return autoOpenNodes();
854     }
855   };
856
857   JqTreeWidget.prototype._getAutoOpenMaxLevel = function() {
858     if (this.options.autoOpen === true) {
859       return -1;
860     } else {
861       return parseInt(this.options.autoOpen);
862     }
863   };
864
865
866   /*
867   Redraw the tree or part of the tree.
868    * from_node: redraw this subtree
869    */
870
871   JqTreeWidget.prototype._refreshElements = function(from_node) {
872     if (from_node == null) {
873       from_node = null;
874     }
875     this.renderer.render(from_node);
876     return this._triggerEvent('tree.refresh');
877   };
878
879   JqTreeWidget.prototype._click = function(e) {
880     var click_target, event, node;
881     click_target = this._getClickTarget(e.target);
882     if (click_target) {
883       if (click_target.type === 'button') {
884         this.toggle(click_target.node, this.options.slide);
885         e.preventDefault();
886         return e.stopPropagation();
887       } else if (click_target.type === 'label') {
888         node = click_target.node;
889         event = this._triggerEvent('tree.click', {
890           node: node,
891           click_event: e
892         });
893         if (!event.isDefaultPrevented()) {
894           return this._selectNode(node, true);
895         }
896       }
897     }
898   };
899
900   JqTreeWidget.prototype._dblclick = function(e) {
901     var click_target;
902     click_target = this._getClickTarget(e.target);
903     if (click_target && click_target.type === 'label') {
904       return this._triggerEvent('tree.dblclick', {
905         node: click_target.node,
906         click_event: e
907       });
908     }
909   };
910
911   JqTreeWidget.prototype._getClickTarget = function(element) {
912     var $button, $el, $target, node;
913     $target = $(element);
914     $button = $target.closest('.jqtree-toggler');
915     if ($button.length) {
916       node = this._getNode($button);
917       if (node) {
918         return {
919           type: 'button',
920           node: node
921         };
922       }
923     } else {
924       $el = $target.closest('.jqtree-element');
925       if ($el.length) {
926         node = this._getNode($el);
927         if (node) {
928           return {
929             type: 'label',
930             node: node
931           };
932         }
933       }
934     }
935     return null;
936   };
937
938   JqTreeWidget.prototype._getNode = function($element) {
939     var $li;
940     $li = $element.closest('li.jqtree_common');
941     if ($li.length === 0) {
942       return null;
943     } else {
944       return $li.data('node');
945     }
946   };
947
948   JqTreeWidget.prototype._getNodeElementForNode = function(node) {
949     if (node.isFolder()) {
950       return new FolderElement(node, this);
951     } else {
952       return new NodeElement(node, this);
953     }
954   };
955
956   JqTreeWidget.prototype._getNodeElement = function($element) {
957     var node;
958     node = this._getNode($element);
959     if (node) {
960       return this._getNodeElementForNode(node);
961     } else {
962       return null;
963     }
964   };
965
966   JqTreeWidget.prototype._contextmenu = function(e) {
967     var $div, node;
968     $div = $(e.target).closest('ul.jqtree-tree .jqtree-element');
969     if ($div.length) {
970       node = this._getNode($div);
971       if (node) {
972         e.preventDefault();
973         e.stopPropagation();
974         this._triggerEvent('tree.contextmenu', {
975           node: node,
976           click_event: e
977         });
978         return false;
979       }
980     }
981   };
982
983   JqTreeWidget.prototype._saveState = function() {
984     if (this.options.saveState) {
985       return this.save_state_handler.saveState();
986     }
987   };
988
989   JqTreeWidget.prototype._mouseCapture = function(position_info) {
990     if (this.options.dragAndDrop) {
991       return this.dnd_handler.mouseCapture(position_info);
992     } else {
993       return false;
994     }
995   };
996
997   JqTreeWidget.prototype._mouseStart = function(position_info) {
998     if (this.options.dragAndDrop) {
999       return this.dnd_handler.mouseStart(position_info);
1000     } else {
1001       return false;
1002     }
1003   };
1004
1005   JqTreeWidget.prototype._mouseDrag = function(position_info) {
1006     var result;
1007     if (this.options.dragAndDrop) {
1008       result = this.dnd_handler.mouseDrag(position_info);
1009       if (this.scroll_handler) {
1010         this.scroll_handler.checkScrolling();
1011       }
1012       return result;
1013     } else {
1014       return false;
1015     }
1016   };
1017
1018   JqTreeWidget.prototype._mouseStop = function(position_info) {
1019     if (this.options.dragAndDrop) {
1020       return this.dnd_handler.mouseStop(position_info);
1021     } else {
1022       return false;
1023     }
1024   };
1025
1026   JqTreeWidget.prototype._triggerEvent = function(event_name, values) {
1027     var event;
1028     event = $.Event(event_name);
1029     $.extend(event, values);
1030     this.element.trigger(event);
1031     return event;
1032   };
1033
1034   JqTreeWidget.prototype.testGenerateHitAreas = function(moving_node) {
1035     this.dnd_handler.current_item = this._getNodeElementForNode(moving_node);
1036     this.dnd_handler.generateHitAreas();
1037     return this.dnd_handler.hit_areas;
1038   };
1039
1040   JqTreeWidget.prototype._selectCurrentNode = function() {
1041     var node, node_element;
1042     node = this.getSelectedNode();
1043     if (node) {
1044       node_element = this._getNodeElementForNode(node);
1045       if (node_element) {
1046         return node_element.select();
1047       }
1048     }
1049   };
1050
1051   JqTreeWidget.prototype._deselectCurrentNode = function() {
1052     var node;
1053     node = this.getSelectedNode();
1054     if (node) {
1055       return this.removeFromSelection(node);
1056     }
1057   };
1058
1059   JqTreeWidget.prototype._getDefaultClosedIcon = function() {
1060     if (this.options.rtl) {
1061       return '&#x25c0;';
1062     } else {
1063       return '&#x25ba;';
1064     }
1065   };
1066
1067   JqTreeWidget.prototype._getRtlOption = function() {
1068     var data_rtl;
1069     if (this.options.rtl !== null) {
1070       return this.options.rtl;
1071     } else {
1072       data_rtl = this.element.data('rtl');
1073       if ((data_rtl != null) && data_rtl !== false) {
1074         return true;
1075       } else {
1076         return false;
1077       }
1078     }
1079   };
1080
1081   JqTreeWidget.prototype._notifyLoading = function(is_loading, node, $el) {
1082     if (this.options.onLoading) {
1083       return this.options.onLoading(is_loading, node, $el);
1084     }
1085   };
1086
1087   return JqTreeWidget;
1088
1089 })(MouseWidget);
1090
1091 JqTreeWidget.getModule = function(name) {
1092   var modules;
1093   modules = {
1094     'node': node_module,
1095     'util': util_module,
1096     'drag_and_drop_handler': drag_and_drop_handler
1097   };
1098   return modules[name];
1099 };
1100
1101 SimpleWidget.register(JqTreeWidget, 'tree');