diff -u b/js/linkit.field.js b/js/linkit.field.js --- b/js/linkit.field.js +++ b/js/linkit.field.js @@ -2,38 +2,29 @@ * @file * Linkit field ui functions */ -(function ($) { - Drupal.behaviors.linkit_field = { +(function($, behavior) { + 'use strict'; + + Drupal.behaviors[behavior] = { attach: function(context, settings) { // If there is no fields, just stop here. - if (settings.linkit == undefined || settings.linkit.fields == null) { + if (undefined === settings.linkit || null === settings.linkit.fields) { return false; } - $.each(settings.linkit.fields, function(id, field) { - $('#' + id, context).once('linkit_field', function() { + $.each(settings.linkit.fields, function(i, instance) { + $('#' + instance.source, context).once(behavior, function() { var element = this; - $('.linkit-field-' + id).click(function(event) { + $('.linkit-field-' + instance.source).click(function(event) { event.preventDefault(); - var instance = { - // Set profile. - profile: field.profile, - // Set the name of the source field. - source: id, - // Set the source type. - helper: 'field', - suppressProfileChanger: true - }; - // Only care about selection if the element is a textarea. if ('textarea' === element.nodeName.toLowerCase()) { instance.selection = Drupal.linkit.getDialogHelper('field').getSelection(element); } Drupal.settings.linkit.currentInstance = instance; - // Create the modal. Drupal.linkit.createModal(); }); }); @@ -45,7 +36,6 @@ * Linkit field dialog helper. */ Drupal.linkit.registerDialogHelper('field', { - init: function() {}, afterInit: function() {}, /** @@ -55,16 +45,15 @@ * The link object. */ insertLink: function(data) { - var linkitSettings = Drupal.settings.linkit, - fieldSettings = linkitSettings.fields[linkitSettings.currentInstance.source], + var instance = Drupal.settings.linkit.currentInstance, // Call the insert plugin. - link = Drupal.linkit.getInsertPlugin(fieldSettings.insert_plugin).insert(data, fieldSettings); + link = Drupal.linkit.getInsertPlugin(instance.insertPlugin).insert(data, instance); - if (linkitSettings.currentInstance.hasOwnProperty('selection')) { + if (instance.hasOwnProperty('selection')) { // Replace the selection and insert the link there. - this.replaceSelection(linkitSettings.currentInstance.source, linkitSettings.currentInstance.selection, link); + this.replaceSelection(instance.source, instance.selection, link); } - else if (fieldSettings.hasOwnProperty('title_field')) { + else if (instance.hasOwnProperty('titleField')) { try { var $link = $(link), text = $link.text(), @@ -72,12 +61,12 @@ } catch (e) {} - this.replaceFieldValue(fieldSettings.title_field, text || link); - this.replaceFieldValue(linkitSettings.currentInstance.source, href || link); + this.replaceFieldValue(instance.titleField, text || link); + this.replaceFieldValue(instance.source, href || link); } else { // Replace the field value. - this.replaceFieldValue(linkitSettings.currentInstance.source, link); + this.replaceFieldValue(instance.source, link); } }, @@ -156 +145 @@ -})(jQuery); +})(jQuery, 'linkitField'); diff -u b/linkit.field.inc b/linkit.field.inc --- b/linkit.field.inc +++ b/linkit.field.inc @@ -202,21 +202,24 @@ // Load the insert plugin for the profile. $insert_plugin = linkit_insert_plugin_load($profile->data['insert_plugin']['plugin']); - $field_id = $element['#id']; - $settings = array( + $js_settings = array( + 'helper' => 'field', + 'source' => $element['#id'], 'profile' => $instance['settings']['linkit']['profile'], - 'url_method' => $profile->data['insert_plugin']['url_method'], - 'insert_plugin' => $profile->data['insert_plugin']['plugin'], - // @TODO: Add autocomplete settings. + 'insertPlugin' => $profile->data['insert_plugin']['plugin'], ); // Special treatment for link fields. if ('link_field' == $element['#type']) { - $field_id = $element['url']['#id']; + $js_settings['source'] = $element['url']['#id']; - // Link fields can have a title field. - if (isset($instance['settings']['title']) && in_array($instance['settings']['title'], array('optional', 'required'))) { - $settings['title_field'] = $element['title']['#id']; + // @see link_field_info() + // @see link_field_instance_settings_form() + // + // Link fields have a title field, but value could + // be changed only for those options. + if (in_array($instance['settings']['title'], array('optional', 'required'))) { + $js_settings['titleField'] = $element['title']['#id']; } } @@ -226,7 +229,7 @@ 'class' => array( "button", "linkit-field-button", - "linkit-field-$field_id", + "linkit-field-{$js_settings['source']}", ), ), )); @@ -238,7 +241,7 @@ $element['#attached']['js'][] = array( 'type' => 'setting', 'data' => array( - 'linkit' => array('fields' => array($field_id => $settings)), + 'linkit' => array('fields' => array($js_settings)), ), ); only in patch2: unchanged: --- a/js/linkit.js +++ b/js/linkit.js @@ -140,6 +140,12 @@ Drupal.linkit.registerDialogHelper = function(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];