Commit f500702462b0244ec58f712cdf69b2323604f7fb

Authored by janek37
1 parent 293f2e96

slickgrid - wyszukiwanie

dictionary/ajax_lexeme_slickgrid.py
... ... @@ -9,6 +9,8 @@ from dictionary.ajax_slickgrid import SlickGridAjax, SlickGridQuery
9 9 from common.decorators import ajax
10 10 from common.util import bisect_left, reverse, json_encode
11 11  
  12 +import locale
  13 +locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8')
12 14  
13 15 class LexemeGrid(SlickGridAjax):
14 16 model = Lexeme
... ... @@ -141,55 +143,32 @@ class LexemeGrid(SlickGridAjax):
141 143 # indeks wiersza w danym sortowaniu, w którym
142 144 # znajdzie się instancja o danym id
143 145 @classmethod
144   - def row_index(cls, pk, query):
145   - pk_list = get_pk_list(query)
146   - count = len(pk_list)
  146 + def row_index(cls, lexeme_id, query):
  147 + id_list = get_id_list(query)
  148 + count = len(id_list)
147 149 if count == 0:
148 150 return 0, 0
149   - return pk_list.index(pk), count
  151 + return id_list.index(lexeme_id), count
150 152  
151   - # id instancji z search_field rownym mask badz takiej, ktora bylaby nastepna
152   - # po instancji z search_field równym mask w danym sortowaniu.
153   - # Jezeli nie ma 'wiekszej' instancji badz reguly sortowania nie uwzgledniaja
154   - # search_field, metoda zwroci pierwsza instancje w danym sortowaniu
155 153 @classmethod
156   - def get_pk(cls, query):
157   - pk_list = get_pk_list(query)
158   - count = len(pk_list)
  154 + def search_index(cls, query):
  155 + id_list = get_id_list(query)
  156 + count = len(id_list)
159 157 if count == 0:
160   - return None, None, 0
161   - # nie podoba mi się w ogóle cała ta idea
162   - sort_rules = query.sort_rules
163   - assert len(sort_rules) >= 0
164   - if sort_rules[0]['field'] != cls.search_field:
165   - selected_pk = super(cls, LexemeGrid).get_pk(query)
166   - index, count = cls.row_index(selected_pk, query)
167   - return selected_pk, index, count
  158 + return 0
  159 + first_rule = query.sort_rules[0]
  160 + if first_rule['field'] != cls.search_field:
  161 + return 0
168 162  
169   - index = bisect_left(pk_list, query.mask,
170   - cmp=make_lexeme_cmp(sort_rules[0]))
  163 + index = bisect_left(id_list, query.mask,
  164 + cmp=make_lexeme_cmp(first_rule))
171 165 if index == count:
172 166 index -= 1
173   - return pk_list[index], index, count
174   -
175   - @classmethod
176   - def get_location(cls, query):
177   - selected_pk, index, count = cls.get_pk(query)
178   - return {
179   - 'rowIndex': index,
180   - 'selected_id': selected_pk,
181   - 'records': count,
182   - }
183   -
184   -
185   -import locale
186   -
187   -locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8')
188   -
  167 + return index
189 168  
190 169 def make_lexeme_cmp(rule):
191   - def lexeme_cmp(pk, mask):
192   - e1 = Lexeme.objects.get(pk=pk).entry
  170 + def lexeme_cmp(lexeme_id, mask):
  171 + e1 = Lexeme.objects.get(id=lexeme_id).entry
193 172 e2 = mask
194 173 if rule['a_tergo']:
195 174 e1 = reverse(e1)
... ... @@ -208,16 +187,15 @@ def find_id(request, id, sort_rules, mask, filters=None):
208 187 filters=filters, sort_rules=sort_rules, mask=mask, user=request.user)
209 188 return LexemeGrid.find_id(id, query)
210 189  
211   -# Zapytanie o id oraz indeks pierwszego wiersza przy danym sortowaniu,
212   -# którego hasło rozpoczyna się od mask.
213   -# 'selected_id' == None, jeśli takiego nie ma
214 190 @ajax(method='get')
215   -def get_location(request, sort_rules, mask='', filters=None):
  191 +def search_index(request, sort_rules, search='', filters=None):
