Skip to content

Commit

Permalink
Merge pull request #286 from romainruaud/fix_code-cleaning
Browse files Browse the repository at this point in the history
Javascript code cleaning
  • Loading branch information
afoucret authored Jan 5, 2017
2 parents 97a1dbb + 1cafe5d commit 313cfd4
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ define([
defaults: {
template: "Smile_ElasticsuiteCatalog/attribute-filter",
showMoreLabel: $.mage.__("Show more"),
showLessLabel: $.mage.__("Show less"),
showLessLabel: $.mage.__("Show less")
},

/**
* Component initialization
*/
initialize: function () {
this._super();
this.expanded = false;
Expand All @@ -41,6 +44,9 @@ define([
this.onShowLess();
},

/**
* Init the place holder
*/
initSearchPlaceholder: function () {
var examples = this.items.slice(0, 2).map(function (item) {return item.label});

Expand All @@ -50,60 +56,91 @@ define([

this.searchPlaceholder = $.mage.__('Search (%s)').replace('%s', examples.join(', '));
},


/**
* Triggered when typing on the search input
*/
onSearchChange: function () {
if (this.fulltextSearch().trim() == "") {
this.items = this.items;
if (this.fulltextSearch().trim() === "") {
this.fulltextSearch(null);
this.onShowLess();
} else {

}
},


/**
* Retrieve additional Results
*
* @param callback
*/
loadAdditionalItems: function (callback) {
$.get(this.ajaxLoadUrl, function (data) {
this.items = data;
this.hasMoreItems = false;

if (callback) {
callback();
return callback();
}
}.bind(this));
},


/**
* Retrieve items to display
*
* @returns {*}
*/
getDisplayedItems: function () {
var items = this.items;

if (this.expanded() == false) {
if (this.expanded() === false) {
items = this.items.slice(0, this.maxSize);
}

return items;
},


/**
* Callback for the "Show more" button
*/
onShowMore: function () {
if (this.hasMoreItems) {
this.loadAdditionalItems(this.onShowMore.bind(this));
} else {
this.expanded(true);
}
},


/**
* Callback for the "Show less" button
*/
onShowLess: function () {
this.expanded(false);
},


/**
* Check if the filter can be expanded
*
* @returns {boolean}
*/
enableExpansion : function () {
return this.hasMoreItems || this.items.length > this.maxSize;
},

/**
* Displays the "Show More" link
*
* @returns {*|boolean}
*/
displayShowMore: function () {
return this.enableExpansion() && this.expanded() == false && this.fulltextSearch() == null;
return this.enableExpansion() && this.expanded() === false && !this.fulltextSearch();
},


/**
* Displays the "Show Less" link
*
* @returns {*|boolean}
*/
displayShowLess: function () {
return this.enableExpansion() && this.expanded() == true && this.fulltextSearch() == null;
return this.enableExpansion() && this.expanded() === true && !this.fulltextSearch();
}
});
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
define(['underscore'], function(_) {
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/

/*jshint browser:true jquery:true*/
/*global alert*/

define(['underscore'], function(_) {
var Renderer = {
render : function (data) {
var data = data.filter(function(item) {
return item.type == "product_attribute";
data = data.filter(function(item) {
return item.type === "product_attribute";
}).map(function(item) {
return item['attribute_label']
}).reduce(function(prev, item) {
Expand All @@ -16,7 +33,7 @@ define(['underscore'], function(_) {

data = _.pairs(data).sort(function(item1, item2) {
return item2[0] - item1[0]
}).map(function(item) {return item[0]});
}).map(function(item) {return item[0]});

if (data.length > 2) {
data = data.slice(0, 2);
Expand All @@ -26,5 +43,6 @@ define(['underscore'], function(_) {
return data.join(', ');
}
}

return Renderer;
});
184 changes: 138 additions & 46 deletions src/module-elasticsuite-core/view/frontend/web/js/form-mini.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <[email protected]>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/

/*jshint browser:true jquery:true*/
/*global alert*/

define([
'ko',
'jquery',
Expand Down Expand Up @@ -307,69 +323,145 @@ define([

switch (keyCode) {
case $.ui.keyCode.HOME:
this._getFirstVisibleElement().addClass(this.options.selectClass);
this.responseList.selected = this._getFirstVisibleElement();
this._selectElement(this._getFirstVisibleElement());
break;
case $.ui.keyCode.END:
this._getLastElement().addClass(this.options.selectClass);
this.responseList.selected = this._getLastElement();
this._selectElement(this._getLastElement());
break;
case $.ui.keyCode.ESCAPE:
this._resetResponseList(true);
this.autoComplete.hide();
break;
case $.ui.keyCode.ENTER:
if (this.responseList.selected.attr('href') !== undefined) {
window.location = this.responseList.selected.attr('href');
e.preventDefault();
return false;
}
this.searchForm.trigger('submit');
this._validateElement();
break;
case $.ui.keyCode.DOWN:
if (this.responseList.indexList) {
if (!this.responseList.selected) {
this._getFirstVisibleElement().addClass(this.options.selectClass);
this.responseList.selected = this._getFirstVisibleElement();
}
else if (!this._getLastElement().hasClass(this.options.selectClass)) {
var nextElement = this.responseList.selected.next('dd');
this.responseList.selected.removeClass(this.options.selectClass);
if (nextElement.length === 0) {
nextElement = this.responseList.selected.parent('dl').next('dl').find('dd').first();
}
this.responseList.selected = nextElement.addClass(this.options.selectClass);
} else {
this.responseList.selected.removeClass(this.options.selectClass);
this._getFirstVisibleElement().addClass(this.options.selectClass);
this.responseList.selected = this._getFirstVisibleElement();
}
this.element.val(this.responseList.selected.find('.qs-option-name').text());
this.element.attr('aria-activedescendant', this.responseList.selected.attr('id'));
}
this._navigateDown();
break;
case $.ui.keyCode.UP:
if (this.responseList.indexList !== null) {
if (!this._getFirstVisibleElement().hasClass(this.options.selectClass)) {
var prevElement = this.responseList.selected.prev('dd');
this.responseList.selected.removeClass(this.options.selectClass);
if (prevElement.length === 0) {
prevElement = this.responseList.selected.parent('dl').prev('dl').find('dd').last();
}
this.responseList.selected = prevElement.addClass(this.options.selectClass);
} else {
this.responseList.selected.removeClass(this.options.selectClass);
this._getLastElement().addClass(this.options.selectClass);
this.responseList.selected = this._getLastElement();
}
this.element.val(this.responseList.selected.find('.qs-option-name').text());
this.element.attr('aria-activedescendant', this.responseList.selected.attr('id'));
}
this._navigateUp();
break;
default:
return true;
}
},

/**
* Validate selection of an element (eg : when ENTER is pressed)
*
* @returns {boolean}
*
* @private
*/
_validateElement: function() {
if (this.responseList.selected.attr('href') !== undefined) {
window.location = this.responseList.selected.attr('href');
e.preventDefault();
return false;
}
this.searchForm.trigger('submit');
},

/**
* Process down navigation on autocomplete box
*
* @private
*/
_navigateDown: function() {
if (this.responseList.indexList) {
if (!this.responseList.selected) {
this._getFirstVisibleElement().addClass(this.options.selectClass);
this.responseList.selected = this._getFirstVisibleElement();
}
else if (!this._getLastElement().hasClass(this.options.selectClass)) {
var nextElement = this._getNextElement();
this.responseList.selected.removeClass(this.options.selectClass);
this.responseList.selected = nextElement.addClass(this.options.selectClass);
} else {
this.responseList.selected.removeClass(this.options.selectClass);
this._getFirstVisibleElement().addClass(this.options.selectClass);
this.responseList.selected = this._getFirstVisibleElement();
}
this._activateElement();
}
},

/**
* Process up navigation on autocomplete box
*
* @private
*/
_navigateUp: function() {
if (this.responseList.indexList !== null) {
if (!this._getFirstVisibleElement().hasClass(this.options.selectClass)) {
var prevElement = this._getPrevElement();
this.responseList.selected.removeClass(this.options.selectClass);
this.responseList.selected = prevElement.addClass(this.options.selectClass);
} else {
this.responseList.selected.removeClass(this.options.selectClass);
this._getLastElement().addClass(this.options.selectClass);
this.responseList.selected = this._getLastElement();
}
this._activateElement();
}
},

/**
* Toggles an element as currently selected
*
* @param {Element} e - The DOM element
*
* @private
*/
_selectElement: function(element) {
element.addClass(this.options.selectClass);
this.responseList.selected = element;
},

/**
* Toggles an element as active
*
* @param {Element} e - The DOM element
*
* @private
*/
_activateElement: function() {
this.element.val(this.responseList.selected.find('.qs-option-name').text());
this.element.attr('aria-activedescendant', this.responseList.selected.attr('id'));
},

/**
* Retrieve the next element when navigating through keyboard
*
* @private
*
* @return Element
*/
_getNextElement: function() {
var nextElement = this.responseList.selected.next('dd');
if (nextElement.length === 0) {
nextElement = this.responseList.selected.parent('dl').next('dl').find('dd').first();
}

return nextElement;
},

/**
* Retrieve the previous element when navigating through keyboard
*
* @private
*
* @return Element
*/
_getPrevElement: function() {
var prevElement = this.responseList.selected.prev('dd');
this.responseList.selected.removeClass(this.options.selectClass);
if (prevElement.length === 0) {
prevElement = this.responseList.selected.parent('dl').prev('dl').find('dd').last();
}

return prevElement;
}
});

return $.smileEs.quickSearch;
Expand Down

0 comments on commit 313cfd4

Please sign in to comment.