Skip to content
Snippets Groups Projects

Make the AJAX request on-demand.

Merged catch requested to merge issue/coffee-3494208:3494208-ajax-request-on-demand into 2.x
1 file
+ 90
81
Compare changes
  • Side-by-side
  • Inline
+ 90
81
@@ -67,90 +67,9 @@
.wrapInner('<div id="coffee-form-inner" />')
.appendTo(DrupalCoffee.wrapper);
// Load autocomplete data set, consider implementing
// caching with local storage.
DrupalCoffee.dataset = [];
DrupalCoffee.isItemSelected = false;
var autocomplete_data_element = 'ui-autocomplete';
var url;
if (drupalSettings.coffee.dataPath) {
url = drupalSettings.coffee.dataPath;
}
else {
url = Drupal.url('admin/coffee/get-data');
}
$.ajax({
url: url,
dataType: 'json',
success: function (data) {
DrupalCoffee.dataset = data;
// Apply autocomplete plugin on show.
var $autocomplete = $(DrupalCoffee.field).autocomplete({
source: DrupalCoffee.dataset,
focus: function (event, ui) {
// Prevents replacing the value of the input field.
DrupalCoffee.isItemSelected = true;
event.preventDefault();
},
change: function (event, ui) {
DrupalCoffee.isItemSelected = false;
},
select: function (event, ui) {
DrupalCoffee.redirect(ui.item.value, event.metaKey || event.ctrlKey);
event.preventDefault();
return false;
},
delay: 0,
appendTo: DrupalCoffee.results
});
$autocomplete.data(autocomplete_data_element)._renderItem = function (ul, item) {
// Strip the basePath when displaying the link description.
var description = item.value;
if (item.value.indexOf(drupalSettings.path.basePath) === 0) {
description = item.value.substring(drupalSettings.path.basePath.length);
}
return $('<li></li>')
.data('item.autocomplete', item)
.append('<a>' + item.label + '<small class="description">' + description + '</small></a>')
.appendTo(ul);
};
// We want to limit the number of results.
$(DrupalCoffee.field).data(autocomplete_data_element)._renderMenu = function (ul, items) {
var self = this;
items = items.slice(0, drupalSettings.coffee.maxResults);
$.each(items, function (index, item) {
self._renderItemData(ul, item);
});
};
DrupalCoffee.form.keydown(function (event) {
if (event.keyCode === 13) {
var openInNewWindow = false;
if (event.metaKey || event.ctrlKey) {
openInNewWindow = true;
}
if (!DrupalCoffee.isItemSelected) {
var $firstItem = $(DrupalCoffee.results).find('li:first').data('item.autocomplete');
if (typeof $firstItem === 'object') {
DrupalCoffee.redirect($firstItem.value, openInNewWindow);
event.preventDefault();
}
}
}
});
},
error: function () {
DrupalCoffee.field.val('Could not load data, please refresh the page');
}
});
$('.toolbar-icon-coffee').click(function (event) {
event.preventDefault();
DrupalCoffee.coffee_show();
@@ -178,12 +97,102 @@
}
};
/**
* Initializes the autocomplete widget with data.
*/
DrupalCoffee.coffee_initialize_search_box = function () {
// Only do this once per page request to allow for opening and closing the
// dialog multiple times.
if (DrupalCoffee.dataset.length !== 0) {
return;
}
var autocomplete_data_element = 'ui-autocomplete';
var url;
if (drupalSettings.coffee.dataPath) {
url = drupalSettings.coffee.dataPath;
}
else {
url = Drupal.url('admin/coffee/get-data');
}
$.ajax({
url: url,
dataType: 'json',
success: function (data) {
DrupalCoffee.dataset = data;
// Apply autocomplete plugin on show.
var $autocomplete = $(DrupalCoffee.field).autocomplete({
source: DrupalCoffee.dataset,
focus: function (event, ui) {
// Prevents replacing the value of the input field.
DrupalCoffee.isItemSelected = true;
event.preventDefault();
},
change: function (event, ui) {
DrupalCoffee.isItemSelected = false;
},
select: function (event, ui) {
DrupalCoffee.redirect(ui.item.value, event.metaKey || event.ctrlKey);
event.preventDefault();
return false;
},
delay: 0,
appendTo: DrupalCoffee.results
});
$autocomplete.data(autocomplete_data_element)._renderItem = function (ul, item) {
// Strip the basePath when displaying the link description.
var description = item.value;
if (item.value.indexOf(drupalSettings.path.basePath) === 0) {
description = item.value.substring(drupalSettings.path.basePath.length);
}
return $('<li></li>')
.data('item.autocomplete', item)
.append('<a>' + item.label + '<small class="description">' + description + '</small></a>')
.appendTo(ul);
};
// We want to limit the number of results.
$(DrupalCoffee.field).data(autocomplete_data_element)._renderMenu = function (ul, items) {
var self = this;
items = items.slice(0, drupalSettings.coffee.maxResults);
$.each(items, function (index, item) {
self._renderItemData(ul, item);
});
};
DrupalCoffee.form.keydown(function (event) {
if (event.keyCode === 13) {
var openInNewWindow = false;
if (event.metaKey || event.ctrlKey) {
openInNewWindow = true;
}
if (!DrupalCoffee.isItemSelected) {
var $firstItem = $(DrupalCoffee.results).find('li:first').data('item.autocomplete');
if (typeof $firstItem === 'object') {
DrupalCoffee.redirect($firstItem.value, openInNewWindow);
event.preventDefault();
}
}
}
});
},
error: function () {
DrupalCoffee.field.val('Could not load data, please refresh the page');
}
});
}
// Prefix the open and close functions to avoid
// conflicts with autocomplete plugin.
/**
* Open the form and focus on the search field.
*/
DrupalCoffee.coffee_show = function () {
DrupalCoffee.coffee_initialize_search_box();
DrupalCoffee.wrapper.removeClass('hide-form');
DrupalCoffee.bg.show();
DrupalCoffee.field.focus();
Loading