216 192 query = SlickGridQuery(
217   - filters=filters, sort_rules=sort_rules, mask=mask, user=request.user)
218   - return LexemeGrid.get_location(query)
  193 + filters=filters, sort_rules=sort_rules, mask=search, user=request.user)
  194 + return {
  195 + 'index': LexemeGrid.search_index(query)
  196 + }
  197 +
219 198  
220   -# twór Miłosza - trzeba kiedyś poprawić
221 199 def cache_key(query):
222 200 key = json_encode(query.sort_rules) + json_encode(query.filters)
223 201 for vocabulary in visible_vocabularies(query.user):
... ... @@ -226,35 +204,34 @@ def cache_key(query):
226 204 key += query.mask
227 205 return md5(key).hexdigest()
228 206  
229   -
230 207 def get_cached_lexemes(query):
231 208 key = cache_key(query)
232 209 return cache.get(key)
233 210  
234 211  
235   -def cache_lexemes(pk_list, query):
  212 +def cache_lexemes(id_list, query):
236 213 key = cache_key(query)
237   - cache.set(key, pk_list)
  214 + cache.set(key, id_list)
238 215 key_list = cache.get('key_list', [])
239 216 if key not in key_list:
240 217 key_list.append(key)
241 218 cache.set('key_list', key_list)
242 219  
243 220  
244   -def get_pk_list(query, force_reload=False):
  221 +def get_id_list(query, force_reload=False):
245 222 if not force_reload:
246   - pk_list = get_cached_lexemes(query)
  223 + id_list = get_cached_lexemes(query)
247 224 else:
248   - pk_list = None
249   - if pk_list is None:
  225 + id_list = None
  226 + if id_list is None:
250 227 lexemes = LexemeGrid.get_sorted_queryset(query)
251 228 if 'rev' in lexemes.query.extra_select:
252   - pk_list = list(row[0] for row in lexemes.values_list('pk', 'rev'))
  229 + id_list = list(row[0] for row in lexemes.values_list('id', 'rev'))
253 230 else:
254 231 #print lexemes.values_list('pk', flat=True).query
255   - pk_list = list(lexemes.values_list('pk', flat=True))
256   - cache_lexemes(pk_list, query)
257   - return pk_list
  232 + id_list = list(lexemes.values_list('id', flat=True))
  233 + cache_lexemes(id_list, query)
  234 + return id_list
