diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-24 16:13:16 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-24 16:48:04 +0200 |
commit | f60470064e73bf874c96db8f4217c4c8ba603c39 (patch) | |
tree | 354a5f8d9b9c2b226c7fae104a4f799d6a8fceef /ishtar_common | |
parent | df6ea17f4b8bb1d45fe480044af80bd19742bf80 (diff) | |
download | Ishtar-f60470064e73bf874c96db8f4217c4c8ba603c39.tar.bz2 Ishtar-f60470064e73bf874c96db8f4217c4c8ba603c39.zip |
WIP: ajaxify shortcut menu - 3
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/static/js/ishtar.js | 144 | ||||
-rw-r--r-- | ishtar_common/static/media/style.css | 10 | ||||
-rw-r--r-- | ishtar_common/templates/base.html | 6 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/shortcut_menu.html | 20 | ||||
-rw-r--r-- | ishtar_common/urls.py | 4 | ||||
-rw-r--r-- | ishtar_common/views.py | 78 |
6 files changed, 109 insertions, 153 deletions
diff --git a/ishtar_common/static/js/ishtar.js b/ishtar_common/static/js/ishtar.js index 393603b6c..9021dc4f5 100644 --- a/ishtar_common/static/js/ishtar.js +++ b/ishtar_common/static/js/ishtar.js @@ -37,62 +37,43 @@ function get_next_table_id(){} function get_previous_table_id(){} var shortcut_url = ''; -var get_file_url = '/get-file-shortcut/'; -var get_own_file_url = '/get-file-shortcut/own/'; -var get_operation_url = '/get-operation-shortcut/'; -var get_own_operation_url = '/get-operation-shortcut/own/'; -var get_contextrecord_url = '/get-contextrecord-shortcut/'; -var get_own_contextrecord_url = '/get-contextrecord-shortcut/own/'; -var get_find_url = '/get-find-shortcut/'; -var get_own_find_url = '/get-find-shortcut/own/'; -var shortcut_get_attr = '?submited=1&page=1&rows=10&cached_label='; +var activate_all_search_url = '/activate-all-search/'; +var activate_own_search_url = '/activate-own-search/'; -var current_file_change = function(){ - $.post('/' + url_path + 'update-current-item/', - {item:'file', value:$("#current_file").val()}, - load_shortcut_menu - ); -}; - -var current_operation_change = function(){ - $.post('/' + url_path + 'update-current-item/', - {item:'operation', value:$("#current_operation").val()}, - load_shortcut_menu - ); -}; - -var current_contextrecord_change = function(){ - $.post('/' + url_path + 'update-current-item/', - {item:'contextrecord', value:$("#current_contextrecord").val()}, - load_shortcut_menu - ); -}; - -var current_find_change = function(){ - $.post('/' + url_path + 'update-current-item/', - {item:'find', value:$("#current_find").val()}, - load_shortcut_menu - ); -}; function init_shortcut_fields(){ - var is_own = $('.activate_all_search').hasClass('disabled'); - var file_url = get_file_url; - if (is_own) file_url = get_own_file_url; - chosen_ajaxify("current_file", file_url + shortcut_get_attr, - current_file_change); - var operation_url = get_operation_url; - if (is_own) operation_url = get_own_operation_url; - chosen_ajaxify("current_operation", operation_url + shortcut_get_attr, - current_operation_change); - var contextrecord_url = get_contextrecord_url; - if (is_own) contextrecord_url = get_own_contextrecord_url; - chosen_ajaxify("current_contextrecord", contextrecord_url + shortcut_get_attr, - current_contextrecord_change); - var find_url = get_find_url; - if (is_own) find_url = get_own_find_url; - chosen_ajaxify("current_find", find_url + shortcut_get_attr, - current_find_change); + $('#id_file-shortcut').change(function(){ + $("#id_select_file-shortcut").attr( + 'title', $('#id_select_file-shortcut').val()); + $.post('/' + url_path + 'update-current-item/', + {item: "file", value:$("#id_file-shortcut").val()}, + load_shortcut_menu + ); + }); + $('#id_operation-shortcut').change(function(){ + $("#id_select_operation-shortcut").attr( + 'title', $('#id_select_operation-shortcut').val()); + $.post('/' + url_path + 'update-current-item/', + {item: "operation", value:$("#id_operation-shortcut").val()}, + load_shortcut_menu + ); + }); + $('#id_contextrecord-shortcut').change(function(){ + $("#id_select_contextrecord-shortcut").attr( + 'title', $('#id_select_contextrecord-shortcut').val()); + $.post('/' + url_path + 'update-current-item/', + {item: "contextrecord", value:$("#id_contextrecord-shortcut").val()}, + load_shortcut_menu + ); + }); + $('#id_find-shortcut').change(function(){ + $("#id_select_find-shortcut").attr( + 'title', $('#id_select_find-shortcut').val()); + $.post('/' + url_path + 'update-current-item/', + {item: "find", value:$("#id_find-shortcut").val()}, + load_shortcut_menu + ); + }); } function init_shortcut_menu(html){ @@ -225,7 +206,7 @@ function load_window(url, speed, on_success){ } function load_current_window(url, model_name){ - var id = $("#current_" + model_name).val(); + var id = $("#id_" + model_name + "-shortcut").val(); if (!id) return; url = url.split('/'); url[url.length - 1] = id; @@ -302,66 +283,23 @@ function show_hide_flex(id){ } } -var delay = (function(){ - var timer = 0; - return function(callback, ms){ - clearTimeout (timer); - timer = setTimeout(callback, ms); - }; -})(); - - -function chosen_ajaxify(id, ajax_url, current_change_callback){ - $('div#' + id + '_chosen .chosen-search input').keyup(function(){ - var keyword = $('div#' + id + '_chosen .chosen-search input').val(); - var keyword_pattern = new RegExp(keyword, 'gi'); - $('div#' + id + '_chosen ul.chosen-results').empty(); - $("#"+id).empty(); - delay(function(){ - $.ajax({ - url: ajax_url + keyword, - dataType: "json", - success: function(response){ - $('#'+id).append('<option value="">----------</option>'); - if ('rows' in response){ - $.map(response['rows'], function(item){ - $('#'+id).append('<option value="' + item['id'] + '">' + item['cached_label'] + '</option>'); - }); - } - $("#"+id).trigger("chosen:updated"); - $('div#' + id + '_chosen .chosen-search input').val(keyword); - $('div#' + id + '_chosen').removeClass('chosen-container-single-nosearch'); - $('div#' + id + '_chosen .chosen-search input').removeAttr('readonly'); - $('div#' + id + '_chosen .chosen-search input').focus(); - $('div#' + id + '_chosen .active-result').each(function(){ - var html = $('div#' + id + '_chosen ul.chosen-results').html(); - $('div#' + id + '_chosen ul.chosen-results').html(html.replace(keyword_pattern, function(matched){ - return '<em>' + matched + '</em>'; - })); - }); - $("#" + id).change(current_change_callback); - } - }); - }, 1000); - }); -} - - var activate_all_search_msg = "Searches in the shortcut menu deals with all items."; var activate_own_search_msg = "Searches in the shortcut menu deals with only your items."; function activate_all_search(){ $('.activate_all_search').removeClass('disabled'); $('.activate_own_search').addClass('disabled'); - init_shortcut_fields(); - display_info(activate_all_search_msg); + $.get(activate_all_search_url, function(data) { + display_info(activate_all_search_msg); + }); return false; } function activate_own_search(){ $('.activate_own_search').removeClass('disabled'); $('.activate_all_search').addClass('disabled'); - init_shortcut_fields(); - display_info(activate_own_search_msg); + $.get(activate_own_search_url, function(data) { + display_info(activate_own_search_msg); + }); return false; } diff --git a/ishtar_common/static/media/style.css b/ishtar_common/static/media/style.css index d74e0aaa2..fff2fad78 100644 --- a/ishtar_common/static/media/style.css +++ b/ishtar_common/static/media/style.css @@ -29,10 +29,16 @@ a, a.remove { color:#D14; } +span.disabled, a.disabled { color:#ccc; } +span.disabled:hover, +a.disabled:hover { + color:#DB5C7C; +} + .badge, a.add-button, #reset_wizards, @@ -812,7 +818,9 @@ table.confirm tr.spacer td:last-child{ /* jquery widget customizations */ .ui-autocomplete{ - font-size:0.7em + font-size:0.7em; + z-index:10000 !important; + width:350px; } .ui-autocomplete-loading { diff --git a/ishtar_common/templates/base.html b/ishtar_common/templates/base.html index cd7b00457..7101ed7de 100644 --- a/ishtar_common/templates/base.html +++ b/ishtar_common/templates/base.html @@ -33,13 +33,11 @@ <script type='text/javascript'> var shortcut_url = '{% url "shortcut-menu" %}'; var get_file_url = '{% url "get-file-shortcut" %}'; - var get_own_file_url = '{% url "get-own-file-shortcut" %}'; var get_operation_url = '{% url "get-operation-shortcut" %}'; - var get_own_operation_url = '{% url "get-own-operation-shortcut" %}'; var get_contextrecord_url = '{% url "get-contextrecord-shortcut" %}'; - var get_own_contextrecord_url = '{% url "get-own-contextrecord-shortcut" %}'; var get_find_url = '{% url "get-find-shortcut" %}'; - var get_own_find_url = '{% url "get-own-find-shortcut" %}'; + var activate_all_search_url = '{% url "activate-all-search" %}'; + var activate_own_search_url = '{% url "activate-own-search" %}'; var activate_all_search_msg = "{% trans 'Searches in the shortcut menu deals with all items.' %}"; var activate_own_search_msg = "{% trans 'Searches in the shortcut menu deals with only your items.' %}"; </script> diff --git a/ishtar_common/templates/ishtar/blocks/shortcut_menu.html b/ishtar_common/templates/ishtar/blocks/shortcut_menu.html index 8a0f694ae..fc1131c90 100644 --- a/ishtar_common/templates/ishtar/blocks/shortcut_menu.html +++ b/ishtar_common/templates/ishtar/blocks/shortcut_menu.html @@ -1,27 +1,27 @@ {% load i18n %} {% load url from future%} -{% if current_menu %} +{% if menu %} <form method="post" action="{% url 'update-current-item' %}"> <fieldset> <div id='action_current_items'> <p><i class="icon fa fa-thumb-tack fa-2x" aria-hidden="true" title="{% trans 'Pin an item in order to constrain default searches with this item. By default only your items are displayed. New created and modified items are auto-pin.' %}"></i></p> <p> -<a href='' onclick='return activate_own_search();' class='activate_own_search'><i class="icon fa fa-user" aria-hidden="true" title="{% trans 'Search within my items' %}"></i></a> -<a href='' class='activate_all_search disabled' onclick='return activate_all_search();'><i class="icon fa fa-users" aria-hidden="true" title="{% trans 'Search within all items' %}"></i></a> +<a href='' onclick='return activate_own_search();' class='activate_own_search{% if SHORTCUT_SEARCH == 'all' %} disabled{% endif %}'><i class="icon fa fa-user" aria-hidden="true" title="{% trans 'Search within my items' %}"></i></a> +<a href='' class='activate_all_search{% if SHORTCUT_SEARCH == 'own' %} disabled{% endif %}' onclick='return activate_all_search();'><i class="icon fa fa-users" aria-hidden="true" title="{% trans 'Search within all items' %}"></i></a> +</p> +<p> +<a href='' onclick='return load_shortcut_menu();' class='disabled'><i class="icon fa fa-2x fa-refresh" aria-hidden="true" title="{% trans 'Refresh menu' %}"></i></a> </p> </div> <table id='current_items'> -{% for lbl, model_name, main_cls, items in current_menu %} +{% for lbl, model_name, current, widget in menu %} <tr> <td><label for="current_{{model_name}}">{{lbl}}</label></td> <td> - <select class='{{main_cls}} chosen-select' id='current_{{model_name}}'> - <option class='normal' value=''>----------</option> - {% for val, label, selected, cls in items %}<option{% if cls %} class='{{cls}}'{% endif %} value='{{val}}'{% if selected %} selected="selected"{% endif %}>{% ifequal cls 'basket' %} {% endifequal %}{% ifequal cls 'green' %} {% endifequal %}{% ifequal cls 'orange' %} {% endifequal %}{% ifequal cls 'red' %} {% endifequal %}{{label}}</option> - {% endfor %}</select> + {{widget|safe}} </td>{% with 'show-'|add:model_name as model_url%} - <td><a href='#' onclick='load_current_window("{% url model_url 0 %}", "{{model_name}}");' class='display_details'><i class="fa fa-info-circle" aria-hidden="true"></i></a></td> - <td><span class='pin-action' onclick='$.get("{% url 'unpin' model_name %}", function(){load_shortcut_menu();});' title="{% trans 'Unpin' %}"> + <td><a href='#' onclick='load_current_window("{% url model_url current %}", "{{model_name}}");' class='display_details'><i class="fa fa-info-circle" aria-hidden="true"></i></a></td> + <td><span class='disabled pin-action' onclick='$.get("{% url 'unpin' model_name %}", function(){load_shortcut_menu();});' title="{% trans 'Unpin' %}"> <i class="fa fa-times"></i> </span></td> {% endwith %} diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index 27078dd9e..5d8d25843 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -192,6 +192,10 @@ urlpatterns += patterns( views.OrgaManualMergeItems.as_view(), name='orga_manual_merge_items'), url(r'reset/$', 'reset_wizards', name='reset_wizards'), + url(r'activate-all-search/$', 'activate_all_search', + name='activate-all-search'), + url(r'activate-own-search/$', 'activate_own_search', + name='activate-own-search'), url(r'(?P<action_slug>' + actions + r')/$', 'action', name='action'), ) diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 27b192e74..67b883447 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -63,7 +63,8 @@ from archaeological_operations.forms import DashboardForm as DashboardFormOpe from archaeological_files.forms import DashboardForm as DashboardFormFile from ishtar_common.forms import FinalForm, FinalDeleteForm -from ishtar_common.utils import get_random_item_image_link, shortify +from ishtar_common.widgets import JQueryAutoComplete +from ishtar_common.utils import get_random_item_image_link from ishtar_common import forms_common as forms from ishtar_common import wizards from ishtar_common.models import HistoryError, PRIVATE_FIELDS, \ @@ -199,6 +200,16 @@ def get_autocomplete_generic(model, extra={'available': True}): return func +def activate_all_search(request): + request.session['SHORTCUT_SEARCH'] = 'all' + return HttpResponse('OK', mimetype='text/plain') + + +def activate_own_search(request): + request.session['SHORTCUT_SEARCH'] = 'own' + return HttpResponse('OK', mimetype='text/plain') + + def shortcut_menu(request): profile = get_current_profile() CURRENT_ITEMS = [] @@ -209,36 +220,19 @@ def shortcut_menu(request): CURRENT_ITEMS.append((_(u"Context record"), ContextRecord)) if profile.find: CURRENT_ITEMS.append((_(u"Find"), Find)) - dct = {'current_menu': []} - current_selected_item = None + dct = {'current_menu': [], 'menu': [], + 'SHORTCUT_SEARCH': request.session['SHORTCUT_SEARCH'] + if 'SHORTCUT_SEARCH' in request.session else 'own'} + for lbl, model in CURRENT_ITEMS: - new_selected_item = None model_name = model.SLUG - cls = '' current = model_name in request.session and request.session[model_name] - items = [] - for item in model.get_owns(request.user, - menu_filtr=current_selected_item): - pk = unicode(item.pk) - if item.IS_BASKET: - pk = "basket-" + pk - selected = pk == current - if selected: - cls = item.get_short_menu_class() - new_selected_item = item - items.append((pk, shortify(unicode(item), 60), - selected, item.get_short_menu_class())) - # selected is not in owns - add it to the list - if not new_selected_item and current: - try: - item = model.objects.get(pk=int(current)) - new_selected_item = item - items.append((item.pk, shortify(unicode(item), 60), - True, item.get_short_menu_class())) - except (model.DoesNotExist, ValueError): - pass - dct['current_menu'].append((lbl, model_name, cls, items)) - current_selected_item = new_selected_item + + dct['menu'].append((lbl, model_name, current or 0, JQueryAutoComplete( + reverse('get-' + model.SLUG + '-shortcut'), + model).render( + model.SLUG + '-shortcut', value=current, + attrs={'id': 'current_' + model.SLUG}))) return render_to_response('ishtar/blocks/shortcut_menu.html', dct, context_instance=RequestContext(request)) @@ -281,6 +275,7 @@ def update_current_item(request, item_type=None, pk=None): request.session[item_type] = request.POST['value'] else: request.session[item_type] = str(pk) + request.session['SHORTCUT_SEARCH'] = 'all' currents = get_current_items(request) # reinit when descending item are not relevant @@ -507,6 +502,9 @@ def get_item(model, func_name, default_name, extra_request_keys=[], break # max right reach if force_own: own = True + if full == 'shortcut' and 'SHORTCUT_SEARCH' in request.session and \ + request.session['SHORTCUT_SEARCH'] == 'own': + own = True EMPTY = '' if 'type' in dct: data_type = dct.pop('type') @@ -535,6 +533,8 @@ def get_item(model, func_name, default_name, extra_request_keys=[], request_items = request.method == 'POST' and request.POST \ or request.GET dct = base_request.copy() + if full == 'shortcut': + dct['cached_label__icontains'] = request.GET.get('term', None) and_reqs, or_reqs = [], [] try: old = 'old' in request_items and int(request_items['old']) @@ -568,7 +568,7 @@ def get_item(model, func_name, default_name, extra_request_keys=[], q = Q(**{req_key: val}) reqs = reqs | q and_reqs.append(reqs) - if 'submited' not in request_items: + if 'submited' not in request_items and full != 'shortcut': # default search # an item is selected in the default menu if default_name in request.session and \ @@ -782,6 +782,9 @@ def get_item(model, func_name, default_name, extra_request_keys=[], pass start = (page_nb - 1) * row_nb end = page_nb * row_nb + if full == 'shortcut': + start = 0 + end = 20 items_nb = items.count() if manual_sort_key: items = items.all() @@ -896,13 +899,18 @@ def get_item(model, func_name, default_name, extra_request_keys=[], if hasattr(model, 'COL_LINK') and k in model.COL_LINK: value = link_ext_template.format(value, value) res[k] = value + if full == 'shortcut': + res['value'] = res.pop('cached_label') rows.append(res) - data = json.dumps({ - "records": items_nb, - "rows": rows, - "page": page_nb, - "total": (items_nb / row_nb + 1) if row_nb else items_nb, - }) + if full == 'shortcut': + data = json.dumps(rows) + else: + data = json.dumps({ + "records": items_nb, + "rows": rows, + "page": page_nb, + "total": (items_nb / row_nb + 1) if row_nb else items_nb, + }) return HttpResponse(data, mimetype='text/plain') elif data_type == "csv": response = HttpResponse(mimetype='text/csv') |