refactoring (gulp and libraries) + some fixing

This commit is contained in:
Bachir Soussi Chiadmi
2018-04-04 12:58:58 +02:00
parent c8f54cb9f1
commit 6263b674b8
31 changed files with 13519 additions and 150 deletions
@@ -0,0 +1,109 @@
(function ($, Drupal, drupalSettings) {
var settings = drupalSettings.edlp_search;
var _$body = $('body');
var _$container;
var _$form;
function init(){
// console.log('EdlpSearch Init', settings);
initEvents();
initAjax();
};
function initEvents(){
$('body')
.on('new-content-ajax-loaded', initAjax)
.on('edlp_search_search_form-col-closed', onSearchClosed);
};
function initAjax(){
_$form = $('#edlp-search-form:not(.ajax-enabled)');
if(!_$form.length) return false;
//console.log('EdlpSearch initAjaxForm()');
_$form = $('#edlp-search-form:not(.ajax-enabled)')
.on('submit', onSubmitForm)
.addClass('ajax-enabled');
_$container = _$form.parents('.row');
if(!_$container.length){
_$container = _$form.parent();
}
};
function onSubmitForm(e){
e.preventDefault();
// console.log('onSubmitForm',e);
var args = {};
// search for key words
args.keys = $('input[type="search"]', this).val();
// entries filter
args.entries = [];
$('input[type="checkbox"]:checked', '#edit-entries').each(function(index, el) {
args.entries.push($(this).val());
});
// langues
args.langues = $('input[name="langues"]', this).val();
// genres
args.genres = $('input[name="genres"]', this).val();
//console.log('EdlpSearch onSubmitForm() : args',args);
loadResults(args);
return false;
};
function loadResults(args){
//console.log('EdlpSearch loadResults() : args', args);
_$form.addClass('ajax-loading');
_$body.addClass('ajax-loading');
$('[theme="edlp_search_results"]', _$container).addClass('ajax-loading');
var path = window.location.origin + drupalSettings.path.baseUrl +settings.results_ajax_url;
$.getJSON(path, args)
.done(function(data){
onResultsLoaded(data);
})
.fail(function(jqxhr, textStatus, error){
onResultsLoadFail(jqxhr, textStatus, error);
});
};
function onResultsLoaded(data){
// console.log('EdlpSearch onResultsLoaded()', data);
_$form.removeClass('ajax-loading');
_$body.removeClass('ajax-loading');
// insert results col
$prev_results = $('[theme="edlp_search_results"]', _$container);
if($prev_results.length){
$prev_results.replaceWith(data.rendered);
}else{
_$container.append(data.rendered);
}
// trigger event
_$body.trigger({
'type':'search-results-loaded',
'results':data.results_nids
});
};
function onResultsLoadFail(jqxhr, textStatus, error){
console.warn('EdlpSearch : search results ajax load failed : '+error, jqxhr.responseText);
};
function onSearchClosed(e){
// console.log('Edlp Search onSearchClosed()');
$('div[theme="edlp_search_results"]').remove();
// trigger event
_$body.trigger({'type':'search-closed'});
}
init();
})(jQuery, Drupal, drupalSettings);
@@ -1,6 +1,7 @@
edlp_search-library:
js:
assets/js/edlp_search.js: { scope: footer }
assets/scripts/edlp_search.js: { scope: footer }
dependencies:
- core/drupalSettings
- core/jquery
- core/drupal.autocomplete
@@ -1,6 +1,6 @@
edlp_search.edlp_search_controller_searchForm:
path: '/edlp_search/searchform'
path: '/search'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchForm'
_title: 'Search Form'
@@ -8,7 +8,7 @@ edlp_search.edlp_search_controller_searchForm:
_permission: 'access content'
edlp_search.edlp_search_controller_searchForm_ajax:
path: '/edlp_search/searchform/ajax'
path: '/search/ajax'
defaults:
_controller: '\Drupal\edlp_search\Controller\EdlpSearchController::searchFormJson'
_title: 'Search Form'
@@ -73,6 +73,7 @@ class EdlpSearchController extends ControllerBase {
$rendered = render($this->renderable);
// TODO: make respponse cachable
$response = new JsonResponse();
$response->setData([
'rendered'=> $rendered,
@@ -20,7 +20,7 @@ class EdlpSearchLinkBlock extends BlockBase {
*/
public function build() {
$build = [];
$url = Url::fromRoute('edlp_search.edlp_search_controller_searchForm', [], ['absolute' => TRUE]);
$url = Url::fromRoute('edlp_search.edlp_search_controller_searchForm');
$build['edlp_search_link_block'] = array(
'#title' => "Search",
'#type' => 'link',