source: pro-violet-viettel/docs/Space/assets/js/uncompressed/fuelux/fuelux.tree.js @ 290

Last change on this file since 290 was 290, checked in by lamdt, 11 years ago
File size: 5.1 KB
Line 
1/*
2 * Fuel UX Tree
3 * https://github.com/ExactTarget/fuelux
4 *
5 * Copyright (c) 2012 ExactTarget
6 * Licensed under the MIT license.
7 */
8
9(function($ , undefined) {
10
11        //var $ = require('jquery');
12
13
14        // TREE CONSTRUCTOR AND PROTOTYPE
15
16        var Tree = function (element, options) {
17                this.$element = $(element);
18                this.options = $.extend({}, $.fn.tree.defaults, options);
19
20                this.$element.on('click', '.tree-item', $.proxy( function(ev) { this.selectItem(ev.currentTarget); } ,this));
21                this.$element.on('click', '.tree-folder-header', $.proxy( function(ev) { this.selectFolder(ev.currentTarget); }, this));
22
23                this.render();
24        };
25
26        Tree.prototype = {
27                constructor: Tree,
28
29                render: function () {
30                        this.populate(this.$element);
31                },
32
33                populate: function ($el) {
34                        var self = this;
35                        var loader = $el.parent().find('.tree-loader:eq(0)');
36
37                        loader.show();
38                        this.options.dataSource.data($el.data(), function (items) {
39                                loader.hide();
40
41                                $.each( items.data, function(index, value) {
42                                        var $entity;
43
44                                        if(value.type === "folder") {
45                                                $entity = self.$element.find('.tree-folder:eq(0)').clone().show();
46                                                $entity.find('.tree-folder-name').html(value.name);
47                                                $entity.find('.tree-loader').html(self.options.loadingHTML);
48                                                var header = $entity.find('.tree-folder-header');
49                                                header.data(value);
50                                                if('icon-class' in value)
51                                                        header.find('[class*="icon-"]').addClass(value['icon-class']);
52                                        } else if (value.type === "item") {
53                                                $entity = self.$element.find('.tree-item:eq(0)').clone().show();
54                                                $entity.find('.tree-item-name').html(value.name);
55                                                $entity.data(value);
56
57                                                if('additionalParameters' in value
58                                                        && 'item-selected' in value.additionalParameters
59                                                                && value.additionalParameters['item-selected'] == true) {
60                                                                $entity.addClass ('tree-selected');
61                                                                $entity.find('i').removeClass(self.options['unselected-icon']).addClass(self.options['selected-icon']);
62                                                                //$entity.closest('.tree-folder-content').show();
63                                                }
64                                        }
65
66                                        if($el.hasClass('tree-folder-header')) {
67                                                $el.parent().find('.tree-folder-content:eq(0)').append($entity);
68                                        } else {
69                                                $el.append($entity);
70                                        }
71                                });
72
73                                self.$element.trigger('loaded');
74                        });
75                },
76
77                selectItem: function (el) {
78                        if(this.options['selectable'] == false) return;
79                        var $el = $(el);
80                        var $all = this.$element.find('.tree-selected');
81                        var data = [];
82
83                        if (this.options.multiSelect) {
84                                $.each($all, function(index, value) {
85                                        var $val = $(value);
86                                        if($val[0] !== $el[0]) {
87                                                data.push( $(value).data() );
88                                        }
89                                });
90                        } else if ($all[0] !== $el[0]) {
91                                $all.removeClass('tree-selected')
92                                        .find('i').removeClass(this.options['selected-icon']).addClass(this.options['unselected-icon']);
93                                data.push($el.data());
94                        }
95
96                        if($el.hasClass('tree-selected')) {
97                                $el.removeClass('tree-selected');
98                                $el.find('i').removeClass(this.options['selected-icon']).addClass(this.options['unselected-icon']);
99                        } else {
100                                $el.addClass ('tree-selected');
101                                $el.find('i').removeClass(this.options['unselected-icon']).addClass(this.options['selected-icon']);
102                                if (this.options.multiSelect) {
103                                        data.push( $el.data() );
104                                }
105                        }
106
107                        if(data.length) {
108                                this.$element.trigger('selected', {info: data});
109                        }
110
111                },
112
113                selectFolder: function (el) {
114                        var $el = $(el);
115                        var $par = $el.parent();
116
117                        if($el.find('.'+this.options['close-icon']).length) {
118                                if ($par.find('.tree-folder-content').children().length) {
119                                        $par.find('.tree-folder-content:eq(0)').show();
120                                } else {
121                                        this.populate( $el );
122                                }
123
124                                $par.find('.'+this.options['close-icon']+':eq(0)')
125                                        .removeClass(this.options['close-icon'])
126                                        .addClass(this.options['open-icon']);
127
128                                this.$element.trigger('opened', $el.data());
129                        } else {
130                                if(this.options.cacheItems) {
131                                        $par.find('.tree-folder-content:eq(0)').hide();
132                                } else {
133                                        $par.find('.tree-folder-content:eq(0)').empty();
134                                }
135
136                                $par.find('.'+this.options['open-icon']+':eq(0)')
137                                        .removeClass(this.options['open-icon'])
138                                        .addClass(this.options['close-icon']);
139
140                                this.$element.trigger('closed', $el.data());
141                        }
142                },
143
144                selectedItems: function () {
145                        var $sel = this.$element.find('.tree-selected');
146                        var data = [];
147
148                        $.each($sel, function (index, value) {
149                                data.push($(value).data());
150                        });
151                        return data;
152                }
153        };
154
155
156        // TREE PLUGIN DEFINITION
157
158        $.fn.tree = function (option, value) {
159                var methodReturn;
160
161                var $set = this.each(function () {
162                        var $this = $(this);
163                        var data = $this.data('tree');
164                        var options = typeof option === 'object' && option;
165
166                        if (!data) $this.data('tree', (data = new Tree(this, options)));
167                        if (typeof option === 'string') methodReturn = data[option](value);
168                });
169
170                return (methodReturn === undefined) ? $set : methodReturn;
171        };
172
173        $.fn.tree.defaults = {
174                multiSelect: false,
175                loadingHTML: '<div>Loading...</div>',
176                cacheItems: true
177        };
178
179        $.fn.tree.Constructor = Tree;
180
181})(window.jQuery);
Note: See TracBrowser for help on using the repository browser.