258 235  
259 236  
260 237 @ajax(method='get')
... ... @@ -264,16 +241,16 @@ def get_lexemes(request, from_page, to_page, rows, sort_rules, filters=None,
264 241 #request.session['filters'] = json_encode(filters)
265 242 query = SlickGridQuery(
266 243 filters=filters, sort_rules=sort_rules, mask=mask, user=request.user)
267   - pk_list = get_pk_list(query, force_reload)
268   - count = len(pk_list)
  244 + id_list = get_id_list(query, force_reload)
  245 + count = len(id_list)
269 246 start, response_rowcount = LexemeGrid.count_pages(
270 247 count, from_page, to_page, rows)
271   - sublist = pk_list[start:start + response_rowcount]
272   - lexemes_qs = Lexeme.objects.filter(pk__in=sublist).select_related(
  248 + sublist = id_list[start:start + response_rowcount]
  249 + lexemes_qs = Lexeme.objects.filter(id__in=sublist).select_related(
273 250 'owner_vocabulary', 'part_of_speech').prefetch_related(
274 251 'lexemeinflectionpattern_set__pattern',
275 252 'lexemeinflectionpattern_set__inflection_characteristic',
276 253 'vocabularies')
277   - lexemes_dict = dict((l.pk, l) for l in lexemes_qs)
278   - lexemes = [lexemes_dict[pk] for pk in sublist]
  254 + lexemes_dict = dict((l.id, l) for l in lexemes_qs)
  255 + lexemes = [lexemes_dict[id] for id in sublist]
279 256 return LexemeGrid.make_response(lexemes, count, from_page)
... ...
dictionary/templates/lexeme_slickgrid_view.html
... ... @@ -38,8 +38,6 @@
38 38 <script type="text/javascript"
39 39 src="{{ MEDIA_URL }}js/lib/slick.grid.js"></script>
40 40 <script type="text/javascript"
41   - src="{{ MEDIA_URL }}js/lib/slick.rowselectionmodel.js"></script>
42   - <script type="text/javascript"
43 41 src="{{ MEDIA_URL }}js/remotemodel.js"></script>
44 42 <script type="text/javascript"
45 43 src="{{ MEDIA_URL }}js/slickgrid.js"></script>
... ... @@ -66,7 +64,7 @@
66 64 <span class="ui-icon ui-icon-columns">pokaż/ukryj</span>
67 65 </button>
68 66 <input type="text" id="text-search" class="text-search"/>
69   - <button id="searchButton" title="szukaj">
  67 + <button id="search-button" title="szukaj">
70 68 <span class="ui-icon ui-icon-search">szukaj</span>
71 69 </button>
72 70 {% if perms.dictionary.change_lexeme %}
... ... @@ -108,6 +106,17 @@
108 106 <div class="dialog-buttons">
109 107 <button type="button" id="save-sort-order">Zatwierdź"</button>
110 108 </div>
  109 + <div id="choose-filter-dialog" title="Filtrowanie">
  110 + <p>
  111 + <select class="op">
  112 + <option value="AND" selected="selected">oraz</option>
  113 + <option value="OR">lub</option>
  114 + </select>
  115 + <input type="button" value="+" title="Dodaj filtr"
  116 + class="add-filter">
  117 + </p>
  118 + <table id="filter-table"></table>
  119 + </div>
111 120 <div id="load-filter-dialog" title="Wybierz filtr">
112 121 <ul id="filter-list" class="load-dialog-list"></ul>
113 122 </div>
... ...
dictionary/views.py
... ... @@ -35,7 +35,7 @@ def main(request):
35 35 return HttpResponseRedirect(reverse('pattern_view'))
36 36 if request.user.has_perm('dictionary.manage_vocabulary') or \
37 37 request.user.has_perm('auth.add_user'):
38   - return HttpResponseRedirect('management_menu')
  38 + return HttpResponseRedirect(reverse('management_menu'))
39 39 if request.user.has_perm('dictionary.export_lexemes'):
40 40 return HttpResponseRedirect(reverse('export'))
41 41 return HttpResponseRedirect('auth_login') # niezbyt prawdopodobne
... ... @@ -157,7 +157,7 @@ def lexeme_slickgrid_view(request):
157 157 'ajax_odm_forms': reverse('odm_forms'),
158 158 'ajax_edit_form': reverse('lexeme_edit_form'),
159 159 'ajax_update_lexeme': reverse('update_lexeme'),
160   - 'ajax_location': reverse('get_location_slick'),
  160 + 'ajax_search_index': reverse('search_index'),
161 161 'ajax_find_id': reverse('find_id_slick'),
162 162 'ajax_save_filter': reverse('save_filter'),
163 163 'ajax_get_filters': reverse('get_filters'),
... ...
media/css/general.css
... ... @@ -72,6 +72,11 @@ li {
72 72 display: block;
73 73 }
74 74  
  75 +li a
  76 +{
  77 + outline-color: rgba(0, 0, 0, 0);
  78 +}​
  79 +
75 80 .ui-jqgrid-titlebar {
76 81 display: none;
77 82 }
... ...
media/js/common.js
... ... @@ -193,7 +193,7 @@ $(function () {
193 193 });
194 194 debug.height($(window).height() - main_menu.height());
195 195 debug.css('top', main_menu.height());
196   - debug.ajaxError(parse_error);
  196 + $(document).ajaxError(parse_error);
197 197 });
198 198  
199 199 // hash
... ...
media/js/edit.js
... ... @@ -50,5 +50,15 @@ $(function() {
50 50 $(document).on(
51 51 'change', '#' + edit.form_id + ' input[type=checkbox]',
52 52 edit.show_changed);
  53 + $(document).on('click', '#' + edit.form_cancel_id, function () {
  54 + var id = $('input[name=id]', this.form).value();
  55 + $.ajaxJSON({
  56 + method: 'get',
  57 + dest: $('#edit'),
  58 + url: $dj.ajax_edit_form,
  59 + data: {id: id},
  60 + callback: edit.form_init
  61 + });
  62 + });
53 63 edit.init_form_widgets();
54 64 });
55 65 \ No newline at end of file
... ...
media/js/jqgrid.js
1 1 var sort_rules = $dj.sort_rules,
2   - colModel = $dj.colModel, // TODO najpierw wybór kolumn
3   - colNames = $dj.colNames; // TODO?
4   -var timeoutHnd; // TODO wyszukiwanie
  2 + colModel = $dj.colModel, // TODO na koniec
  3 + colNames = $dj.colNames;
  4 +var timeoutHnd;
