From 0afae95e8bc33f593edc986c95b512d32dd10694 Mon Sep 17 00:00:00 2001 From: rlx Date: Wed, 13 Jan 2016 15:55:47 +0530 Subject: [PATCH] fix filter selection --- static/js/filter.js | 58 +++++++++++++++++++++++++++++++++++++++------ static/js/oml.js | 4 ++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/static/js/filter.js b/static/js/filter.js index b04100f..b1787cd 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -158,13 +158,57 @@ oml.ui.filter = function(id) { }) .bindEvent({ change: function(data) { - var isOuter = filterIndex % 4 == 0, id = data.checked[0].id; - ui.filters[filterIndex] = Ox.getObjectById(oml.config.user.ui.filters, id); - ui._filterState = oml.getFilterState(ui.find); - oml.$ui[isOuter ? 'filtersOuterPanel' : 'filtersInnerPanel'].replaceElement( - isOuter ? filterIndex / 2 : filterIndex - 1, - oml.$ui.filters[filterIndex] = oml.ui.filter(id) - ); + var id_ = data.checked[0].id, + filter_ = Ox.getObjectById(oml.config.filters, id_), + filterIndex_ = Ox.getIndexById(ui.filters, id_), + filters = Ox.clone(ui.filters), + filterState, + find; + if (filterIndex_ == -1) { + // New filter was not part of old filter set + if (ui._filterState[filterIndex].selectedLength) { + // Filter with selection gets replaced, update find + find = Ox.clone(ui.find, true); + find.conditions.splice( + ui._filterState[filterIndex].index, 1 + ); + } + filters[filterIndex] = { + id: id_, + sort: filter_.sort + }; + oml.UI.set(Ox.extend({ + filters: filters + }, find ? { + find: find + } : {})); + replaceFilter(filterIndex, id_); + } else { + // Swap two exisiting filters + filterState = Ox.clone(ui._filterState[filterIndex]); + ui._filterState[filterIndex] = ui._filterState[filterIndex_]; + ui._filterState[filterIndex_] = filterState; + filters[filterIndex] = { + id: id_, + sort: ui.filters[filterIndex_].sort + }; + filters[filterIndex_] = { + id: id, + sort: ui.filters[filterIndex].sort + }; + oml.UI.set({filters: filters}); + replaceFilter(filterIndex, id_); + replaceFilter(filterIndex_, id); + } + function replaceFilter(index, id) { + var isOuter = index % 4 == 0; + oml.$ui[ + isOuter ? 'filtersOuterPanel' : 'filtersInnerPanel' + ].replaceElement( + isOuter ? index / 2 : index - 1, + oml.$ui.filters[index] = oml.ui.filter(id) + ); + } }, click: function(data) { if (data.id == 'clearFilter') { diff --git a/static/js/oml.js b/static/js/oml.js index 2dce269..c4211c6 100644 --- a/static/js/oml.js +++ b/static/js/oml.js @@ -58,6 +58,10 @@ }).map(function(key) { return { id: key.id, + sort: [{ + key: key.id == 'date' ? 'name' : 'items', + operator: '-' + }], title: key.title, type: Ox.isArray(key.type) ? key.type[0] : key.type };