1 | var collections_widget = null; |
---|
2 | var columns_widget = null; |
---|
3 | var active_widget = null; |
---|
4 | |
---|
5 | function ListWidget(_parent_div) { |
---|
6 | this.parent_div = $(_parent_div);//$('#columns-widget'); |
---|
7 | this.listwidget_container = {}; |
---|
8 | this.widgets = []; |
---|
9 | |
---|
10 | this.init(); |
---|
11 | |
---|
12 | }; |
---|
13 | |
---|
14 | ListWidget.prototype.getType = function(){ |
---|
15 | if (this == collections_widget) { |
---|
16 | return "cls"; |
---|
17 | } |
---|
18 | return "col"; |
---|
19 | }; |
---|
20 | |
---|
21 | ListWidget.prototype.getTypePrefix = function(){ |
---|
22 | if (this == columns_widget) { |
---|
23 | return "col_0_"; |
---|
24 | } |
---|
25 | if (this == collections_widget) { |
---|
26 | return "cls_0_"; |
---|
27 | } |
---|
28 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
29 | return "col_" + qid + "_"; |
---|
30 | |
---|
31 | }; |
---|
32 | |
---|
33 | ListWidget.prototype.add = function(list_item){ |
---|
34 | this.widgets[this.widgets.length] = list_item; |
---|
35 | list_item.parent_container = this.getListWidget(); |
---|
36 | list_item.listid = this.getTypePrefix() + this.widgets.length; |
---|
37 | list_item.render(); |
---|
38 | }; |
---|
39 | |
---|
40 | ListWidget.prototype.clear = function() { |
---|
41 | var type = this.getTypePrefix(); |
---|
42 | for (var i = this.widgets.length; i > 0; i--){ |
---|
43 | this.widgets.splice(i, 1); |
---|
44 | $('#' + type + i).remove(); |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | ListWidget.prototype.remove = function(id){ |
---|
49 | for (var i = 0; i < this.widgets.length; i++) { |
---|
50 | if (this.widgets[i].listid == id) { |
---|
51 | this.widgets.splice(i, 1); |
---|
52 | } |
---|
53 | } |
---|
54 | $('#' + id).remove(); |
---|
55 | }; |
---|
56 | |
---|
57 | ListWidget.prototype.init = function () { |
---|
58 | var x = "<input type='text' class='input-widget autocomplete-input' /><div class='index-context'><table></table></div><div class='list-widget'></div>"; |
---|
59 | $(this.parent_div).append(x); |
---|
60 | |
---|
61 | listwidget_container = $(this.parent_div).find('.list-widget');//$('#columns-list'); |
---|
62 | |
---|
63 | $(this.parent_div).find('.cmd_del').live('click', function(event) { |
---|
64 | var colid = $(this).closest('.list-item').attr("id"); |
---|
65 | if ($(this).parent().parent().parent().attr('class') == 'query-columns'){ |
---|
66 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
67 | var q = queryset.getQuery(qid); |
---|
68 | q.columns_widget.remove(colid); |
---|
69 | }else if ($(this).parent().parent().parent().attr('id') == "columns-widget"){ |
---|
70 | columns_widget.remove(colid); |
---|
71 | } else { |
---|
72 | collections_widget.remove(colid); |
---|
73 | } |
---|
74 | }); |
---|
75 | |
---|
76 | |
---|
77 | var input = $(this.parent_div).find('.input-widget'); |
---|
78 | |
---|
79 | input.focusin(function(){ |
---|
80 | if ($(this).parent().attr('class') == 'query-columns'){ |
---|
81 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
82 | var q = queryset.getQuery(qid); |
---|
83 | active_widget = q.columns_widget; |
---|
84 | }else{ |
---|
85 | active_widget = columns_widget; |
---|
86 | } |
---|
87 | }); |
---|
88 | input.bind('change', function(){ |
---|
89 | |
---|
90 | }); |
---|
91 | |
---|
92 | input.keydown(function(ev){ |
---|
93 | var evStop = function(){ |
---|
94 | ev.stopPropagation(); |
---|
95 | ev.preventDefault(); |
---|
96 | }; |
---|
97 | if (ev.which === 13) { |
---|
98 | if ($(this).parent().attr('class') == 'query-columns'){ |
---|
99 | var qid = $(this).closest('.query_wrapper').attr("id"); |
---|
100 | var q = queryset.getQuery(qid); |
---|
101 | q.columns_widget.add(new ListItem($(this).val())); |
---|
102 | }else{ |
---|
103 | columns_widget.add(new ListItem($(this).val())); |
---|
104 | } |
---|
105 | evStop(); |
---|
106 | } |
---|
107 | }); |
---|
108 | |
---|
109 | if ($(this.parent_div).attr('id') == "collections-widget") { |
---|
110 | $(input).hide(); |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | ListWidget.prototype.getListWidget = function() { |
---|
115 | return $(this.parent_div).find('.list-widget'); |
---|
116 | }; |
---|
117 | ListWidget.prototype.initAutocomplete = function(autocomplete_array) { |
---|
118 | |
---|
119 | if (autocomplete_array.size == 0) return; |
---|
120 | |
---|
121 | //autocomplete |
---|
122 | function handleSelectionWidget(elem, widget){ |
---|
123 | var input = $(widget.parent_div).find('.input-widget'); |
---|
124 | var context = $(input).next('.index-context'); |
---|
125 | |
---|
126 | // fill context |
---|
127 | $(context).html(elements_hashtable[elem]); |
---|
128 | $(context).show(); |
---|
129 | $(input).blur(function(){ |
---|
130 | $(context).hide(); |
---|
131 | }); |
---|
132 | $(input).focusin(function(){ |
---|
133 | $(context).hide(); |
---|
134 | }); |
---|
135 | |
---|
136 | }; |
---|
137 | |
---|
138 | //autocomplete |
---|
139 | function findValueWidget(e) { |
---|
140 | var sValue = e.selectValue; |
---|
141 | handleSelectionWidget(sValue, active_widget); |
---|
142 | } |
---|
143 | |
---|
144 | function selectItemWidget(li) { |
---|
145 | findValueWidget(li); |
---|
146 | } |
---|
147 | |
---|
148 | var ac = $(this.parent_div).find('.input-widget').autocompleteArray(autocomplete_array,{ |
---|
149 | autoFill:true, |
---|
150 | width:150, |
---|
151 | onFindValue:findValueWidget, |
---|
152 | onItemSelect:selectItemWidget |
---|
153 | //extraParams: {oo, '75'} |
---|
154 | }); |
---|
155 | |
---|
156 | //ac.setExtraParams({aaa:3}); |
---|
157 | |
---|
158 | }; |
---|
159 | |
---|
160 | ListWidget.prototype.load = function(items) { |
---|
161 | this.clear(); |
---|
162 | |
---|
163 | if (this.getType() == "cls"){ |
---|
164 | for(var i=0; i<items.length; ++i) { |
---|
165 | this.add(new ListItem(items[i].name, items[i].index)); |
---|
166 | } |
---|
167 | |
---|
168 | } else |
---|
169 | { |
---|
170 | for(var i=0; i<items.length; ++i) { |
---|
171 | if (items[i].length > 0){ |
---|
172 | this.add(new ListItem(items[i])); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | } |
---|
177 | }; |
---|
178 | |
---|
179 | ListWidget.prototype.getColumnsListText = function() { |
---|
180 | var column_text = ""; |
---|
181 | $.each(this.widgets,function(i,column){ |
---|
182 | column_text = column_text + "," + column.name; |
---|
183 | }); |
---|
184 | |
---|
185 | if (column_text.length > 1) { |
---|
186 | column_text = column_text.substring(1); |
---|
187 | } |
---|
188 | return column_text; |
---|
189 | }; |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | function ListItem(_name, _index ) { |
---|
197 | this.name = _name; |
---|
198 | this.index = _index; |
---|
199 | this.listid = ""; |
---|
200 | this.parent_container = {}; |
---|
201 | } |
---|
202 | |
---|
203 | ListItem.prototype.render = function () { |
---|
204 | |
---|
205 | var x = "<div id='" + this.listid + "'class='list-item'><span>" + this.name + "</span><span class='cmd cmd_del'> </span></div>"; |
---|
206 | |
---|
207 | $(this.parent_container).prepend(x); |
---|
208 | //$('#columns-list').prepend(x); |
---|
209 | //addToColumnslist(x); |
---|
210 | this.container = $('#' + this.listid ); |
---|
211 | }; |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|