mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-21 01:28:47 +02:00
search widget : widget search history
SVN:b1162[5562]
This commit is contained in:
@@ -52,6 +52,7 @@ class NiceWebPage extends WebPage
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.positionBy.js');
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.popupmenu.js');
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/search/search_form_handler.js');
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/search/search_form_handler_history.js');
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/search/search_form_criteria.js');
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/search/search_form_criteria_raw.js');
|
||||
$this->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/search/search_form_criteria_string.js');
|
||||
|
||||
@@ -24,6 +24,7 @@ $(function()
|
||||
},
|
||||
'search': {
|
||||
'base_oql': '',
|
||||
'class_name': null,
|
||||
'criterion': [
|
||||
// Structure
|
||||
// {
|
||||
@@ -90,6 +91,9 @@ $(function()
|
||||
|
||||
this.element.addClass('search_form_handler');
|
||||
|
||||
//init others widgets :
|
||||
this.element.search_form_handler_history({"itop_root_class":me.options.search.class_name});
|
||||
|
||||
// Prepare DOM elements
|
||||
this._prepareFormArea();
|
||||
this._prepareCriterionArea();
|
||||
@@ -337,11 +341,12 @@ $(function()
|
||||
.text(Dict.S('UI:Search:AddCriteria:List:RecentlyUsed:Title'))
|
||||
.appendTo(oRecentsElem);
|
||||
|
||||
// TODO: Add elements and remove hidden class if there is some.
|
||||
var oRecentsItemsElem = $('<ul class="sfl_items"></ul>')
|
||||
.append('<li class="sfl_i_placeholder">' + Dict.S('UI:Search:AddCriteria:List:RecentlyUsed:Placeholder') + '</li>')
|
||||
.appendTo(oRecentsElem);
|
||||
|
||||
me._refreshRecentlyUsed();
|
||||
|
||||
// - Search zlist list
|
||||
var oZlistElem = $('<div></div>')
|
||||
.addClass('sf_list')
|
||||
@@ -357,13 +362,8 @@ $(function()
|
||||
|
||||
for(var sFieldRef in this.options.search.fields.zlist)
|
||||
{
|
||||
var oField = this.options.search.fields.zlist[sFieldRef];
|
||||
var sFieldTitleAttr = (oField.description !== undefined) ? 'title="' + oField.description + '"' : '';
|
||||
var oFieldElem = $('<li></li>')
|
||||
.addClass('sfm_field')
|
||||
.attr('data-field-ref', sFieldRef)
|
||||
.append('<label ' + sFieldTitleAttr + '><input type="checkbox" value="' + sFieldRef + '" />' + oField.label + '</label>')
|
||||
.appendTo(oZListItemsElem);
|
||||
var oFieldElem = me._getHtmlLiFromFieldRef(me, sFieldRef, ['zlist']);
|
||||
oFieldElem.appendTo(oZListItemsElem);
|
||||
}
|
||||
|
||||
// - Remaining fields list
|
||||
@@ -383,13 +383,8 @@ $(function()
|
||||
|
||||
for(var sFieldRef in this.options.search.fields.others)
|
||||
{
|
||||
var oField = this.options.search.fields.others[sFieldRef];
|
||||
var sFieldTitleAttr = (oField.description !== undefined) ? 'title="' + oField.description + '"' : '';
|
||||
var oFieldElem = $('<li></li>')
|
||||
.addClass('sfm_field')
|
||||
.attr('data-field-ref', sFieldRef)
|
||||
.append('<label ' + sFieldTitleAttr + '><input type="checkbox" value="' + sFieldRef + '" />' + oField.label + '</label>')
|
||||
.appendTo(oOthersItemsElem);
|
||||
var oFieldElem = me._getHtmlLiFromFieldRef(me, sFieldRef, ['others']);
|
||||
oFieldElem.appendTo(oOthersItemsElem);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -504,7 +499,7 @@ $(function()
|
||||
});
|
||||
|
||||
// - Add one criteria
|
||||
this.elements.more_criterion.find('.sfm_field').on('click', function(oEvent){
|
||||
this.elements.more_criterion.on('click', '.sfm_field', function(oEvent){
|
||||
// Prevent anchor
|
||||
oEvent.preventDefault();
|
||||
// Prevent propagation to not close the opening criteria
|
||||
@@ -513,13 +508,17 @@ $(function()
|
||||
// If no checkbox checked, add criteria right away, otherwise we "queue" it we other checkboxed.
|
||||
if(me.elements.more_criterion.find('.sfm_field input[type="checkbox"]:checked').length === 0)
|
||||
{
|
||||
var sFieldRef = $(this).attr('data-field-ref');
|
||||
|
||||
// Prepare new criterion data (as already opened to increase UX)
|
||||
var oData = {
|
||||
'ref': $(this).attr('data-field-ref'),
|
||||
'ref': sFieldRef,
|
||||
'init_opened': (oEvent.ctrlKey) ? false : true,
|
||||
};
|
||||
|
||||
// Add criteria but don't submit form as the user has not specified the value yet.
|
||||
me.element.search_form_handler_history('setLatest', sFieldRef);
|
||||
me._refreshRecentlyUsed();
|
||||
me._addCriteria(oData);
|
||||
}
|
||||
else
|
||||
@@ -529,7 +528,7 @@ $(function()
|
||||
});
|
||||
|
||||
// - Add several criterion
|
||||
this.elements.more_criterion.find('.sfm_field input[type="checkbox"]').on('click', function(oEvent){
|
||||
this.elements.more_criterion.on('click', '.sfm_field input[type="checkbox"]', function(oEvent){
|
||||
// Prevent propagation to field and instant add of the criteria
|
||||
oEvent.stopPropagation();
|
||||
|
||||
@@ -550,12 +549,17 @@ $(function()
|
||||
if($(this).attr('name') === 'apply')
|
||||
{
|
||||
me.elements.more_criterion.find('.sfm_field input[type="checkbox"]:checked').each(function(iIdx, oElem){
|
||||
var sFieldRef = $(oElem).closest('.sfm_field').attr('data-field-ref');
|
||||
var oData = {
|
||||
'ref': $(oElem).closest('.sfm_field').attr('data-field-ref'),
|
||||
'ref': sFieldRef,
|
||||
'init_opened': false,
|
||||
};
|
||||
|
||||
me.element.search_form_handler_history('setLatest', sFieldRef);
|
||||
me._addCriteria(oData);
|
||||
});
|
||||
|
||||
me._refreshRecentlyUsed();
|
||||
me._closeMoreCriterion();
|
||||
}
|
||||
|
||||
@@ -571,6 +575,66 @@ $(function()
|
||||
oButtonsElem.hide();
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* "add new criteria" <li /> markup
|
||||
* - with checkbox, label, data-* ...
|
||||
* - without event binding
|
||||
*
|
||||
* @private
|
||||
*
|
||||
* @param sFieldRef
|
||||
* @param aFieldCollectionsEligible
|
||||
*
|
||||
* @return jQuery detached <li />
|
||||
*/
|
||||
_getHtmlLiFromFieldRef: function(me, sFieldRef, aFieldCollectionsEligible) {
|
||||
var oFieldElem = undefined;
|
||||
|
||||
aFieldCollectionsEligible.forEach(function (sFieldCollection) {
|
||||
if (typeof me.options.search.fields[sFieldCollection][sFieldRef] == 'undefined')
|
||||
{
|
||||
return true;//if this field is not present in the Collection, let's try the next
|
||||
}
|
||||
|
||||
var oField = me.options.search.fields[sFieldCollection][sFieldRef];
|
||||
var sFieldTitleAttr = (oField.description !== undefined) ? 'title="' + oField.description + '"' : '';
|
||||
oFieldElem = $('<li></li>')
|
||||
.addClass('sfm_field')
|
||||
.attr('data-field-ref', sFieldRef)
|
||||
.append('<label ' + sFieldTitleAttr + '><input type="checkbox" value="' + sFieldRef + '" />' + oField.label + '</label>')
|
||||
});
|
||||
|
||||
if (undefined == oFieldElem)
|
||||
{
|
||||
me._trace('no sFieldRef in given collection', {"sFieldRef":sFieldRef, "aFieldCollectionsEligible":aFieldCollectionsEligible});
|
||||
return $('<!-- no sFieldRef in given collection -->');
|
||||
}
|
||||
|
||||
return oFieldElem;
|
||||
},
|
||||
|
||||
_refreshRecentlyUsed: function()
|
||||
{
|
||||
me = this;
|
||||
|
||||
var aHistory = me.element.search_form_handler_history("getHistory");
|
||||
var oRecentsItemsElem = me.element.find('.sf_list_recents .sfl_items');
|
||||
|
||||
if (aHistory.length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
oRecentsItemsElem.empty();
|
||||
|
||||
|
||||
aHistory.forEach(function(sFieldRef) {
|
||||
var oFieldElem = me._getHtmlLiFromFieldRef(me, sFieldRef, ['zlist', 'others']);
|
||||
oRecentsItemsElem.append(oFieldElem);
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
// - Prepare results area
|
||||
_prepareResultsArea: function()
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*
|
||||
* $(cssSelector).search_form_handler_history({"itop_root_class":"FooBarClass"}) : constructor
|
||||
* $(cssSelector).search_form_handler_history("getHistory") : history array getter
|
||||
* $(cssSelector).search_form_handler_history("historyUnshift", "field.ref") : prepend the field ref to the beginning of the history array
|
||||
* $(cssSelector).search_form_handler_history("setLatest", "field.ref") : prepend the field ref to the beginning of the history array
|
||||
*
|
||||
*
|
||||
* please take a look at the options for custom constructor values
|
||||
@@ -37,12 +37,31 @@ $(function()
|
||||
|
||||
iStoreHistoryTimeoutHandler : undefined,
|
||||
|
||||
/**
|
||||
* the constructor, called by the widget factory
|
||||
* @private
|
||||
*/
|
||||
_create: function()
|
||||
{
|
||||
var me = this;
|
||||
|
||||
if (me.options.preference_code == undefined)
|
||||
{
|
||||
me.options.preference_code = me.options.itop_root_class + '__search_history';
|
||||
}
|
||||
|
||||
|
||||
me.aHistory = JSON.parse(GetUserPreference(me._getPreferenceCode(), "[]"));
|
||||
},
|
||||
|
||||
getHistory: function()
|
||||
{
|
||||
var me = this;
|
||||
|
||||
return me.aHistory;
|
||||
},
|
||||
|
||||
historyUnshift: function(sFieldRef)
|
||||
setLatest: function(sFieldRef)
|
||||
{
|
||||
var me = this;
|
||||
|
||||
@@ -60,7 +79,7 @@ $(function()
|
||||
var iDeleteCount = me.aHistory.length - me.options.history_max_length;
|
||||
if (iDeleteCount > 0)
|
||||
{
|
||||
me.aHistory.splice(0, iDeleteCount);
|
||||
me.aHistory.splice(-iDeleteCount, iDeleteCount);
|
||||
}
|
||||
|
||||
//store it in the backend (with a delay in the hope to wait long enough to make bulk modifications
|
||||
@@ -70,22 +89,7 @@ $(function()
|
||||
return;
|
||||
},
|
||||
|
||||
/**
|
||||
* the constructor, called by the widget factory
|
||||
* @private
|
||||
*/
|
||||
_create: function()
|
||||
{
|
||||
var me = this;
|
||||
|
||||
if (me.options.preference_code == undefined)
|
||||
{
|
||||
me.options.preference_code = me.options.itop_root_class + '|search_history';
|
||||
}
|
||||
|
||||
|
||||
me.aHistory = GetUserPreference(me._getPreferenceCode(), []);
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns String
|
||||
@@ -93,12 +97,12 @@ $(function()
|
||||
*/
|
||||
_getPreferenceCode: function()
|
||||
{
|
||||
return me.options.preference_code;
|
||||
return this.options.preference_code;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* should only be called by historyUnshift in order to store the updated history
|
||||
* should only be called by setLatest in order to store the updated history
|
||||
* @private
|
||||
*/
|
||||
_storeHistory: function()
|
||||
@@ -110,18 +114,16 @@ $(function()
|
||||
clearTimeout(me.iStoreHistoryTimeoutHandler);
|
||||
}
|
||||
|
||||
me.iStoreHistoryTimeoutHandler = setTimeout(me._storeHistoryTimeoutFunction, me.options.history_backend_store_timeout);
|
||||
me.iStoreHistoryTimeoutHandler = setTimeout(me._storeHistoryTimeoutFunction(me), me.options.history_backend_store_timeout);
|
||||
},
|
||||
|
||||
/**
|
||||
* should only be called by _storeHistory using a timeout
|
||||
* @private
|
||||
*/
|
||||
_storeHistoryTimeoutFunction: function()
|
||||
_storeHistoryTimeoutFunction: function(me)
|
||||
{
|
||||
var me = this;
|
||||
|
||||
SetUserPreference(me._getPreferenceCode(), me.getHistory(), true);
|
||||
SetUserPreference(me._getPreferenceCode(), JSON.stringify(me.getHistory()), true);
|
||||
|
||||
me.iStoreHistoryTimeoutHandler = undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user