Issue #1839130 by rballou, nod_, droplet: Refactor modules/views_ui()/js/views-admin.js.

8.0.x
webchick 2013-08-06 23:27:16 -07:00
parent 69422425d5
commit 9a77b82151
2 changed files with 617 additions and 732 deletions

View File

@ -2,43 +2,40 @@
* @file * @file
* Some basic behaviors and utility functions for Views UI. * Some basic behaviors and utility functions for Views UI.
*/ */
Drupal.viewsUi = {}; (function ($, Drupal, drupalSettings, debounce) {
Drupal.behaviors.viewsUiEditView = {}; "use strict";
Drupal.viewsUi = {};
/** /**
* Improve the user experience of the views edit interface. * Improve the user experience of the views edit interface.
*/ */
Drupal.behaviors.viewsUiEditView.attach = function (context, settings) { Drupal.behaviors.viewsUiEditView = {
attach: function () {
"use strict";
// Only show the SQL rewrite warning when the user has chosen the // Only show the SQL rewrite warning when the user has chosen the
// corresponding checkbox. // corresponding checkbox.
jQuery('#edit-query-options-disable-sql-rewrite').click(function () { $('#edit-query-options-disable-sql-rewrite').on('click', function () {
jQuery('.sql-rewrite-warning').toggleClass('js-hide'); $('.sql-rewrite-warning').toggleClass('js-hide');
}); });
}
}; };
Drupal.behaviors.viewsUiAddView = {};
/** /**
* In the add view wizard, use the view name to prepopulate form fields such as * In the add view wizard, use the view name to prepopulate form fields such as
* page title and menu link. * page title and menu link.
*/ */
Drupal.behaviors.viewsUiAddView.attach = function (context, settings) { Drupal.behaviors.viewsUiAddView = {
attach: function (context) {
"use strict"; var $context = $(context);
var $ = jQuery;
var exclude, replace, suffix;
// Set up regular expressions to allow only numbers, letters, and dashes. // Set up regular expressions to allow only numbers, letters, and dashes.
exclude = new RegExp('[^a-z0-9\\-]+', 'g'); var exclude = new RegExp('[^a-z0-9\\-]+', 'g');
replace = '-'; var replace = '-';
var suffix;
// The page title, block title, and menu link fields can all be prepopulated // The page title, block title, and menu link fields can all be prepopulated
// with the view name - no regular expression needed. // with the view name - no regular expression needed.
var $fields = $(context).find('[id^="edit-page-title"], [id^="edit-block-title"], [id^="edit-page-link-properties-title"]'); var $fields = $context.find('[id^="edit-page-title"], [id^="edit-block-title"], [id^="edit-page-link-properties-title"]');
if ($fields.length) { if ($fields.length) {
if (!this.fieldsFiller) { if (!this.fieldsFiller) {
this.fieldsFiller = new Drupal.viewsUi.FormFieldFiller($fields); this.fieldsFiller = new Drupal.viewsUi.FormFieldFiller($fields);
@ -54,7 +51,7 @@ Drupal.behaviors.viewsUiAddView.attach = function (context, settings) {
} }
// Prepopulate the path field with a URLified version of the view name. // Prepopulate the path field with a URLified version of the view name.
var $pathField = $(context).find('[id^="edit-page-path"]'); var $pathField = $context.find('[id^="edit-page-path"]');
if ($pathField.length) { if ($pathField.length) {
if (!this.pathFiller) { if (!this.pathFiller) {
this.pathFiller = new Drupal.viewsUi.FormFieldFiller($pathField, exclude, replace); this.pathFiller = new Drupal.viewsUi.FormFieldFiller($pathField, exclude, replace);
@ -66,7 +63,7 @@ Drupal.behaviors.viewsUiAddView.attach = function (context, settings) {
// Populate the RSS feed field with a URLified version of the view name, and // Populate the RSS feed field with a URLified version of the view name, and
// an .xml suffix (to make it unique). // an .xml suffix (to make it unique).
var $feedField = $(context).find('[id^="edit-page-feed-properties-path"]'); var $feedField = $context.find('[id^="edit-page-feed-properties-path"]');
if ($feedField.length) { if ($feedField.length) {
if (!this.feedFiller) { if (!this.feedFiller) {
suffix = '.xml'; suffix = '.xml';
@ -76,6 +73,7 @@ Drupal.behaviors.viewsUiAddView.attach = function (context, settings) {
this.feedFiller.rebind($feedField); this.feedFiller.rebind($feedField);
} }
} }
}
}; };
/** /**
@ -95,10 +93,6 @@ Drupal.behaviors.viewsUiAddView.attach = function (context, settings) {
* Optional. A suffix to append at the end of the target field content. * Optional. A suffix to append at the end of the target field content.
*/ */
Drupal.viewsUi.FormFieldFiller = function ($target, exclude, replace, suffix) { Drupal.viewsUi.FormFieldFiller = function ($target, exclude, replace, suffix) {
"use strict";
var $ = jQuery;
this.source = $('#edit-label'); this.source = $('#edit-label');
this.target = $target; this.target = $target;
this.exclude = exclude || false; this.exclude = exclude || false;
@ -118,108 +112,85 @@ Drupal.viewsUi.FormFieldFiller = function ($target, exclude, replace, suffix) {
// Object constructor; no return value. // Object constructor; no return value.
}; };
$.extend(Drupal.viewsUi.FormFieldFiller.prototype, {
/** /**
* Bind the form-filling behavior. * Bind the form-filling behavior.
*/ */
Drupal.viewsUi.FormFieldFiller.prototype.bind = function () { bind: function () {
"use strict";
this.unbind(); this.unbind();
// Populate the form field when the source changes. // Populate the form field when the source changes.
this.source.bind('keyup.viewsUi change.viewsUi', this.populate); this.source.on('keyup.viewsUi change.viewsUi', this.populate);
// Quit populating the field as soon as it gets focus. // Quit populating the field as soon as it gets focus.
this.target.bind('focus.viewsUi', this.unbind); this.target.on('focus.viewsUi', this.unbind);
}; },
/** /**
* Get the source form field value as altered by the passed-in parameters. * Get the source form field value as altered by the passed-in parameters.
*/ */
Drupal.viewsUi.FormFieldFiller.prototype.getTransliterated = function () { getTransliterated: function () {
"use strict";
var from = this.source.val(); var from = this.source.val();
if (this.exclude) { if (this.exclude) {
from = from.toLowerCase().replace(this.exclude, this.replace); from = from.toLowerCase().replace(this.exclude, this.replace);
} }
return from + this.suffix; return from + this.suffix;
}; },
/** /**
* Populate the target form field with the altered source field value. * Populate the target form field with the altered source field value.
*/ */
Drupal.viewsUi.FormFieldFiller.prototype._populate = function () { _populate: function () {
"use strict";
var transliterated = this.getTransliterated(); var transliterated = this.getTransliterated();
this.target.val(transliterated); this.target.val(transliterated);
}; },
/** /**
* Stop prepopulating the form fields. * Stop prepopulating the form fields.
*/ */
Drupal.viewsUi.FormFieldFiller.prototype._unbind = function () { _unbind: function () {
this.source.off('keyup.viewsUi change.viewsUi', this.populate);
"use strict"; this.target.off('focus.viewsUi', this.unbind);
},
this.source.unbind('keyup.viewsUi change.viewsUi', this.populate);
this.target.unbind('focus.viewsUi', this.unbind);
};
/** /**
* Bind event handlers to the new form fields, after they're replaced via AJAX. * Bind event handlers to the new form fields, after they're replaced via AJAX.
*/ */
Drupal.viewsUi.FormFieldFiller.prototype.rebind = function ($fields) { rebind: function ($fields) {
"use strict";
this.target = $fields; this.target = $fields;
this.bind(); this.bind();
}; }});
Drupal.behaviors.addItemForm = {};
Drupal.behaviors.addItemForm.attach = function (context) {
"use strict"; Drupal.behaviors.addItemForm = {
attach: function (context) {
var $ = jQuery;
// The add item form may have an id of views-ui-add-item-form--n. // The add item form may have an id of views-ui-add-item-form--n.
var $form = $(context).find('form[id^="views-ui-add-item-form"]').first(); var $form = $(context).find('form[id^="views-ui-add-item-form"]').first();
// Make sure we don't add more than one event handler to the same form. // Make sure we don't add more than one event handler to the same form.
$form = $form.once('views-ui-add-item-form'); $form = $form.once('views-ui-add-item-form');
if ($form.length) { if ($form.length) {
new Drupal.viewsUi.addItemForm($form); new Drupal.viewsUi.AddItemForm($form);
}
} }
}; };
Drupal.viewsUi.addItemForm = function($form) { Drupal.viewsUi.AddItemForm = function ($form) {
"use strict";
this.$form = $form; this.$form = $form;
this.$form.find('.views-filterable-options :checkbox').click(jQuery.proxy(this.handleCheck, this)); this.$form.find('.views-filterable-options :checkbox').on('click', $.proxy(this.handleCheck, this));
// Find the wrapper of the displayed text. // Find the wrapper of the displayed text.
this.$selected_div = this.$form.find('.views-selected-options').parent(); this.$selected_div = this.$form.find('.views-selected-options').parent();
this.$selected_div.hide(); this.$selected_div.hide();
this.checkedItems = []; this.checkedItems = [];
}; };
Drupal.viewsUi.addItemForm.prototype.handleCheck = function (event) { Drupal.viewsUi.AddItemForm.prototype.handleCheck = function (event) {
var $target = $(event.target);
"use strict"; var label = $.trim($target.next().text());
var $target = jQuery(event.target);
var label = jQuery.trim($target.next().text());
// Add/remove the checked item to the list. // Add/remove the checked item to the list.
if ($target.is(':checked')) { if ($target.is(':checked')) {
this.$selected_div.show(); this.$selected_div.show();
this.checkedItems.push(label); this.checkedItems.push(label);
} }
else { else {
var length = this.checkedItems.length; var position = $.inArray(label, this.checkedItems);
var position = jQuery.inArray(label, this.checkedItems);
// Delete the item from the list and take sure that the list doesn't have undefined items left. // Delete the item from the list and take sure that the list doesn't have undefined items left.
for (var i = 0; i < this.checkedItems.length; i++) { for (var i = 0; i < this.checkedItems.length; i++) {
if (i === position) { if (i === position) {
@ -239,10 +210,7 @@ Drupal.viewsUi.addItemForm.prototype.handleCheck = function (event) {
/** /**
* Refresh the display of the checked items. * Refresh the display of the checked items.
*/ */
Drupal.viewsUi.addItemForm.prototype.refreshCheckedItems = function() { Drupal.viewsUi.AddItemForm.prototype.refreshCheckedItems = function () {
"use strict";
// Perhaps we should precache the text div, too. // Perhaps we should precache the text div, too.
this.$selected_div.find('.views-selected-options').html(this.checkedItems.join(', ')); this.$selected_div.find('.views-selected-options').html(this.checkedItems.join(', '));
Drupal.viewsUi.resizeModal('', true); Drupal.viewsUi.resizeModal('', true);
@ -253,19 +221,14 @@ Drupal.viewsUi.addItemForm.prototype.refreshCheckedItems = function() {
* The following behavior detaches the <input> elements from the DOM, wraps them * The following behavior detaches the <input> elements from the DOM, wraps them
* in an unordered list, then appends them to the list of tabs. * in an unordered list, then appends them to the list of tabs.
*/ */
Drupal.behaviors.viewsUiRenderAddViewButton = {}; Drupal.behaviors.viewsUiRenderAddViewButton = {
attach: function (context) {
Drupal.behaviors.viewsUiRenderAddViewButton.attach = function (context, settings) {
"use strict";
var $ = jQuery;
// Build the add display menu and pull the display input buttons into it. // Build the add display menu and pull the display input buttons into it.
var $menu = $('#views-display-menu-tabs', context).once('views-ui-render-add-view-button-processed'); var $menu = $(context).find('#views-display-menu-tabs').once('views-ui-render-add-view-button-processed');
if (!$menu.length) { if (!$menu.length) {
return; return;
} }
var $addDisplayDropdown = $('<li class="add"><a href="#"><span class="icon add"></span>' + Drupal.t('Add') + '</a><ul class="action-list" style="display:none;"></ul></li>'); var $addDisplayDropdown = $('<li class="add"><a href="#"><span class="icon add"></span>' + Drupal.t('Add') + '</a><ul class="action-list" style="display:none;"></ul></li>');
var $displayButtons = $menu.nextAll('input.add-display').detach(); var $displayButtons = $menu.nextAll('input.add-display').detach();
$displayButtons.appendTo($addDisplayDropdown.find('.action-list')).wrap('<li>') $displayButtons.appendTo($addDisplayDropdown.find('.action-list')).wrap('<li>')
@ -283,7 +246,7 @@ Drupal.behaviors.viewsUiRenderAddViewButton.attach = function (context, settings
$addDisplayDropdown.appendTo($menu); $addDisplayDropdown.appendTo($menu);
// Add the click handler for the add display button // Add the click handler for the add display button
$('li.add > a', $menu).bind('click', function (event) { $menu.find('li.add > a').on('click', function (event) {
event.preventDefault(); event.preventDefault();
var $trigger = $(this); var $trigger = $(this);
Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger); Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger);
@ -302,6 +265,7 @@ Drupal.behaviors.viewsUiRenderAddViewButton.attach = function (context, settings
Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger); Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger);
} }
}); });
}
}; };
/** /**
@ -309,20 +273,12 @@ Drupal.behaviors.viewsUiRenderAddViewButton.attach = function (context, settings
* not written specifically for this UI, but I'm not sure where to put it. * not written specifically for this UI, but I'm not sure where to put it.
*/ */
Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu = function ($trigger) { Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu = function ($trigger) {
"use strict";
$trigger.parent().toggleClass('open'); $trigger.parent().toggleClass('open');
$trigger.next().slideToggle('fast'); $trigger.next().slideToggle('fast');
}; };
Drupal.behaviors.viewsUiSearchOptions = {}; Drupal.behaviors.viewsUiSearchOptions = {
attach: function (context) {
Drupal.behaviors.viewsUiSearchOptions.attach = function (context) {
"use strict";
var $ = jQuery;
// The add item form may have an id of views-ui-add-item-form--n. // The add item form may have an id of views-ui-add-item-form--n.
var $form = $(context).find('form[id^="views-ui-add-item-form"]').first(); var $form = $(context).find('form[id^="views-ui-add-item-form"]').first();
// Make sure we don't add more than one event handler to the same form. // Make sure we don't add more than one event handler to the same form.
@ -330,6 +286,7 @@ Drupal.behaviors.viewsUiSearchOptions.attach = function (context) {
if ($form.length) { if ($form.length) {
new Drupal.viewsUi.OptionsSearch($form); new Drupal.viewsUi.OptionsSearch($form);
} }
}
}; };
/** /**
@ -340,38 +297,32 @@ Drupal.behaviors.viewsUiSearchOptions.attach = function (context) {
* containing "taxonomy" in their label. * containing "taxonomy" in their label.
*/ */
Drupal.viewsUi.OptionsSearch = function ($form) { Drupal.viewsUi.OptionsSearch = function ($form) {
"use strict";
this.$form = $form; this.$form = $form;
// Add a keyup handler to the search box. // Add a keyup handler to the search box.
this.$searchBox = this.$form.find('#edit-override-controls-options-search'); this.$searchBox = this.$form.find('#edit-override-controls-options-search');
this.$searchBox.keyup(jQuery.proxy(this.handleKeyup, this)); this.$searchBox.keyup($.proxy(this.handleKeyup, this));
// Get a list of option labels and their corresponding divs and maintain it // Get a list of option labels and their corresponding divs and maintain it
// in memory, so we have as little overhead as possible at keyup time. // in memory, so we have as little overhead as possible at keyup time.
this.options = this.getOptions(this.$form.find('.filterable-option')); this.options = this.getOptions(this.$form.find('.filterable-option'));
// Restripe on initial loading. // Restripe on initial loading.
this.handleKeyup(); this.handleKeyup();
// Trap the ENTER key in the search box so that it doesn't submit the form. // Trap the ENTER key in the search box so that it doesn't submit the form.
this.$searchBox.keypress(function(event) { this.$searchBox.on('keypress', function (event) {
if (event.which === 13) { if (event.which === 13) {
event.preventDefault(); event.preventDefault();
} }
}); });
}; };
$.extend(Drupal.viewsUi.OptionsSearch.prototype, {
/** /**
* Assemble a list of all the filterable options on the form. * Assemble a list of all the filterable options on the form.
* *
* @param $allOptions * @param $allOptions
* A jQuery object representing the rows of filterable options to be * A $ object representing the rows of filterable options to be
* shown and hidden depending on the user's search terms. * shown and hidden depending on the user's search terms.
*/ */
Drupal.viewsUi.OptionsSearch.prototype.getOptions = function ($allOptions) { getOptions: function ($allOptions) {
"use strict";
var $ = jQuery;
var i, $label, $description, $option; var i, $label, $description, $option;
var options = []; var options = [];
var length = $allOptions.length; var length = $allOptions.length;
@ -389,15 +340,12 @@ Drupal.viewsUi.OptionsSearch.prototype.getOptions = function ($allOptions) {
}; };
} }
return options; return options;
}; },
/** /**
* Keyup handler for the search box that hides or shows the relevant options. * Keyup handler for the search box that hides or shows the relevant options.
*/ */
Drupal.viewsUi.OptionsSearch.prototype.handleKeyup = function (event) { handleKeyup: function (event) {
"use strict";
var found, i, j, option, search, words, wordsLength, zebraClass, zebraCounter; var found, i, j, option, search, words, wordsLength, zebraClass, zebraCounter;
// Determine the user's search query. The search text has been converted to // Determine the user's search query. The search text has been converted to
@ -435,26 +383,22 @@ Drupal.viewsUi.OptionsSearch.prototype.handleKeyup = function (event) {
option.$div.hide(); option.$div.hide();
} }
} }
}; }
});
Drupal.behaviors.viewsUiPreview = {};
Drupal.behaviors.viewsUiPreview.attach = function (context, settings) {
"use strict";
var $ = jQuery;
Drupal.behaviors.viewsUiPreview = {
attach: function (context) {
// Only act on the edit view form. // Only act on the edit view form.
var contextualFiltersBucket = $('.views-display-column .views-ui-display-tab-bucket.contextual-filters', context); var $contextualFiltersBucket = $(context).find('.views-display-column .views-ui-display-tab-bucket.contextual-filters');
if (contextualFiltersBucket.length === 0) { if ($contextualFiltersBucket.length === 0) {
return; return;
} }
// If the display has no contextual filters, hide the form where you enter // If the display has no contextual filters, hide the form where you enter
// the contextual filters for the live preview. If it has contextual filters, // the contextual filters for the live preview. If it has contextual filters,
// show the form. // show the form.
var contextualFilters = $('.views-display-setting a', contextualFiltersBucket); var $contextualFilters = $contextualFiltersBucket.find('.views-display-setting a');
if (contextualFilters.length) { if ($contextualFilters.length) {
$('#preview-args').parent().show(); $('#preview-args').parent().show();
} }
else { else {
@ -463,48 +407,42 @@ Drupal.behaviors.viewsUiPreview.attach = function (context, settings) {
// Executes an initial preview. // Executes an initial preview.
if ($('#edit-displays-live-preview').once('edit-displays-live-preview').is(':checked')) { if ($('#edit-displays-live-preview').once('edit-displays-live-preview').is(':checked')) {
$('#preview-submit').once('edit-displays-live-preview').click(); $('#preview-submit').once('edit-displays-live-preview').trigger('click');
}
} }
}; };
Drupal.behaviors.viewsUiRearrangeFilter = {}; Drupal.behaviors.viewsUiRearrangeFilter = {
Drupal.behaviors.viewsUiRearrangeFilter.attach = function (context, settings) { attach: function (context) {
"use strict";
var $ = jQuery;
// Only act on the rearrange filter form. // Only act on the rearrange filter form.
if (typeof Drupal.tableDrag === 'undefined' || typeof Drupal.tableDrag['views-rearrange-filters'] === 'undefined') { if (typeof Drupal.tableDrag === 'undefined' || typeof Drupal.tableDrag['views-rearrange-filters'] === 'undefined') {
return; return;
} }
var $context = $(context);
var table = $('#views-rearrange-filters', context).once('views-rearrange-filters'); var $table = $context.find('#views-rearrange-filters').once('views-rearrange-filters');
var operator = $('.form-item-filter-groups-operator', context).once('views-rearrange-filters'); var $operator = $context.find('.form-item-filter-groups-operator').once('views-rearrange-filters');
if (table.length) { if ($table.length) {
new Drupal.viewsUi.rearrangeFilterHandler(table, operator); new Drupal.viewsUi.RearrangeFilterHandler($table, $operator);
}
} }
}; };
/** /**
* Improve the UI of the rearrange filters dialog box. * Improve the UI of the rearrange filters dialog box.
*/ */
Drupal.viewsUi.rearrangeFilterHandler = function (table, operator) { Drupal.viewsUi.RearrangeFilterHandler = function ($table, $operator) {
"use strict";
var $ = jQuery;
// Keep a reference to the <table> being altered and to the div containing // Keep a reference to the <table> being altered and to the div containing
// the filter groups operator dropdown (if it exists). // the filter groups operator dropdown (if it exists).
this.table = table; this.table = $table;
this.operator = operator; this.operator = $operator;
this.hasGroupOperator = this.operator.length > 0; this.hasGroupOperator = this.operator.length > 0;
// Keep a reference to all draggable rows within the table. // Keep a reference to all draggable rows within the table.
this.draggableRows = $('.draggable', table); this.draggableRows = $table.find('.draggable');
// Keep a reference to the buttons for adding and removing filter groups. // Keep a reference to the buttons for adding and removing filter groups.
this.addGroupButton = $('input#views-add-group'); this.addGroupButton = $('input#views-add-group');
this.removeGroupButtons = $('input.views-remove-group', table); this.removeGroupButtons = $table.find('input.views-remove-group');
// Add links that duplicate the functionality of the (hidden) add and remove // Add links that duplicate the functionality of the (hidden) add and remove
// buttons. // buttons.
@ -527,9 +465,9 @@ Drupal.viewsUi.rearrangeFilterHandler = function (table, operator) {
// next to the filters in each group, and bind a handler so that they change // next to the filters in each group, and bind a handler so that they change
// based on the values of the operator dropdown within that group. // based on the values of the operator dropdown within that group.
this.redrawOperatorLabels(); this.redrawOperatorLabels();
$('.views-group-title select', table) $table.find('.views-group-title select')
.once('views-rearrange-filter-handler') .once('views-rearrange-filter-handler')
.bind('change.views-rearrange-filter-handler', $.proxy(this, 'redrawOperatorLabels')); .on('change.views-rearrange-filter-handler', $.proxy(this, 'redrawOperatorLabels'));
// Bind handlers so that when a "Remove" link is clicked, we: // Bind handlers so that when a "Remove" link is clicked, we:
// - Update the rowspans of cells containing an operator dropdown (since they // - Update the rowspans of cells containing an operator dropdown (since they
@ -537,20 +475,17 @@ Drupal.viewsUi.rearrangeFilterHandler = function (table, operator) {
// - Redraw the operator labels next to the filters in the group (since the // - Redraw the operator labels next to the filters in the group (since the
// filter that is currently displayed last in each group is not supposed to // filter that is currently displayed last in each group is not supposed to
// have a label display next to it). // have a label display next to it).
$('a.views-groups-remove-link', this.table) $table.find('a.views-groups-remove-link')
.once('views-rearrange-filter-handler') .once('views-rearrange-filter-handler')
.bind('click.views-rearrange-filter-handler', $.proxy(this, 'updateRowspans')) .on('click.views-rearrange-filter-handler', $.proxy(this, 'updateRowspans'))
.bind('click.views-rearrange-filter-handler', $.proxy(this, 'redrawOperatorLabels')); .on('click.views-rearrange-filter-handler', $.proxy(this, 'redrawOperatorLabels'));
}; };
$.extend(Drupal.viewsUi.RearrangeFilterHandler.prototype, {
/** /**
* Insert links that allow filter groups to be added and removed. * Insert links that allow filter groups to be added and removed.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.insertAddRemoveFilterGroupLinks = function () { insertAddRemoveFilterGroupLinks: function () {
"use strict";
var $ = jQuery;
// Insert a link for adding a new group at the top of the page, and make it // Insert a link for adding a new group at the top of the page, and make it
// match the action links styling used in a typical page.tpl.php. Note that // match the action links styling used in a typical page.tpl.php. Note that
@ -561,7 +496,7 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.insertAddRemoveFilterGroupLinks
// When the link is clicked, dynamically click the hidden form button for // When the link is clicked, dynamically click the hidden form button for
// adding a new filter group. // adding a new filter group.
.once('views-rearrange-filter-handler') .once('views-rearrange-filter-handler')
.bind('click.views-rearrange-filter-handler', $.proxy(this, 'clickAddGroupButton')); .on('click.views-rearrange-filter-handler', $.proxy(this, 'clickAddGroupButton'));
// Find each (visually hidden) button for removing a filter group and insert // Find each (visually hidden) button for removing a filter group and insert
// a link next to it. // a link next to it.
@ -575,24 +510,22 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.insertAddRemoveFilterGroupLinks
// When the link is clicked, dynamically click the corresponding form // When the link is clicked, dynamically click the corresponding form
// button. // button.
.once('views-rearrange-filter-handler') .once('views-rearrange-filter-handler')
.bind('click.views-rearrange-filter-handler', {buttonId: buttonId}, $.proxy(this, 'clickRemoveGroupButton')); .on('click.views-rearrange-filter-handler', { buttonId: buttonId }, $.proxy(this, 'clickRemoveGroupButton'));
} }
}; },
/** /**
* Dynamically click the button that adds a new filter group. * Dynamically click the button that adds a new filter group.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.clickAddGroupButton = function () { clickAddGroupButton: function () {
"use strict";
// Due to conflicts between Drupal core's AJAX system and the Views AJAX // Due to conflicts between Drupal core's AJAX system and the Views AJAX
// system, the only way to get this to work seems to be to trigger both the // system, the only way to get this to work seems to be to trigger both the
// .mousedown() and .submit() events. // .mousedown() and .submit() events.
this.addGroupButton.mousedown(); this.addGroupButton
this.addGroupButton.submit(); .trigger('mousedown')
.trigger('submit');
event.preventDefault(); event.preventDefault();
}; },
/** /**
* Dynamically click a button for removing a filter group. * Dynamically click a button for removing a filter group.
@ -601,32 +534,33 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.clickAddGroupButton = function (
* Event being triggered, with event.data.buttonId set to the ID of the * Event being triggered, with event.data.buttonId set to the ID of the
* form button that should be clicked. * form button that should be clicked.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.clickRemoveGroupButton = function (event) { clickRemoveGroupButton: function (event) {
"use strict";
// For some reason, here we only need to trigger .submit(), unlike for // For some reason, here we only need to trigger .submit(), unlike for
// Drupal.viewsUi.rearrangeFilterHandler.prototype.clickAddGroupButton() // Drupal.viewsUi.RearrangeFilterHandler.prototype.clickAddGroupButton()
// where we had to trigger .mousedown() also. // where we had to trigger .mousedown() also.
jQuery('input#' + event.data.buttonId, this.table).submit(); this.table.find('#' + event.data.buttonId).trigger('submit');
event.preventDefault(); event.preventDefault();
}; },
/** /**
* Move the groups operator so that it's between the first two groups, and * Move the groups operator so that it's between the first two groups, and
* duplicate it between any subsequent groups. * duplicate it between any subsequent groups.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.duplicateGroupsOperator = function () { duplicateGroupsOperator: function () {
var dropdowns, newRow, titleRow;
"use strict"; var titleRows = $('tr.views-group-title');
var $ = jQuery;
var dropdowns, newRow;
var titleRows = $('tr.views-group-title'), titleRow;
// Get rid of the explanatory text around the operator; its placement is // Get rid of the explanatory text around the operator; its placement is
// explanatory enough. // explanatory enough.
this.operator.find('label').add('div.description').addClass('element-invisible');
this.operator.find('select').addClass('form-select');
// Keep a list of the operator dropdowns, so we can sync their behavior later.
dropdowns = this.operator;
// Move the operator to a new row just above the second group.
titleRow = $('tr#views-group-title-2');
this.operator.find('label').add('div.description').addClass('visually-hidden'); this.operator.find('label').add('div.description').addClass('visually-hidden');
this.operator.find('select').addClass('form-select'); this.operator.find('select').addClass('form-select');
@ -653,44 +587,34 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.duplicateGroupsOperator = functi
} }
return dropdowns; return dropdowns;
}; },
/** /**
* Make the duplicated groups operators change in sync with each other. * Make the duplicated groups operators change in sync with each other.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.syncGroupsOperators = function () { syncGroupsOperators: function () {
"use strict";
if (this.dropdowns.length < 2) { if (this.dropdowns.length < 2) {
// We only have one dropdown (or none at all), so there's nothing to sync. // We only have one dropdown (or none at all), so there's nothing to sync.
return; return;
} }
this.dropdowns.change(jQuery.proxy(this, 'operatorChangeHandler')); this.dropdowns.on('change', $.proxy(this, 'operatorChangeHandler'));
}; },
/** /**
* Click handler for the operators that appear between filter groups. * Click handler for the operators that appear between filter groups.
* *
* Forces all operator dropdowns to have the same value. * Forces all operator dropdowns to have the same value.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.operatorChangeHandler = function (event) { operatorChangeHandler: function (event) {
"use strict";
var $ = jQuery;
var $target = $(event.target); var $target = $(event.target);
var operators = this.dropdowns.find('select').not($target); var operators = this.dropdowns.find('select').not($target);
// Change the other operators to match this new value. // Change the other operators to match this new value.
operators.val($target.val()); operators.val($target.val());
}; },
Drupal.viewsUi.rearrangeFilterHandler.prototype.modifyTableDrag = function () {
"use strict";
modifyTableDrag: function () {
var tableDrag = Drupal.tableDrag['views-rearrange-filters']; var tableDrag = Drupal.tableDrag['views-rearrange-filters'];
var filterHandler = this; var filterHandler = this;
@ -710,7 +634,7 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.modifyTableDrag = function () {
// Make sure the row that just got moved (this.group) is inside one of // Make sure the row that just got moved (this.group) is inside one of
// the filter groups (i.e. below an empty marker row or a draggable). If // the filter groups (i.e. below an empty marker row or a draggable). If
// it isn't, move it down one. // it isn't, move it down one.
var thisRow = jQuery(this.group); var thisRow = $(this.group);
var previousRow = thisRow.prev('tr'); var previousRow = thisRow.prev('tr');
if (previousRow.length && !previousRow.hasClass('group-message') && !previousRow.hasClass('draggable')) { if (previousRow.length && !previousRow.hasClass('group-message') && !previousRow.hasClass('draggable')) {
// Move the dragged row down one. // Move the dragged row down one.
@ -730,8 +654,6 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.modifyTableDrag = function () {
* Override the onDrop method from tabledrag.js. * Override the onDrop method from tabledrag.js.
*/ */
tableDrag.onDrop = function () { tableDrag.onDrop = function () {
var $ = jQuery;
// If the tabledrag change marker (i.e., the "*") has been inserted inside // If the tabledrag change marker (i.e., the "*") has been inserted inside
// a row after the operator label (i.e., "And" or "Or") rearrange the items // a row after the operator label (i.e., "And" or "Or") rearrange the items
// so the operator label continues to appear last. // so the operator label continues to appear last.
@ -757,22 +679,17 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.modifyTableDrag = function () {
groupField.val(groupName); groupField.val(groupName);
} }
}; };
}; },
/** /**
* Redraw the operator labels that are displayed next to each filter. * Redraw the operator labels that are displayed next to each filter.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.redrawOperatorLabels = function () { redrawOperatorLabels: function () {
for (var i = 0; i < this.draggableRows.length; i++) {
"use strict";
var $ = jQuery;
var i;
for (i = 0; i < this.draggableRows.length; i++) {
// Within the row, the operator labels are displayed inside the first table // Within the row, the operator labels are displayed inside the first table
// cell (next to the filter name). // cell (next to the filter name).
var $draggableRow = $(this.draggableRows[i]); var $draggableRow = $(this.draggableRows[i]);
var $firstCell = $('td:first', $draggableRow); var $firstCell = $draggableRow.find('td:first');
if ($firstCell.length) { if ($firstCell.length) {
// The value of the operator label ("And" or "Or") is taken from the // The value of the operator label ("And" or "Or") is taken from the
// first operator dropdown we encounter, going backwards from the current // first operator dropdown we encounter, going backwards from the current
@ -806,16 +723,12 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.redrawOperatorLabels = function
} }
} }
} }
}; },
/** /**
* Update the rowspan attribute of each cell containing an operator dropdown. * Update the rowspan attribute of each cell containing an operator dropdown.
*/ */
Drupal.viewsUi.rearrangeFilterHandler.prototype.updateRowspans = function () { updateRowspans: function () {
"use strict";
var $ = jQuery;
var i, $row, $currentEmptyRow, draggableCount, $operatorCell; var i, $row, $currentEmptyRow, draggableCount, $operatorCell;
var rows = $(this.table).find('tr'); var rows = $(this.table).find('tr');
var length = rows.length; var length = rows.length;
@ -824,7 +737,7 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.updateRowspans = function () {
if ($row.hasClass('views-group-title')) { if ($row.hasClass('views-group-title')) {
// This row is a title row. // This row is a title row.
// Keep a reference to the cell containing the dropdown operator. // Keep a reference to the cell containing the dropdown operator.
$operatorCell = $($row.find('td.group-operator')); $operatorCell = $row.find('td.group-operator');
// Assume this filter group is empty, until we find otherwise. // Assume this filter group is empty, until we find otherwise.
draggableCount = 0; draggableCount = 0;
$currentEmptyRow = $row.next('tr'); $currentEmptyRow = $row.next('tr');
@ -833,7 +746,7 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.updateRowspans = function () {
// the "this group is empty" row. // the "this group is empty" row.
$operatorCell.attr('rowspan', 2); $operatorCell.attr('rowspan', 2);
} }
else if (($row).hasClass('draggable') && $row.is(':visible')) { else if ($row.hasClass('draggable') && $row.is(':visible')) {
// We've found a visible filter row, so we now know the group isn't empty. // We've found a visible filter row, so we now know the group isn't empty.
draggableCount++; draggableCount++;
$currentEmptyRow.removeClass('group-empty').addClass('group-populated'); $currentEmptyRow.removeClass('group-empty').addClass('group-populated');
@ -841,24 +754,19 @@ Drupal.viewsUi.rearrangeFilterHandler.prototype.updateRowspans = function () {
$operatorCell.attr('rowspan', draggableCount + 1); $operatorCell.attr('rowspan', draggableCount + 1);
} }
} }
}; }});
Drupal.behaviors.viewsFilterConfigSelectAll = {};
/** /**
* Add a select all checkbox, which checks each checkbox at once. * Add a select all checkbox, which checks each checkbox at once.
*/ */
Drupal.behaviors.viewsFilterConfigSelectAll.attach = function(context) { Drupal.behaviors.viewsFilterConfigSelectAll = {
attach: function (context) {
"use strict";
var $ = jQuery;
// Show the select all checkbox. // Show the select all checkbox.
$('#views-ui-config-item-form div.form-item-options-value-all', context).once(function() { $(context).find('#views-ui-config-item-form div.form-item-options-value-all').once('filterConfigSelectAll')
$(this).show(); .show()
})
.find('input[type=checkbox]') .find('input[type=checkbox]')
.click(function() { .on('click', function () {
var checked = $(this).is(':checked'); var checked = $(this).is(':checked');
// Update all checkbox beside the select all checkbox. // Update all checkbox beside the select all checkbox.
$(this).parents('.form-checkboxes').find('input[type=checkbox]').each(function () { $(this).parents('.form-checkboxes').find('input[type=checkbox]').each(function () {
@ -866,57 +774,49 @@ Drupal.behaviors.viewsFilterConfigSelectAll.attach = function(context) {
}); });
}); });
// Uncheck the select all checkbox if any of the others are unchecked. // Uncheck the select all checkbox if any of the others are unchecked.
$('#views-ui-config-item-form div.form-type-checkbox').not($('.form-item-options-value-all')).find('input[type=checkbox]').each(function() { $('#views-ui-config-item-form').find('div.form-type-checkbox').not($('.form-item-options-value-all'))
$(this).click(function() { .find('input[type=checkbox]')
.on('click', function () {
if ($(this).is('checked') === false) { if ($(this).is('checked') === false) {
$('#edit-options-value-all').prop('checked', false); $('#edit-options-value-all').prop('checked', false);
} }
}); });
}); }
}; };
/** /**
* Remove icon class from elements that are themed as buttons or dropbuttons. * Remove icon class from elements that are themed as buttons or dropbuttons.
*/ */
Drupal.behaviors.viewsRemoveIconClass = {}; Drupal.behaviors.viewsRemoveIconClass = {
Drupal.behaviors.viewsRemoveIconClass.attach = function (context, settings) { attach: function (context) {
$(context).find('.dropbutton').once('dropbutton-icon', function () {
"use strict"; $(this).find('.icon').removeClass('icon');
jQuery(context).find('.dropbutton').once('dropbutton-icon', function () {
jQuery(this).find('.icon').removeClass('icon');
}); });
}
}; };
/** /**
* Change "Expose filter" buttons into checkboxes. * Change "Expose filter" buttons into checkboxes.
*/ */
Drupal.behaviors.viewsUiCheckboxify = {}; Drupal.behaviors.viewsUiCheckboxify = {
Drupal.behaviors.viewsUiCheckboxify.attach = function (context, settings) { attach: function (context, settings) {
"use strict";
var $ = jQuery;
var $buttons = $('#edit-options-expose-button-button, #edit-options-group-button-button').once('views-ui-checkboxify'); var $buttons = $('#edit-options-expose-button-button, #edit-options-group-button-button').once('views-ui-checkboxify');
var length = $buttons.length; var length = $buttons.length;
var i; var i;
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {
new Drupal.viewsUi.Checkboxifier($buttons[i]); new Drupal.viewsUi.Checkboxifier($buttons[i]);
} }
}
}; };
/** /**
* Change the default widget to select the default group according to the * Change the default widget to select the default group according to the
* selected widget for the exposed group. * selected widget for the exposed group.
*/ */
Drupal.behaviors.viewsUiChangeDefaultWidget = {}; Drupal.behaviors.viewsUiChangeDefaultWidget = {
Drupal.behaviors.viewsUiChangeDefaultWidget.attach = function (context, settings) { attach: function () {
function changeDefaultWidget (event) {
"use strict"; if ($(event.target).prop('checked')) {
var $ = jQuery;
function change_default_widget(multiple) {
if (multiple) {
$('input.default-radios').hide(); $('input.default-radios').hide();
$('td.any-default-radios-row').parent().hide(); $('td.any-default-radios-row').parent().hide();
$('input.default-checkboxes').show(); $('input.default-checkboxes').show();
@ -928,11 +828,11 @@ Drupal.behaviors.viewsUiChangeDefaultWidget.attach = function (context, settings
} }
} }
// Update on widget change. // Update on widget change.
$('input[name="options[group_info][multiple]"]').change(function() { $('input[name="options[group_info][multiple]"]')
change_default_widget($(this).attr("checked")); .on('change', changeDefaultWidget)
});
// Update the first time the form is rendered. // Update the first time the form is rendered.
$('input[name="options[group_info][multiple]"]').trigger('change'); .trigger('change');
}
}; };
/** /**
@ -942,10 +842,6 @@ Drupal.behaviors.viewsUiChangeDefaultWidget.attach = function (context, settings
* The DOM object representing the button to be checkboxified. * The DOM object representing the button to be checkboxified.
*/ */
Drupal.viewsUi.Checkboxifier = function (button) { Drupal.viewsUi.Checkboxifier = function (button) {
"use strict";
var $ = jQuery;
this.$button = $(button); this.$button = $(button);
this.$parent = this.$button.parent('div.views-expose, div.views-grouped'); this.$parent = this.$button.parent('div.views-expose, div.views-grouped');
this.$input = this.$parent.find('input:checkbox, input:radio'); this.$input = this.$parent.find('input:checkbox, input:radio');
@ -961,9 +857,6 @@ Drupal.viewsUi.Checkboxifier = function (button) {
* When the checkbox is checked or unchecked, simulate a button press. * When the checkbox is checked or unchecked, simulate a button press.
*/ */
Drupal.viewsUi.Checkboxifier.prototype.clickHandler = function (e) { Drupal.viewsUi.Checkboxifier.prototype.clickHandler = function (e) {
"use strict";
this.$button.mousedown(); this.$button.mousedown();
this.$button.submit(); this.$button.submit();
}; };
@ -971,28 +864,25 @@ Drupal.viewsUi.Checkboxifier.prototype.clickHandler = function (e) {
/** /**
* Change the Apply button text based upon the override select state. * Change the Apply button text based upon the override select state.
*/ */
Drupal.behaviors.viewsUiOverrideSelect = {}; Drupal.behaviors.viewsUiOverrideSelect = {
Drupal.behaviors.viewsUiOverrideSelect.attach = function (context, settings) { attach: function (context) {
$(context).find('#edit-override-dropdown').once('views-ui-override-button-text', function () {
"use strict";
var $ = jQuery;
$('#edit-override-dropdown', context).once('views-ui-override-button-text', function() {
// Closures! :( // Closures! :(
var $submit = $('#edit-submit', context); var $submit = $('#edit-submit');
var old_value = $submit.val(); var old_value = $submit.val();
$submit.once('views-ui-override-button-text') $submit.once('views-ui-override-button-text')
.bind('mouseup', function() { .on('mouseup', function () {
$(this).val(old_value); $(this).val(old_value);
return true; return true;
}); });
$(this).bind('change', function() { $(this).on('change', function () {
if ($(this).val() === 'default') { var $this = $(this);
if ($this.val() === 'default') {
$submit.val(Drupal.t('Apply (all displays)')); $submit.val(Drupal.t('Apply (all displays)'));
} }
else if ($(this).val() === 'default_revert') { else if ($this.val() === 'default_revert') {
$submit.val(Drupal.t('Revert to default')); $submit.val(Drupal.t('Revert to default'));
} }
else { else {
@ -1002,21 +892,20 @@ Drupal.behaviors.viewsUiOverrideSelect.attach = function (context, settings) {
.trigger('change'); .trigger('change');
}); });
}
}; };
Drupal.viewsUi.resizeModal = function (e, no_shrink) { Drupal.viewsUi.resizeModal = function (e, no_shrink) {
"use strict";
var $ = jQuery;
var $modal = $('.views-ui-dialog'); var $modal = $('.views-ui-dialog');
var $scroll = $('.scroll', $modal); var $window = $(window);
var windowWidth = $window.width();
var $scroll = $modal.find('.scroll');
if ($modal.size() === 0 || $modal.css('display') === 'none') { if ($modal.size() === 0 || $modal.css('display') === 'none') {
return; return;
} }
var maxWidth = parseInt($(window).width() * .85); // 70% of window var maxWidth = parseInt(windowWidth * 0.85, 10); // 85% of window
var minWidth = parseInt($(window).width() * .6); // 70% of window var minWidth = parseInt(windowWidth * 0.6, 10); // 60% of window
// Set the modal to the minwidth so that our width calculation of // Set the modal to the minwidth so that our width calculation of
// children works. // children works.
@ -1024,7 +913,7 @@ Drupal.viewsUi.resizeModal = function (e, no_shrink) {
var width = minWidth; var width = minWidth;
// Don't let the window get more than 80% of the display high. // Don't let the window get more than 80% of the display high.
var maxHeight = parseInt($(window).height() * .8); var maxHeight = parseInt($window.height() * 0.8, 10);
var minHeight = 200; var minHeight = 200;
if (no_shrink) { if (no_shrink) {
minHeight = $modal.height(); minHeight = $modal.height();
@ -1039,8 +928,8 @@ Drupal.viewsUi.resizeModal = function (e, no_shrink) {
// Calculate the height of the 'scroll' region. // Calculate the height of the 'scroll' region.
var scrollHeight = 0; var scrollHeight = 0;
scrollHeight += parseInt($scroll.css('padding-top')); scrollHeight += parseInt($scroll.css('padding-top'), 10);
scrollHeight += parseInt($scroll.css('padding-bottom')); scrollHeight += parseInt($scroll.css('padding-bottom'), 10);
$scroll.children().each(function () { $scroll.children().each(function () {
var w = $(this).innerWidth(); var w = $(this).innerWidth();
@ -1054,8 +943,8 @@ Drupal.viewsUi.resizeModal = function (e, no_shrink) {
// will be. // will be.
var difference = 0; var difference = 0;
difference += parseInt($scroll.css('padding-top')); difference += parseInt($scroll.css('padding-top'), 10);
difference += parseInt($scroll.css('padding-bottom')); difference += parseInt($scroll.css('padding-bottom'), 10);
difference += $('.views-override').outerHeight(true); difference += $('.views-override').outerHeight(true);
difference += $('.views-messages').outerHeight(true); difference += $('.views-messages').outerHeight(true);
difference += $('#views-ajax-title').outerHeight(true); difference += $('#views-ajax-title').outerHeight(true);
@ -1098,34 +987,29 @@ Drupal.viewsUi.resizeModal = function (e, no_shrink) {
}; };
Drupal.behaviors.viewsUiHandlerRemoveLink = {}; Drupal.behaviors.viewsUiHandlerRemoveLink = {
Drupal.behaviors.viewsUiHandlerRemoveLink.attach = function(context) { attach: function (context) {
var $ = jQuery; var $context = $(context);
// Handle handler deletion by looking for the hidden checkbox and hiding the // Handle handler deletion by looking for the hidden checkbox and hiding the
// row. // row.
$('a.views-remove-link', context).once('views-processed').click(function(event) { $context.find('a.views-remove-link').once('views').on('click', function (event) {
var id = $(this).attr('id').replace('views-remove-link-', ''); var id = $(this).attr('id').replace('views-remove-link-', '');
$('#views-row-' + id, context).hide(); $context.find('#views-row-' + id).hide();
$('#views-removed-' + id, context).attr('checked', true); $context.find('#views-removed-' + id).prop('checked', true);
event.preventDefault(); event.preventDefault();
}); });
// Handle display deletion by looking for the hidden checkbox and hiding the // Handle display deletion by looking for the hidden checkbox and hiding the
// row. // row.
$('a.display-remove-link', context).once('display').click(function(event) { $context.find('a.display-remove-link').once('display').on('click', function (event) {
var id = $(this).attr('id').replace('display-remove-link-', ''); var id = $(this).attr('id').replace('display-remove-link-', '');
$('#display-row-' + id, context).hide(); $context.find('#display-row-' + id).hide();
$('#display-removed-' + id, context).attr('checked', true); $context.find('#display-removed-' + id).prop('checked', true);
event.preventDefault(); event.preventDefault();
}); });
}
}; };
jQuery(function() { $(window).on('resize scroll', debounce(Drupal.viewsUi.resizeModal, 100));
"use strict" })(jQuery, Drupal, drupalSettings, Drupal.debounce);
jQuery(window).bind('resize', Drupal.viewsUi.resizeModal);
jQuery(window).bind('scroll', Drupal.viewsUi.resizeModal);
});

View File

@ -201,6 +201,7 @@ function views_ui_library_info() {
array('system', 'jquery'), array('system', 'jquery'),
array('system', 'drupal'), array('system', 'drupal'),
array('system', 'drupalSettings'), array('system', 'drupalSettings'),
array('system', 'drupal.debounce'),
array('system', 'jquery.once'), array('system', 'jquery.once'),
array('system', 'jquery.form'), array('system', 'jquery.form'),
array('system', 'drupal.ajax'), array('system', 'drupal.ajax'),