5 5 var jqgrid = {
6 6 grid: undefined,
7 7  
... ... @@ -55,7 +55,7 @@ var jqgrid = {
55 55 jqgrid.refresh_column_headers();
56 56 },
57 57  
58   - doSearch: function() { // TODO wyszukiwanie
  58 + doSearch: function() {
59 59 "use strict";
60 60 if (!$dj.auto_search)
61 61 return;
... ... @@ -64,7 +64,7 @@ var jqgrid = {
64 64 timeoutHnd = setTimeout(jqgrid.findAndScroll, 500);
65 65 },
66 66  
67   - findAndScroll: function() { // TODO wyszukiwanie
  67 + findAndScroll: function() {
68 68 "use strict";
69 69 var data = jqgrid.grid.jqGrid('getGridParam', 'postData');
70 70 data.mask = $("#phrase_box").val();
... ... @@ -153,15 +153,16 @@ $(function () {
153 153 },
154 154 onSearch: function () {
155 155 $('#phrase_box').val('');
  156 + // TODO
156 157 jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload = true;
157 158 },
158 159 onInitializeSearch: jqgrid.add_buttons
159 160 };
160 161  
161   - if (!colModel) { // TODO wybór kolumn
  162 + if (!colModel) { // TODO na koniec
162 163 colModel = jqgrid.initialColModel;
163 164 }
164   - if (!colNames) { // TODO?
  165 + if (!colNames) {
165 166 colNames = jqgrid.initialColNames;
166 167 }
167 168  
... ... @@ -236,8 +237,8 @@ $(function () {
236 237 loadComplete: function (data) {
237 238 var num = data.records;
238 239 document.title = jqgrid.grid_caption + ' (' + num + ')';
239   - // TODO najpierw filtrowanie
240   - if (jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload === true) {
  240 + // TODO? najpierw filtrowanie
  241 + if (jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload) {
241 242 jqgrid.grid.jqGrid('getGridParam', 'postData').force_reload = false;
242 243 setTimeout(jqgrid.findAndScroll, 100);
243 244 }
... ... @@ -255,10 +256,9 @@ $(function () {
255 256 });
256 257  
257 258 jqgrid.refresh_column_headers(); // TODO? sortowanie
258   - jqgrid.findAndScroll(); // TODO wyszukiwanie
  259 + jqgrid.findAndScroll();
259 260  
260 261 $(document).on('submit', '#' + jqgrid.edit_form_id, jqgrid.edit_form_submit);
261   - // TODO [copypasta?]
262 262 $(document).on('click', '#' + jqgrid.edit_form_cancel_id, function () {
263 263 var id = $('input[name=id]', this.form).value();
264 264 $.ajaxJSON({
... ... @@ -281,9 +281,7 @@ $(function () {
281 281 'change', '#' + jqgrid.edit_form_id + ' input[type=checkbox]',
282 282 jqgrid.show_changed);
283 283  
284   - // TODO wyszukiwanie
285 284 $('#searchButton').click(jqgrid.findAndScroll);
286   - // TODO wyszukiwanie
287 285 $('#phrase_box').keydown(jqgrid.doSearch);
288 286  
289 287 // TODO sortowanie
... ...
media/js/lexeme-slickgrid-view.js
... ... @@ -113,7 +113,9 @@ $.extend(slickgrid, {
113 113 'Typ odsyłacza'
114 114 ],
115 115 initial_sort_rules: [
116   - {field: 'entry', order: 'asc', a_tergo: false}
  116 + {field: 'entry', order: 'asc', a_tergo: false},
  117 + {field: 'part_of_speech', order: 'asc'},
  118 + {field: 'id', order: 'asc'}
117 119 ],
118 120 grid_caption: "Leksemy"
119 121 });
... ... @@ -184,8 +186,8 @@ slickgrid.add_filter_buttons = function(f) {
184 186 };
185 187 $.ajaxJSON($.extend(save_filter_data, {
186 188 callback: function (data) {
187   - if (data.exists &&
188   - window.confirm("Filtr o tej nazwie już istnieje. Nadpisać?")) {
  189 + if (data.exists && window.confirm(
  190 + "Filtr o tej nazwie już istnieje. Nadpisać?")) {
189 191 $.ajaxJSON($.extend(save_filter_data, {
190 192 data: $.extend(post_data, {force: true})
191 193 }));
... ...
media/js/lib/slick.rowselectionmodel.js deleted
1   -(function ($) {
2   - // register namespace
3   - $.extend(true, window, {
4   - "Slick": {
5   - "RowSelectionModel": RowSelectionModel
6   - }
7   - });
8   -
9   - function RowSelectionModel(options) {
10   - var _grid;
11   - var _ranges = [];
12   - var _self = this;
13   - var _handler = new Slick.EventHandler();
14   - var _inHandler;
15   - var _options;
16   - var _defaults = {
17   - selectActiveRow: true
18   - };
19   -
20   - function init(grid) {
21   - _options = $.extend(true, {}, _defaults, options);
22   - _grid = grid;
23   - _handler.subscribe(_grid.onActiveCellChanged,
24   - wrapHandler(handleActiveCellChange));
25   - _handler.subscribe(_grid.onKeyDown,
26   - wrapHandler(handleKeyDown));
27   - _handler.subscribe(_grid.onClick,
28   - wrapHandler(handleClick));
29   - }
30   -
31   - function destroy() {
32   - _handler.unsubscribeAll();
33   - }
34   -
35   - function wrapHandler(handler) {
36   - return function () {
37   - if (!_inHandler) {
38   - _inHandler = true;
39   - handler.apply(this, arguments);
40   - _inHandler = false;
41   - }
42   - };
43   - }
44   -
45   - function rangesToRows(ranges) {
46   - var rows = [];
47   - for (var i = 0; i < ranges.length; i++) {
48   - for (var j = ranges[i].fromRow; j <= ranges[i].toRow; j++) {
49   - rows.push(j);
50   - }
51   - }
52   - return rows;
53   - }
54   -
55   - function rowsToRanges(rows) {
56   - var ranges = [];
57   - var lastCell = _grid.getColumns().length - 1;
58   - for (var i = 0; i < rows.length; i++) {
59   - ranges.push(new Slick.Range(rows[i], 0, rows[i], lastCell));
60   - }
61   - return ranges;
62   - }
63   -
64   - function getRowsRange(from, to) {
65   - var i, rows = [];
66   - for (i = from; i <= to; i++) {
67   - rows.push(i);
68   - }
69   - for (i = to; i < from; i++) {
70   - rows.push(i);
71   - }
72   - return rows;
73   - }
74   -
75   - function getSelectedRows() {
76   - return rangesToRows(_ranges);
77   - }
78   -
79   - function setSelectedRows(rows) {
80   - setSelectedRanges(rowsToRanges(rows));
81   - }
82   -
83   - function setSelectedRanges(ranges) {
84   - _ranges = ranges;
85   - _self.onSelectedRangesChanged.notify(_ranges);
86   - }
87   -
88   - function getSelectedRanges() {
89   - return _ranges;
90   - }
91   -
92   - function handleActiveCellChange(e, data) {
93   - if (_options.selectActiveRow && data.row != null) {
94   - setSelectedRanges([new Slick.Range(data.row, 0, data.row, _grid.getColumns().length - 1)]);
95   - }
96   - }
97   -
98   - function handleKeyDown(e) {
99   - var activeRow = _grid.getActiveCell();
100   - if (activeRow && e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey && (e.which == 38 || e.which == 40)) {
101   - var selectedRows = getSelectedRows();
102   - selectedRows.sort(function (x, y) {
103   - return x - y
104   - });
105   -
106   - if (!selectedRows.length) {
107   - selectedRows = [activeRow.row];
108   - }
109   -
110   - var top = selectedRows[0];
111   - var bottom = selectedRows[selectedRows.length - 1];
112   - var active;
113   -
114   - if (e.which == 40) {
115   - active = activeRow.row < bottom || top == bottom ? ++bottom : ++top;
116   - } else {
117   - active = activeRow.row < bottom ? --bottom : --top;
118   - }
119   -
120   - if (active >= 0 && active < _grid.getDataLength()) {
121   - _grid.scrollRowIntoView(active);
122   - _ranges = rowsToRanges(getRowsRange(top, bottom));
123   - setSelectedRanges(_ranges);
124   - }
125   -
126   - e.preventDefault();
127   - e.stopPropagation();
128   - }
129   - }
130   -
131   - function handleClick(e) {
132   - var cell = _grid.getCellFromEvent(e);
133   - if (!cell || !_grid.canCellBeActive(cell.row, cell.cell)) {
134   - return false;
135   - }
136   -
137   - var selection = rangesToRows(_ranges);
138   - var idx = $.inArray(cell.row, selection);
139   -
140   - if (!e.ctrlKey && !e.shiftKey && !e.metaKey) {
141   - return false;
142   - }
143   - else if (_grid.getOptions().multiSelect) {
144   - if (idx === -1 && (e.ctrlKey || e.metaKey)) {
145   - selection.push(cell.row);
146   - _grid.setActiveCell(cell.row, cell.cell);
147   - } else if (idx !== -1 && (e.ctrlKey || e.metaKey)) {
148   - selection = $.grep(selection, function (o, i) {
149   - return (o !== cell.row);
150   - });
151   - _grid.setActiveCell(cell.row, cell.cell);
152   - } else if (selection.length && e.shiftKey) {
153   - var last = selection.pop();
154   - var from = Math.min(cell.row, last);
155   - var to = Math.max(cell.row, last);
156   - selection = [];
157   - for (var i = from; i <= to; i++) {
158   - if (i !== last) {
159   - selection.push(i);
160   - }
161   - }
162   - selection.push(last);
163   - _grid.setActiveCell(cell.row, cell.cell);
164   - }
165   - }
166   -
167   - _ranges = rowsToRanges(selection);
168   - setSelectedRanges(_ranges);
169   - e.stopImmediatePropagation();
170   -
171   - return true;
172   - }
173   -
174   - $.extend(this, {
175   - "getSelectedRows": getSelectedRows,
176   - "setSelectedRows": setSelectedRows,
177   -
178   - "getSelectedRanges": getSelectedRanges,
179   - "setSelectedRanges": setSelectedRanges,
180   -
181   - "init": init,
182   - "destroy": destroy,
183   -
184   - "onSelectedRangesChanged": new Slick.Event()
185   - });
186   - }
187   -})(jQuery);
188 0 \ No newline at end of file
media/js/remotemodel.js
1 1 (function ($) {
2 2 "use strict";
3   - function RemoteModel(url) {
  3 + function RemoteModel(data_url, search_index_url) {
4 4 // private
5 5 var PAGESIZE = 50;
6 6 var data = {length: 0};
... ... @@ -9,6 +9,8 @@
9 9 var sortdir = 1;
10 10 var h_request = null;
11 11 var ajax_request = null;
  12 + var sort_rules = null;
  13 + var filters = null;
12 14  
13 15 // events
14 16 var onDataLoading = new Slick.Event();
... ... @@ -27,7 +29,10 @@
27 29 }
28 30  
29 31 function clear() {
30   - $.each(data, function(key) {delete data[key];});
  32 + for (var key in data) {
  33 + if (data.hasOwnProperty(key))
  34 + delete data[key];
  35 + }
31 36 data.length = 0;
32 37 }
33 38  
... ... @@ -71,17 +76,19 @@
71 76  
72 77 onDataLoading.notify({from: from, to: to});
73 78  
  79 + var ajax_data = {
  80 + rows: PAGESIZE,
  81 + from_page: fromPage,
  82 + to_page: toPage,
  83 + sort_rules: sort_rules
  84 + };
  85 + if (searchstr !== '') ajax_data.mask = searchstr;
  86 +
74 87 ajax_request = $.ajaxJSON({
75   - url: url,
  88 + url: data_url,
76 89 method: 'get',
77 90 callback: onSuccess,
78   - data: {
79   - rows: PAGESIZE,
80   - from_page: fromPage,
81   - to_page: toPage,
82   - // do testów
83   - sort_rules: [{'field': 'entry', 'order': 'asc', 'a_tergo': false}]
84   - },
  91 + data: ajax_data,
85 92 error: function () {
86 93 onError(fromPage, toPage);
87 94 }
... ... @@ -118,10 +125,8 @@
118 125 ensureData(from, to);
119 126 }
120 127  
121   - // TODO: sortowanie po wielu kolumnach
122   - function setSort(column, dir) {
123   - sortcol = column;
124   - sortdir = dir;
  128 + function setSort(rules) {
  129 + sort_rules = rules;
125 130 clear();
126 131 }
127 132  
... ... @@ -130,6 +135,27 @@
130 135 clear();
131 136 }
132 137  
  138 + function setFilters(f) {
  139 + filters = f;
  140 + clear();
  141 + }
  142 +
  143 + function searchRow(search_text, callback) {
  144 + var data = {
  145 + filters: filters,
  146 + sort_rules: sort_rules,
  147 + search: search_text
  148 + };
  149 + $.ajaxJSON({
  150 + url: search_index_url,
  151 + method: 'get',
  152 + data: data,
  153 + callback: function(data) {
  154 + callback(data.index);
  155 + }
  156 + });
  157 + }
  158 +
133 159 init();
134 160  
135 161 return {
... ... @@ -143,6 +169,8 @@
143 169 "reloadData": reloadData,
144 170 "setSort": setSort,
145 171 "setSearch": setSearch,
  172 + "setFilters": setFilters,
  173 + "searchRow": searchRow,
146 174  
147 175 // events
148 176 "onDataLoading": onDataLoading,
... ...
media/js/slickgrid.js
1   -var loader = new Slick.Data.RemoteModel($dj.ajax_get_page);
  1 +var loader = new Slick.Data.RemoteModel(
  2 + $dj.ajax_get_page, $dj.ajax_search_index);
  3 +
  4 +function search_string() {
  5 + "use strict";
  6 + return $('#text-search').val();
  7 +}
2 8  
3 9 var slickgrid = {
4 10 grid: null,
5   - sort_rules: $dj.sort_rules,
6 11  
7 12 active_id: function(cell) {
8 13 "use strict";
9 14 if (cell === undefined)
10 15 cell = slickgrid.grid.getActiveCell();
11 16 var row = cell.row;
  17 + if (row === undefined)
  18 + return undefined;
12 19 return slickgrid.grid.getDataItem(row).id;
  20 + },
  21 +
  22 + scroll_search: function() {
  23 + "use strict";
  24 + // data = aktualne filtry i sort_rules
  25 + loader.searchRow(search_string(), function (row) {
  26 + slickgrid.grid.scrollRowToTop(row);
  27 + slickgrid.grid.setActiveCell(row, 0);
  28 + });
  29 + },
  30 +
  31 + search: function() {
  32 + "use strict";
  33 + if ($dj.filtering_mode) {
  34 + loader.setSearch(search_string());
  35 + slickgrid.grid.scrollRowToTop(0);
  36 + slickgrid.ensure_data();
  37 + slickgrid.grid.setActiveCell(0, 0);
  38 + } else {
  39 + slickgrid.scroll_search();
  40 + }
  41 + },
  42 +
  43 + ensure_data: function() {
  44 + "use strict";
  45 + var vp = slickgrid.grid.getViewport();
  46 + loader.ensureData(vp.top, vp.bottom);
  47 + },
  48 +
  49 + reload_data: function() {
  50 + "use strict";
  51 + var vp = slickgrid.grid.getViewport();
  52 + loader.reloadData(vp.top, vp.bottom);
13 53 }
14 54 };
15 55  
... ... @@ -45,14 +85,12 @@ $(function() {
45 85 //slickgrid.grid.setSelectionModel(new Slick.RowSelectionModel());
46 86  
47 87 slickgrid.grid.onViewportChanged.subscribe(function (e, args) {
48   - var vp = slickgrid.grid.getViewport();
49   - loader.ensureData(vp.top, vp.bottom);
  88 + slickgrid.ensure_data();
50 89 });
51 90  
52 91 slickgrid.grid.onSort.subscribe(function (e, args) {
53 92 loader.setSort(args.sortCol.field, args.sortAsc ? 1 : -1);
54   - var vp = slickgrid.grid.getViewport();
55   - loader.ensureData(vp.top, vp.bottom);
  93 + slickgrid.ensure_data();
56 94 });
57 95  
58 96 var timeout_handler;
... ... @@ -67,11 +105,13 @@ $(function() {
67 105 }
68 106 }
69 107 last_active_row = cell.row;
70   - clearTimeout(timeout_handler);
71   - timeout_handler = setTimeout(function() {
72   - common.busy_on();
73   - slickgrid.load_content(slickgrid.active_id(cell));
74   - }, 500);
  108 + if (cell.row !== undefined) {
  109 + clearTimeout(timeout_handler);
  110 + timeout_handler = setTimeout(function() {
  111 + common.busy_on();
  112 + slickgrid.load_content(slickgrid.active_id(cell));
  113 + }, 500);
  114 + }
75 115 });
76 116  
77 117 loader.onDataLoading.subscribe(function () {
... ... @@ -103,20 +143,31 @@ $(function() {
103 143 var num = slickgrid.grid.getDataLength();
104 144 document.title = slickgrid.grid_caption + ' (' + num + ')';
105 145  
  146 + if (!slickgrid.grid.getActiveCell()) {
  147 + var vp = slickgrid.grid.getViewport();
  148 + slickgrid.grid.setActiveCell(vp.top, 0);
  149 + }
106 150 });
107 151  
108   - var text_search = $("#text-search");
109   - text_search.keyup(function (e) {
  152 + var search_timeout_handler;
  153 + $("#text-search").keyup(function (e) {
110 154 if (e.which === 13) {
111   - loader.setSearch($(this).val());
112   - var vp = slickgrid.grid.getViewport();
113   - loader.ensureData(vp.top, vp.bottom);
  155 + slickgrid.search();
  156 + } else if ($dj.auto_search) {
  157 + clearTimeout(search_timeout_handler);
  158 + search_timeout_handler = setTimeout(slickgrid.search, 500);
114 159 }
115 160 });
116 161  
117   - loader.setSearch(text_search.val());
118   - //loader.setSort("create_ts", -1);
119   - //slickgrid.grid.setSortColumn("date", false);
  162 + if ($dj.filtering_mode) {
  163 + loader.setSearch(search_string());
  164 + }
  165 +
  166 + loader.setSort($dj.sort_rules || slickgrid.initial_sort_rules);
  167 +
  168 + loader.setFilters([]);
  169 +
  170 + $('#search-button').click(slickgrid.search);
120 171  
121 172 // load the first page
122 173 slickgrid.grid.onViewportChanged.notify();
... ...
... ... @@ -63,7 +63,7 @@ urlpatterns += patterns(&#39;dictionary.ajax_lexeme_jqgrid&#39;,
63 63  
64 64 urlpatterns += patterns('dictionary.ajax_lexeme_slickgrid',
65 65 url(r'^ajax/lexemes-slick/$', 'get_lexemes', name='get_lexemes_slick'),
66   - url(r'^ajax/location-slick/$', 'get_location', name='get_location_slick'),
  66 + url(r'^ajax/location-slick/$', 'search_index', name='search_index'),
67 67 url(r'^ajax/find_id-slick/$', 'find_id', name='find_id_slick'),
68 68 )
69 69  
... ...