199 lines
5.4 KiB
JavaScript
199 lines
5.4 KiB
JavaScript
/**
|
|
* @file
|
|
* Linkit dialog functions.
|
|
*/
|
|
|
|
(function ($) {
|
|
|
|
// Create the Linkit namespaces.
|
|
Drupal.linkit = Drupal.linkit || { 'excludeIdSelectors': {} };
|
|
Drupal.linkit.currentInstance = Drupal.linkit.currentInstance || {};
|
|
Drupal.linkit.dialogHelper = Drupal.linkit.dialogHelper || {};
|
|
Drupal.linkit.insertPlugins = Drupal.linkit.insertPlugins || {};
|
|
|
|
// Exclude ids from ajax_html_ids during AJAX requests.
|
|
Drupal.linkit.excludeIdSelectors.ckeditor = ['[id^="cke_"]'];
|
|
Drupal.linkit.excludeIdSelectors.tokens = ['[id^="token-"]'];
|
|
|
|
/**
|
|
* Create the modal dialog.
|
|
*/
|
|
Drupal.linkit.createModal = function() {
|
|
// Create the modal dialog element.
|
|
Drupal.linkit.createModalElement()
|
|
// Create jQuery UI Dialog.
|
|
.dialog(Drupal.linkit.modalOptions())
|
|
// Remove the title bar from the modal.
|
|
.siblings(".ui-dialog-titlebar").hide();
|
|
|
|
// Make the modal seem "fixed".
|
|
$(window).bind("scroll resize", function() {
|
|
$('#linkit-modal').dialog('option', 'position', ['center', 50]);
|
|
});
|
|
|
|
// Get modal content.
|
|
Drupal.linkit.getDashboard();
|
|
};
|
|
|
|
/**
|
|
* Create and append the modal element.
|
|
*/
|
|
Drupal.linkit.createModalElement = function() {
|
|
// Create a new div and give it an ID of linkit-modal.
|
|
// This is the dashboard container.
|
|
var linkitModal = $('<div id="linkit-modal"></div>');
|
|
|
|
// Create a modal div in the <body>.
|
|
$('body').append(linkitModal);
|
|
|
|
return linkitModal;
|
|
};
|
|
|
|
/**
|
|
* Default jQuery dialog options used when creating the Linkit modal.
|
|
*/
|
|
Drupal.linkit.modalOptions = function() {
|
|
return {
|
|
dialogClass: 'linkit-wrapper',
|
|
modal: true,
|
|
draggable: false,
|
|
resizable: false,
|
|
width: 520,
|
|
position: ['center', 50],
|
|
minHeight: 0,
|
|
zIndex: 210000,
|
|
close: Drupal.linkit.modalClose,
|
|
open: function (event, ui) {
|
|
// Change the overlay style.
|
|
$('.ui-widget-overlay').css({
|
|
opacity: 0.5,
|
|
filter: 'Alpha(Opacity=50)',
|
|
backgroundColor: '#FFFFFF'
|
|
});
|
|
},
|
|
title: 'Linkit'
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Close the Linkit modal.
|
|
*/
|
|
Drupal.linkit.modalClose = function (e) {
|
|
$('#linkit-modal').dialog('destroy').remove();
|
|
// Make sure the current intstance settings are removed when the modal is
|
|
// closed.
|
|
Drupal.settings.linkit.currentInstance = {};
|
|
|
|
// The event object does not have a preventDefault member in
|
|
// Internet Explorer prior to version 9.
|
|
if (e && e.preventDefault) {
|
|
e.preventDefault();
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
*
|
|
*/
|
|
Drupal.linkit.getDashboard = function () {
|
|
// Create the AJAX object.
|
|
var ajax_settings = {};
|
|
ajax_settings.event = 'LinkitDashboard';
|
|
ajax_settings.url = Drupal.settings.linkit.dashboardPath + Drupal.settings.linkit.currentInstance.profile;
|
|
ajax_settings.progress = {
|
|
type: 'throbber',
|
|
message : Drupal.t('Loading Linkit dashboard...')
|
|
};
|
|
|
|
Drupal.ajax['linkit-modal'] = new Drupal.ajax('linkit-modal', $('#linkit-modal')[0], ajax_settings);
|
|
|
|
// @TODO: Jquery 1.5 accept success setting to be an array of functions.
|
|
// But we have to wait for jquery to get updated in Drupal core.
|
|
// In the meantime we have to override it.
|
|
Drupal.ajax['linkit-modal'].options.success = function (response, status) {
|
|
if (typeof response == 'string') {
|
|
response = $.parseJSON(response);
|
|
}
|
|
|
|
// Call the ajax success method.
|
|
Drupal.ajax['linkit-modal'].success(response, status);
|
|
// Run the afterInit function.
|
|
var helper = Drupal.settings.linkit.currentInstance.helper;
|
|
Drupal.linkit.getDialogHelper(helper).afterInit();
|
|
|
|
// Set focus in the search field.
|
|
$('#linkit-modal .linkit-search-element').focus();
|
|
};
|
|
|
|
// Update the autocomplete url.
|
|
Drupal.settings.linkit.currentInstance.autocompletePathParsed =
|
|
Drupal.settings.linkit.autocompletePath.replace('___profile___', Drupal.settings.linkit.currentInstance.profile);
|
|
|
|
// Trigger the ajax event.
|
|
$('#linkit-modal').trigger('LinkitDashboard');
|
|
};
|
|
|
|
/**
|
|
* Register new dialog helper.
|
|
*/
|
|
Drupal.linkit.registerDialogHelper = function(name, helper) {
|
|
Drupal.linkit.dialogHelper[name] = helper;
|
|
};
|
|
|
|
/**
|
|
* Get a dialog helper.
|
|
*
|
|
* @param {String} name
|
|
* The name of helper.
|
|
*
|
|
* @return {Object}
|
|
* Dialog helper object.
|
|
*/
|
|
Drupal.linkit.getDialogHelper = function(name) {
|
|
return Drupal.linkit.dialogHelper[name];
|
|
};
|
|
|
|
/**
|
|
* Register new insert plugins.
|
|
*/
|
|
Drupal.linkit.registerInsertPlugin = function(name, plugin) {
|
|
Drupal.linkit.insertPlugins[name] = plugin;
|
|
};
|
|
|
|
/**
|
|
* Get an insert plugin.
|
|
*/
|
|
Drupal.linkit.getInsertPlugin = function(name) {
|
|
return Drupal.linkit.insertPlugins[name];
|
|
};
|
|
|
|
var oldBeforeSerialize = (Drupal.ajax ? Drupal.ajax.prototype.beforeSerialize : false);
|
|
if (oldBeforeSerialize) {
|
|
/**
|
|
* Filter the ajax_html_ids list sent in AJAX requests.
|
|
*
|
|
* This avoids hitting like max_input_vars, which defaults to 1000,
|
|
* even with just a few active editor instances.
|
|
*/
|
|
Drupal.ajax.prototype.beforeSerialize = function (element, options) {
|
|
var ret = oldBeforeSerialize.call(this, element, options);
|
|
var excludeSelectors = [];
|
|
$.each(Drupal.linkit.excludeIdSelectors, function () {
|
|
if ($.isArray(this)) {
|
|
excludeSelectors = excludeSelectors.concat(this);
|
|
}
|
|
});
|
|
if (excludeSelectors.length > 0) {
|
|
options.data['ajax_html_ids[]'] = [];
|
|
$('[id]:not(' + excludeSelectors.join(',') + ')').each(function () {
|
|
options.data['ajax_html_ids[]'].push(this.id);
|
|
});
|
|
}
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
})(jQuery);
|