views-admin.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /**
  2. * @file
  3. * Some basic behaviors and utility functions for Views UI.
  4. */
  5. Drupal.viewsUi = {};
  6. Drupal.behaviors.viewsUiEditView = {};
  7. /**
  8. * Improve the user experience of the views edit interface.
  9. */
  10. Drupal.behaviors.viewsUiEditView.attach = function (context, settings) {
  11. // Only show the SQL rewrite warning when the user has chosen the
  12. // corresponding checkbox.
  13. jQuery('#edit-query-options-disable-sql-rewrite').click(function () {
  14. jQuery('.sql-rewrite-warning').toggleClass('js-hide');
  15. });
  16. };
  17. Drupal.behaviors.viewsUiAddView = {};
  18. /**
  19. * In the add view wizard, use the view name to prepopulate form fields such as
  20. * page title and menu link.
  21. */
  22. Drupal.behaviors.viewsUiAddView.attach = function (context, settings) {
  23. var $ = jQuery;
  24. var exclude, replace, suffix;
  25. // Set up regular expressions to allow only numbers, letters, and dashes.
  26. exclude = new RegExp('[^a-z0-9\\-]+', 'g');
  27. replace = '-';
  28. // The page title, block title, and menu link fields can all be prepopulated
  29. // with the view name - no regular expression needed.
  30. var $fields = $(context).find('[id^="edit-page-title"], [id^="edit-block-title"], [id^="edit-page-link-properties-title"]');
  31. if ($fields.length) {
  32. if (!this.fieldsFiller) {
  33. this.fieldsFiller = new Drupal.viewsUi.FormFieldFiller($fields);
  34. }
  35. else {
  36. // After an AJAX response, this.fieldsFiller will still have event
  37. // handlers bound to the old version of the form fields (which don't exist
  38. // anymore). The event handlers need to be unbound and then rebound to the
  39. // new markup. Note that jQuery.live is difficult to make work in this
  40. // case because the IDs of the form fields change on every AJAX response.
  41. this.fieldsFiller.rebind($fields);
  42. }
  43. }
  44. // Prepopulate the path field with a URLified version of the view name.
  45. var $pathField = $(context).find('[id^="edit-page-path"]');
  46. if ($pathField.length) {
  47. if (!this.pathFiller) {
  48. this.pathFiller = new Drupal.viewsUi.FormFieldFiller($pathField, exclude, replace);
  49. }
  50. else {
  51. this.pathFiller.rebind($pathField);
  52. }
  53. }
  54. // Populate the RSS feed field with a URLified version of the view name, and
  55. // an .xml suffix (to make it unique).
  56. var $feedField = $(context).find('[id^="edit-page-feed-properties-path"]');
  57. if ($feedField.length) {
  58. if (!this.feedFiller) {
  59. suffix = '.xml';
  60. this.feedFiller = new Drupal.viewsUi.FormFieldFiller($feedField, exclude, replace, suffix);
  61. }
  62. else {
  63. this.feedFiller.rebind($feedField);
  64. }
  65. }
  66. };
  67. /**
  68. * Constructor for the Drupal.viewsUi.FormFieldFiller object.
  69. *
  70. * Prepopulates a form field based on the view name.
  71. *
  72. * @param $target
  73. * A jQuery object representing the form field to prepopulate.
  74. * @param exclude
  75. * Optional. A regular expression representing characters to exclude from the
  76. * target field.
  77. * @param replace
  78. * Optional. A string to use as the replacement value for disallowed
  79. * characters.
  80. * @param suffix
  81. * Optional. A suffix to append at the end of the target field content.
  82. */
  83. Drupal.viewsUi.FormFieldFiller = function ($target, exclude, replace, suffix) {
  84. var $ = jQuery;
  85. this.source = $('#edit-human-name');
  86. this.target = $target;
  87. this.exclude = exclude || false;
  88. this.replace = replace || '';
  89. this.suffix = suffix || '';
  90. // Create bound versions of this instance's object methods to use as event
  91. // handlers. This will let us easily unbind those specific handlers later on.
  92. // NOTE: jQuery.proxy will not work for this because it assumes we want only
  93. // one bound version of an object method, whereas we need one version per
  94. // object instance.
  95. var self = this;
  96. this.populate = function () {return self._populate.call(self);};
  97. this.unbind = function () {return self._unbind.call(self);};
  98. this.bind();
  99. // Object constructor; no return value.
  100. };
  101. /**
  102. * Bind the form-filling behavior.
  103. */
  104. Drupal.viewsUi.FormFieldFiller.prototype.bind = function () {
  105. this.unbind();
  106. // Populate the form field when the source changes.
  107. this.source.bind('keyup.viewsUi change.viewsUi', this.populate);
  108. // Quit populating the field as soon as it gets focus.
  109. this.target.bind('focus.viewsUi', this.unbind);
  110. };
  111. /**
  112. * Get the source form field value as altered by the passed-in parameters.
  113. */
  114. Drupal.viewsUi.FormFieldFiller.prototype.getTransliterated = function () {
  115. var from = this.source.val();
  116. if (this.exclude) {
  117. from = from.toLowerCase().replace(this.exclude, this.replace);
  118. }
  119. return from + this.suffix;
  120. };
  121. /**
  122. * Populate the target form field with the altered source field value.
  123. */
  124. Drupal.viewsUi.FormFieldFiller.prototype._populate = function () {
  125. var transliterated = this.getTransliterated();
  126. this.target.val(transliterated);
  127. };
  128. /**
  129. * Stop prepopulating the form fields.
  130. */
  131. Drupal.viewsUi.FormFieldFiller.prototype._unbind = function () {
  132. this.source.unbind('keyup.viewsUi change.viewsUi', this.populate);
  133. this.target.unbind('focus.viewsUi', this.unbind);
  134. };
  135. /**
  136. * Bind event handlers to the new form fields, after they're replaced via AJAX.
  137. */
  138. Drupal.viewsUi.FormFieldFiller.prototype.rebind = function ($fields) {
  139. this.target = $fields;
  140. this.bind();
  141. }
  142. Drupal.behaviors.addItemForm = {};
  143. Drupal.behaviors.addItemForm.attach = function (context) {
  144. var $ = jQuery;
  145. // The add item form may have an id of views-ui-add-item-form--n.
  146. var $form = $(context).find('form[id^="views-ui-add-item-form"]').first();
  147. // Make sure we don't add more than one event handler to the same form.
  148. $form = $form.once('views-ui-add-item-form');
  149. if ($form.length) {
  150. new Drupal.viewsUi.addItemForm($form);
  151. }
  152. }
  153. Drupal.viewsUi.addItemForm = function($form) {
  154. this.$form = $form;
  155. this.$form.find('.views-filterable-options :checkbox').click(jQuery.proxy(this.handleCheck, this));
  156. // Find the wrapper of the displayed text.
  157. this.$selected_div = this.$form.find('.views-selected-options').parent();
  158. this.$selected_div.hide();
  159. this.checkedItems = [];
  160. }
  161. Drupal.viewsUi.addItemForm.prototype.handleCheck = function (event) {
  162. var $target = jQuery(event.target);
  163. var label = jQuery.trim($target.next().text());
  164. // Add/remove the checked item to the list.
  165. if ($target.is(':checked')) {
  166. this.$selected_div.show();
  167. this.checkedItems.push(label);
  168. }
  169. else {
  170. var length = this.checkedItems.length;
  171. var position = jQuery.inArray(label, this.checkedItems);
  172. // Delete the item from the list and take sure that the list doesn't have
  173. // undefined items left.
  174. for (var i = 0; i < this.checkedItems.length; i++) {
  175. if (i == position) {
  176. this.checkedItems.splice(i, 1);
  177. i--;
  178. break;
  179. }
  180. }
  181. // Hide it again if none item is selected.
  182. if (this.checkedItems.length == 0) {
  183. this.$selected_div.hide();
  184. }
  185. }
  186. this.refreshCheckedItems();
  187. }
  188. /**
  189. * Refresh the display of the checked items.
  190. */
  191. Drupal.viewsUi.addItemForm.prototype.refreshCheckedItems = function() {
  192. // Perhaps we should precache the text div, too.
  193. this.$selected_div.find('.views-selected-options').html(Drupal.checkPlain(this.checkedItems.join(', ')));
  194. Drupal.viewsUi.resizeModal('', true);
  195. }
  196. /**
  197. * The input field items that add displays must be rendered as <input> elements.
  198. * The following behavior detaches the <input> elements from the DOM, wraps them
  199. * in an unordered list, then appends them to the list of tabs.
  200. */
  201. Drupal.behaviors.viewsUiRenderAddViewButton = {};
  202. Drupal.behaviors.viewsUiRenderAddViewButton.attach = function (context, settings) {
  203. var $ = jQuery;
  204. // Build the add display menu and pull the display input buttons into it.
  205. var $menu = $('#views-display-menu-tabs', context).once('views-ui-render-add-view-button-processed');
  206. if (!$menu.length) {
  207. return;
  208. }
  209. 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>');
  210. var $displayButtons = $menu.nextAll('input.add-display').detach();
  211. $displayButtons.appendTo($addDisplayDropdown.find('.action-list')).wrap('<li>')
  212. .parent().first().addClass('first').end().last().addClass('last');
  213. // Remove the 'Add ' prefix from the button labels since they're being palced
  214. // in an 'Add' dropdown.
  215. // @todo This assumes English, but so does $addDisplayDropdown above. Add
  216. // support for translation.
  217. $displayButtons.each(function () {
  218. var label = $(this).val();
  219. if (label.substr(0, 4) == 'Add ') {
  220. $(this).val(label.substr(4));
  221. }
  222. });
  223. $addDisplayDropdown.appendTo($menu);
  224. // Add the click handler for the add display button.
  225. $('li.add > a', $menu).bind('click', function (event) {
  226. event.preventDefault();
  227. var $trigger = $(this);
  228. Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger);
  229. });
  230. // Add a mouseleave handler to close the dropdown when the user mouses
  231. // away from the item. We use mouseleave instead of mouseout because
  232. // the user is going to trigger mouseout when she moves from the trigger
  233. // link to the sub menu items.
  234. //
  235. // We use the 'li.add' selector because the open class on this item will be
  236. // toggled on and off and we want the handler to take effect in the cases
  237. // that the class is present, but not when it isn't.
  238. $menu.delegate('li.add', 'mouseleave', function (event) {
  239. var $this = $(this);
  240. var $trigger = $this.children('a[href="#"]');
  241. if ($this.children('.action-list').is(':visible')) {
  242. Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger);
  243. }
  244. });
  245. };
  246. /**
  247. * @note [@jessebeach] I feel like the following should be a more generic function and
  248. * not written specifically for this UI, but I'm not sure where to put it.
  249. */
  250. Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu = function ($trigger) {
  251. $trigger.parent().toggleClass('open');
  252. $trigger.next().slideToggle('fast');
  253. }
  254. Drupal.behaviors.viewsUiSearchOptions = {};
  255. Drupal.behaviors.viewsUiSearchOptions.attach = function (context) {
  256. var $ = jQuery;
  257. // The add item form may have an id of views-ui-add-item-form--n.
  258. var $form = $(context).find('form[id^="views-ui-add-item-form"]').first();
  259. // Make sure we don't add more than one event handler to the same form.
  260. $form = $form.once('views-ui-filter-options');
  261. if ($form.length) {
  262. new Drupal.viewsUi.OptionsSearch($form);
  263. }
  264. };
  265. /**
  266. * Constructor for the viewsUi.OptionsSearch object.
  267. *
  268. * The OptionsSearch object filters the available options on a form according
  269. * to the user's search term. Typing in "taxonomy" will show only those options
  270. * containing "taxonomy" in their label.
  271. */
  272. Drupal.viewsUi.OptionsSearch = function ($form) {
  273. this.$form = $form;
  274. // Add a keyup handler to the search box.
  275. this.$searchBox = this.$form.find('#edit-options-search');
  276. this.$searchBox.keyup(jQuery.proxy(this.handleKeyup, this));
  277. // Get a list of option labels and their corresponding divs and maintain it
  278. // in memory, so we have as little overhead as possible at keyup time.
  279. this.options = this.getOptions(this.$form.find('.filterable-option'));
  280. // Restripe on initial loading.
  281. this.handleKeyup();
  282. // Trap the ENTER key in the search box so that it doesn't submit the form.
  283. this.$searchBox.keypress(function(event) {
  284. if (event.which == 13) {
  285. event.preventDefault();
  286. }
  287. });
  288. };
  289. /**
  290. * Assemble a list of all the filterable options on the form.
  291. *
  292. * @param $allOptions
  293. * A jQuery object representing the rows of filterable options to be
  294. * shown and hidden depending on the user's search terms.
  295. */
  296. Drupal.viewsUi.OptionsSearch.prototype.getOptions = function ($allOptions) {
  297. var $ = jQuery;
  298. var i, $label, $description, $option;
  299. var options = [];
  300. var length = $allOptions.length;
  301. for (i = 0; i < length; i++) {
  302. $option = $($allOptions[i]);
  303. $label = $option.find('label');
  304. $description = $option.find('div.description');
  305. options[i] = {
  306. // Search on the lowercase version of the label text + description.
  307. 'searchText': $label.text().toLowerCase() + " " + $description.text().toLowerCase(),
  308. // Maintain a reference to the jQuery object for each row, so we don't
  309. // have to create a new object inside the performance-sensitive keyup
  310. // handler.
  311. '$div': $option
  312. }
  313. }
  314. return options;
  315. };
  316. /**
  317. * Keyup handler for the search box that hides or shows the relevant options.
  318. */
  319. Drupal.viewsUi.OptionsSearch.prototype.handleKeyup = function (event) {
  320. var found, i, j, option, search, words, wordsLength, zebraClass, zebraCounter;
  321. // Determine the user's search query. The search text has been converted to
  322. // lowercase.
  323. search = (this.$searchBox.val() || '').toLowerCase();
  324. words = search.split(' ');
  325. wordsLength = words.length;
  326. // Start the counter for restriping rows.
  327. zebraCounter = 0;
  328. // Search through the search texts in the form for matching text.
  329. var length = this.options.length;
  330. for (i = 0; i < length; i++) {
  331. // Use a local variable for the option being searched, for performance.
  332. option = this.options[i];
  333. found = true;
  334. // Each word in the search string has to match the item in order for the
  335. // item to be shown.
  336. for (j = 0; j < wordsLength; j++) {
  337. if (option.searchText.indexOf(words[j]) === -1) {
  338. found = false;
  339. }
  340. }
  341. if (found) {
  342. // Show the checkbox row, and restripe it.
  343. zebraClass = (zebraCounter % 2) ? 'odd' : 'even';
  344. option.$div.show();
  345. option.$div.removeClass('even odd');
  346. option.$div.addClass(zebraClass);
  347. zebraCounter++;
  348. }
  349. else {
  350. // The search string wasn't found; hide this item.
  351. option.$div.hide();
  352. }
  353. }
  354. };
  355. Drupal.behaviors.viewsUiPreview = {};
  356. Drupal.behaviors.viewsUiPreview.attach = function (context, settings) {
  357. var $ = jQuery;
  358. // Only act on the edit view form.
  359. var contextualFiltersBucket = $('.views-display-column .views-ui-display-tab-bucket.contextual-filters', context);
  360. if (contextualFiltersBucket.length == 0) {
  361. return;
  362. }
  363. // If the display has no contextual filters, hide the form where you enter
  364. // the contextual filters for the live preview. If it has contextual filters,
  365. // show the form.
  366. var contextualFilters = $('.views-display-setting a', contextualFiltersBucket);
  367. if (contextualFilters.length) {
  368. $('#preview-args').parent().show();
  369. }
  370. else {
  371. $('#preview-args').parent().hide();
  372. }
  373. // Executes an initial preview.
  374. if ($('#edit-displays-live-preview').once('edit-displays-live-preview').is(':checked')) {
  375. $('#preview-submit').once('edit-displays-live-preview').click();
  376. }
  377. };
  378. Drupal.behaviors.viewsUiRearrangeFilter = {};
  379. Drupal.behaviors.viewsUiRearrangeFilter.attach = function (context, settings) {
  380. var $ = jQuery;
  381. // Only act on the rearrange filter form.
  382. if (typeof Drupal.tableDrag == 'undefined' || typeof Drupal.tableDrag['views-rearrange-filters'] == 'undefined') {
  383. return;
  384. }
  385. var table = $('#views-rearrange-filters', context).once('views-rearrange-filters');
  386. var operator = $('.form-item-filter-groups-operator', context).once('views-rearrange-filters');
  387. if (table.length) {
  388. new Drupal.viewsUi.rearrangeFilterHandler(table, operator);
  389. }
  390. };
  391. /**
  392. * Improve the UI of the rearrange filters dialog box.
  393. */
  394. Drupal.viewsUi.rearrangeFilterHandler = function (table, operator) {
  395. var $ = jQuery;
  396. // Keep a reference to the <table> being altered and to the div containing
  397. // the filter groups operator dropdown (if it exists).
  398. this.table = table;
  399. this.operator = operator;
  400. this.hasGroupOperator = this.operator.length > 0;
  401. // Keep a reference to all draggable rows within the table.
  402. this.draggableRows = $('.draggable', table);
  403. // Keep a reference to the buttons for adding and removing filter groups.
  404. this.addGroupButton = $('input#views-add-group');
  405. this.removeGroupButtons = $('input.views-remove-group', table);
  406. // Add links that duplicate the functionality of the (hidden) add and remove
  407. // buttons.
  408. this.insertAddRemoveFilterGroupLinks();
  409. // When there is a filter groups operator dropdown on the page, create
  410. // duplicates of the dropdown between each pair of filter groups.
  411. if (this.hasGroupOperator) {
  412. this.dropdowns = this.duplicateGroupsOperator();
  413. this.syncGroupsOperators();
  414. }
  415. // Add methods to the tableDrag instance to account for operator cells (which
  416. // span multiple rows), the operator labels next to each filter (e.g., "And"
  417. // or "Or"), the filter groups, and other special aspects of this tableDrag
  418. // instance.
  419. this.modifyTableDrag();
  420. // Initialize the operator labels (e.g., "And" or "Or") that are displayed
  421. // next to the filters in each group, and bind a handler so that they change
  422. // based on the values of the operator dropdown within that group.
  423. this.redrawOperatorLabels();
  424. $('.views-group-title select', table)
  425. .once('views-rearrange-filter-handler')
  426. .bind('change.views-rearrange-filter-handler', $.proxy(this, 'redrawOperatorLabels'));
  427. // Bind handlers so that when a "Remove" link is clicked, we:
  428. // - Update the rowspans of cells containing an operator dropdown (since they
  429. // need to change to reflect the number of rows in each group).
  430. // - Redraw the operator labels next to the filters in the group (since the
  431. // filter that is currently displayed last in each group is not supposed to
  432. // have a label display next to it).
  433. $('a.views-groups-remove-link', this.table)
  434. .once('views-rearrange-filter-handler')
  435. .bind('click.views-rearrange-filter-handler', $.proxy(this, 'updateRowspans'))
  436. .bind('click.views-rearrange-filter-handler', $.proxy(this, 'redrawOperatorLabels'));
  437. };
  438. /**
  439. * Insert links that allow filter groups to be added and removed.
  440. */
  441. Drupal.viewsUi.rearrangeFilterHandler.prototype.insertAddRemoveFilterGroupLinks = function () {
  442. var $ = jQuery;
  443. // Insert a link for adding a new group at the top of the page, and make it
  444. // match the action links styling used in a typical page.tpl.php. Note that
  445. // Drupal does not provide a theme function for this markup, so this is the
  446. // best we can do.
  447. $('<ul class="action-links"><li><a id="views-add-group-link" href="#">' + this.addGroupButton.val() + '</a></li></ul>')
  448. .prependTo(this.table.parent())
  449. // When the link is clicked, dynamically click the hidden form button for
  450. // adding a new filter group.
  451. .once('views-rearrange-filter-handler')
  452. .bind('click.views-rearrange-filter-handler', $.proxy(this, 'clickAddGroupButton'));
  453. // Find each (visually hidden) button for removing a filter group and insert
  454. // a link next to it.
  455. var length = this.removeGroupButtons.length;
  456. for (i = 0; i < length; i++) {
  457. var $removeGroupButton = $(this.removeGroupButtons[i]);
  458. var buttonId = $removeGroupButton.attr('id');
  459. $('<a href="#" class="views-remove-group-link">' + Drupal.t('Remove group') + '</a>')
  460. .insertBefore($removeGroupButton)
  461. // When the link is clicked, dynamically click the corresponding form
  462. // button.
  463. .once('views-rearrange-filter-handler')
  464. .bind('click.views-rearrange-filter-handler', {buttonId: buttonId}, $.proxy(this, 'clickRemoveGroupButton'));
  465. }
  466. };
  467. /**
  468. * Dynamically click the button that adds a new filter group.
  469. */
  470. Drupal.viewsUi.rearrangeFilterHandler.prototype.clickAddGroupButton = function () {
  471. // Due to conflicts between Drupal core's AJAX system and the Views AJAX
  472. // system, the only way to get this to work seems to be to trigger both the
  473. // .mousedown() and .submit() events.
  474. this.addGroupButton.mousedown();
  475. this.addGroupButton.submit();
  476. return false;
  477. };
  478. /**
  479. * Dynamically click a button for removing a filter group.
  480. *
  481. * @param event
  482. * Event being triggered, with event.data.buttonId set to the ID of the
  483. * form button that should be clicked.
  484. */
  485. Drupal.viewsUi.rearrangeFilterHandler.prototype.clickRemoveGroupButton = function (event) {
  486. // For some reason, here we only need to trigger .submit(), unlike for
  487. // Drupal.viewsUi.rearrangeFilterHandler.prototype.clickAddGroupButton()
  488. // where we had to trigger .mousedown() also.
  489. jQuery('input#' + event.data.buttonId, this.table).submit();
  490. return false;
  491. };
  492. /**
  493. * Move the groups operator so that it's between the first two groups, and
  494. * duplicate it between any subsequent groups.
  495. */
  496. Drupal.viewsUi.rearrangeFilterHandler.prototype.duplicateGroupsOperator = function () {
  497. var $ = jQuery;
  498. var dropdowns, newRow;
  499. var titleRows = $('tr.views-group-title'), titleRow;
  500. // Get rid of the explanatory text around the operator; its placement is
  501. // explanatory enough.
  502. this.operator.find('label').add('div.description').addClass('element-invisible');
  503. this.operator.find('select').addClass('form-select');
  504. // Keep a list of the operator dropdowns, so we can sync their behavior later.
  505. dropdowns = this.operator;
  506. // Move the operator to a new row just above the second group.
  507. titleRow = $('tr#views-group-title-2');
  508. newRow = $('<tr class="filter-group-operator-row"><td colspan="5"></td></tr>');
  509. newRow.find('td').append(this.operator);
  510. newRow.insertBefore(titleRow);
  511. var i, length = titleRows.length;
  512. // Starting with the third group, copy the operator to a new row above the
  513. // group title.
  514. for (i = 2; i < length; i++) {
  515. titleRow = $(titleRows[i]);
  516. // Make a copy of the operator dropdown and put it in a new table row.
  517. var fakeOperator = this.operator.clone();
  518. fakeOperator.attr('id', '');
  519. newRow = $('<tr class="filter-group-operator-row"><td colspan="5"></td></tr>');
  520. newRow.find('td').append(fakeOperator);
  521. newRow.insertBefore(titleRow);
  522. dropdowns = dropdowns.add(fakeOperator);
  523. }
  524. return dropdowns;
  525. };
  526. /**
  527. * Make the duplicated groups operators change in sync with each other.
  528. */
  529. Drupal.viewsUi.rearrangeFilterHandler.prototype.syncGroupsOperators = function () {
  530. if (this.dropdowns.length < 2) {
  531. // We only have one dropdown (or none at all), so there's nothing to sync.
  532. return;
  533. }
  534. this.dropdowns.change(jQuery.proxy(this, 'operatorChangeHandler'));
  535. };
  536. /**
  537. * Click handler for the operators that appear between filter groups.
  538. *
  539. * Forces all operator dropdowns to have the same value.
  540. */
  541. Drupal.viewsUi.rearrangeFilterHandler.prototype.operatorChangeHandler = function (event) {
  542. var $ = jQuery;
  543. var $target = $(event.target);
  544. var operators = this.dropdowns.find('select').not($target);
  545. // Change the other operators to match this new value.
  546. operators.val($target.val());
  547. };
  548. Drupal.viewsUi.rearrangeFilterHandler.prototype.modifyTableDrag = function () {
  549. var tableDrag = Drupal.tableDrag['views-rearrange-filters'];
  550. var filterHandler = this;
  551. /**
  552. * Override the row.onSwap method from tabledrag.js.
  553. *
  554. * When a row is dragged to another place in the table, several things need
  555. * to occur.
  556. * - The row needs to be moved so that it's within one of the filter groups.
  557. * - The operator cells that span multiple rows need their rowspan attributes
  558. * updated to reflect the number of rows in each group.
  559. * - The operator labels that are displayed next to each filter need to be
  560. * redrawn, to account for the row's new location.
  561. */
  562. tableDrag.row.prototype.onSwap = function () {
  563. if (filterHandler.hasGroupOperator) {
  564. // Make sure the row that just got moved (this.group) is inside one of
  565. // the filter groups (i.e. below an empty marker row or a draggable). If
  566. // it isn't, move it down one.
  567. var thisRow = jQuery(this.group);
  568. var previousRow = thisRow.prev('tr');
  569. if (previousRow.length && !previousRow.hasClass('group-message') && !previousRow.hasClass('draggable')) {
  570. // Move the dragged row down one.
  571. var next = thisRow.next();
  572. if (next.is('tr')) {
  573. this.swap('after', next);
  574. }
  575. }
  576. filterHandler.updateRowspans();
  577. }
  578. // Redraw the operator labels that are displayed next to each filter, to
  579. // account for the row's new location.
  580. filterHandler.redrawOperatorLabels();
  581. };
  582. /**
  583. * Override the onDrop method from tabledrag.js.
  584. */
  585. tableDrag.onDrop = function () {
  586. var $ = jQuery;
  587. // If the tabledrag change marker (i.e., the "*") has been inserted inside
  588. // a row after the operator label (i.e., "And" or "Or") rearrange the items
  589. // so the operator label continues to appear last.
  590. var changeMarker = $(this.oldRowElement).find('.tabledrag-changed');
  591. if (changeMarker.length) {
  592. // Search for occurrences of the operator label before the change marker,
  593. // and reverse them.
  594. var operatorLabel = changeMarker.prevAll('.views-operator-label');
  595. if (operatorLabel.length) {
  596. operatorLabel.insertAfter(changeMarker);
  597. }
  598. }
  599. // Make sure the "group" dropdown is properly updated when rows are dragged
  600. // into an empty filter group. This is borrowed heavily from the block.js
  601. // Implements tableDrag.onDrop().
  602. var groupRow = $(this.rowObject.element).prevAll('tr.group-message').get(0);
  603. var groupName = groupRow.className.replace(/([^ ]+[ ]+)*group-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
  604. var groupField = $('select.views-group-select', this.rowObject.element);
  605. if ($(this.rowObject.element).prev('tr').is('.group-message') && !groupField.is('.views-group-select-' + groupName)) {
  606. var oldGroupName = groupField.attr('class').replace(/([^ ]+[ ]+)*views-group-select-([^ ]+)([ ]+[^ ]+)*/, '$2');
  607. groupField.removeClass('views-group-select-' + oldGroupName).addClass('views-group-select-' + groupName);
  608. groupField.val(groupName);
  609. }
  610. };
  611. };
  612. /**
  613. * Redraw the operator labels that are displayed next to each filter.
  614. */
  615. Drupal.viewsUi.rearrangeFilterHandler.prototype.redrawOperatorLabels = function () {
  616. var $ = jQuery;
  617. for (i = 0; i < this.draggableRows.length; i++) {
  618. // Within the row, the operator labels are displayed inside the first table
  619. // cell (next to the filter name).
  620. var $draggableRow = $(this.draggableRows[i]);
  621. var $firstCell = $('td:first', $draggableRow);
  622. if ($firstCell.length) {
  623. // The value of the operator label ("And" or "Or") is taken from the
  624. // first operator dropdown we encounter, going backwards from the current
  625. // row. This dropdown is the one associated with the current row's filter
  626. // group.
  627. var operatorValue = $draggableRow.prevAll('.views-group-title').find('option:selected').html();
  628. var operatorLabel = '<span class="views-operator-label">' + operatorValue + '</span>';
  629. // If the next visible row after this one is a draggable filter row,
  630. // display the operator label next to the current row. (Checking for
  631. // visibility is necessary here since the "Remove" links hide the removed
  632. // row but don't actually remove it from the document).
  633. var $nextRow = $draggableRow.nextAll(':visible').eq(0);
  634. var $existingOperatorLabel = $firstCell.find('.views-operator-label');
  635. if ($nextRow.hasClass('draggable')) {
  636. // If an operator label was already there, replace it with the new one.
  637. if ($existingOperatorLabel.length) {
  638. $existingOperatorLabel.replaceWith(operatorLabel);
  639. }
  640. // Otherwise, append the operator label to the end of the table cell.
  641. else {
  642. $firstCell.append(operatorLabel);
  643. }
  644. }
  645. // If the next row doesn't contain a filter, then this is the last row
  646. // in the group. We don't want to display the operator there (since
  647. // operators should only display between two related filters, e.g.
  648. // "filter1 AND filter2 AND filter3"). So we remove any existing label
  649. // that this row has.
  650. else {
  651. $existingOperatorLabel.remove();
  652. }
  653. }
  654. }
  655. };
  656. /**
  657. * Update the rowspan attribute of each cell containing an operator dropdown.
  658. */
  659. Drupal.viewsUi.rearrangeFilterHandler.prototype.updateRowspans = function () {
  660. var $ = jQuery;
  661. var i, $row, $currentEmptyRow, draggableCount, $operatorCell;
  662. var rows = $(this.table).find('tr');
  663. var length = rows.length;
  664. for (i = 0; i < length; i++) {
  665. $row = $(rows[i]);
  666. if ($row.hasClass('views-group-title')) {
  667. // This row is a title row.
  668. // Keep a reference to the cell containing the dropdown operator.
  669. $operatorCell = $($row.find('td.group-operator'));
  670. // Assume this filter group is empty, until we find otherwise.
  671. draggableCount = 0;
  672. $currentEmptyRow = $row.next('tr');
  673. $currentEmptyRow.removeClass('group-populated').addClass('group-empty');
  674. // The cell with the dropdown operator should span the title row and
  675. // the "this group is empty" row.
  676. $operatorCell.attr('rowspan', 2);
  677. }
  678. else if (($row).hasClass('draggable') && $row.is(':visible')) {
  679. // We've found a visible filter row, so we now know the group isn't empty.
  680. draggableCount++;
  681. $currentEmptyRow.removeClass('group-empty').addClass('group-populated');
  682. // The operator cell should span all draggable rows, plus the title.
  683. $operatorCell.attr('rowspan', draggableCount + 1);
  684. }
  685. }
  686. };
  687. Drupal.behaviors.viewsFilterConfigSelectAll = {};
  688. /**
  689. * Add a select all checkbox, which checks each checkbox at once.
  690. */
  691. Drupal.behaviors.viewsFilterConfigSelectAll.attach = function(context) {
  692. var $ = jQuery;
  693. // Show the select all checkbox.
  694. $('#views-ui-config-item-form div.form-item-options-value-all', context).once(function() {
  695. $(this).show();
  696. })
  697. .find('input[type=checkbox]')
  698. .click(function() {
  699. var checked = $(this).is(':checked');
  700. // Update all checkbox beside the select all checkbox.
  701. $(this).parents('.form-checkboxes').find('input[type=checkbox]').each(function() {
  702. $(this).attr('checked', checked);
  703. });
  704. });
  705. // Uncheck the select all checkbox if any of the others are unchecked.
  706. $('#views-ui-config-item-form div.form-type-checkbox').not($('.form-item-options-value-all')).find('input[type=checkbox]').each(function() {
  707. $(this).click(function() {
  708. if ($(this).is('checked') == 0) {
  709. $('#edit-options-value-all').removeAttr('checked');
  710. }
  711. });
  712. });
  713. };
  714. /**
  715. * Ensure the desired default button is used when a form is implicitly submitted via an ENTER press on textfields, radios, and checkboxes.
  716. *
  717. * @see http://www.w3.org/TR/html5/association-of-controls-and-forms.html#implicit-submission
  718. */
  719. Drupal.behaviors.viewsImplicitFormSubmission = {};
  720. Drupal.behaviors.viewsImplicitFormSubmission.attach = function (context, settings) {
  721. var $ = jQuery;
  722. $(':text, :password, :radio, :checkbox', context).once('viewsImplicitFormSubmission', function() {
  723. $(this).keypress(function(event) {
  724. if (event.which == 13) {
  725. var formId = this.form.id;
  726. if (formId && settings.viewsImplicitFormSubmission && settings.viewsImplicitFormSubmission[formId] && settings.viewsImplicitFormSubmission[formId].defaultButton) {
  727. event.preventDefault();
  728. var buttonId = settings.viewsImplicitFormSubmission[formId].defaultButton;
  729. var $button = $('#' + buttonId, this.form);
  730. if ($button.length == 1 && $button.is(':enabled')) {
  731. if (Drupal.ajax && Drupal.ajax[buttonId]) {
  732. $button.trigger(Drupal.ajax[buttonId].element_settings.event);
  733. }
  734. else {
  735. $button.click();
  736. }
  737. }
  738. }
  739. }
  740. });
  741. });
  742. };
  743. /**
  744. * Remove icon class from elements that are themed as buttons or dropbuttons.
  745. */
  746. Drupal.behaviors.viewsRemoveIconClass = {};
  747. Drupal.behaviors.viewsRemoveIconClass.attach = function (context, settings) {
  748. jQuery('.ctools-button', context).once('RemoveIconClass', function () {
  749. var $ = jQuery;
  750. var $this = $(this);
  751. $('.icon', $this).removeClass('icon');
  752. $('.horizontal', $this).removeClass('horizontal');
  753. });
  754. };
  755. /**
  756. * Change "Expose filter" buttons into checkboxes.
  757. */
  758. Drupal.behaviors.viewsUiCheckboxify = {};
  759. Drupal.behaviors.viewsUiCheckboxify.attach = function (context, settings) {
  760. var $ = jQuery;
  761. var $buttons = $('#edit-options-expose-button-button, #edit-options-group-button-button').once('views-ui-checkboxify');
  762. var length = $buttons.length;
  763. var i;
  764. for (i = 0; i < length; i++) {
  765. new Drupal.viewsUi.Checkboxifier($buttons[i]);
  766. }
  767. };
  768. /**
  769. * Change the default widget to select the default group according to the
  770. * selected widget for the exposed group.
  771. */
  772. Drupal.behaviors.viewsUiChangeDefaultWidget = {};
  773. Drupal.behaviors.viewsUiChangeDefaultWidget.attach = function (context, settings) {
  774. var $ = jQuery;
  775. function change_default_widget(multiple) {
  776. if (multiple) {
  777. $('input.default-radios').hide();
  778. $('td.any-default-radios-row').parent().hide();
  779. $('input.default-checkboxes').show();
  780. }
  781. else {
  782. $('input.default-checkboxes').hide();
  783. $('td.any-default-radios-row').parent().show();
  784. $('input.default-radios').show();
  785. }
  786. }
  787. // Update on widget change.
  788. $('input[name="options[group_info][multiple]"]').change(function() {
  789. change_default_widget($(this).attr("checked"));
  790. });
  791. // Update the first time the form is rendered.
  792. $('input[name="options[group_info][multiple]"]').trigger('change');
  793. };
  794. /**
  795. * Attaches an expose filter button to a checkbox that triggers its click event.
  796. *
  797. * @param button
  798. * The DOM object representing the button to be checkboxified.
  799. */
  800. Drupal.viewsUi.Checkboxifier = function (button) {
  801. var $ = jQuery;
  802. this.$button = $(button);
  803. this.$parent = this.$button.parent('div.views-expose, div.views-grouped');
  804. this.$input = this.$parent.find('input:checkbox, input:radio');
  805. // Hide the button and its description.
  806. this.$button.hide();
  807. this.$parent.find('.exposed-description, .grouped-description').hide();
  808. this.$input.click($.proxy(this, 'clickHandler'));
  809. };
  810. /**
  811. * When the checkbox is checked or unchecked, simulate a button press.
  812. */
  813. Drupal.viewsUi.Checkboxifier.prototype.clickHandler = function (e) {
  814. this.$button.mousedown();
  815. this.$button.submit();
  816. };
  817. /**
  818. * Change the Apply button text based upon the override select state.
  819. */
  820. Drupal.behaviors.viewsUiOverrideSelect = {};
  821. Drupal.behaviors.viewsUiOverrideSelect.attach = function (context, settings) {
  822. var $ = jQuery;
  823. $('#edit-override-dropdown', context).once('views-ui-override-button-text', function() {
  824. // Closures! :(
  825. var $submit = $('#edit-submit', context);
  826. var old_value = $submit.val();
  827. $submit.once('views-ui-override-button-text')
  828. .bind('mouseup', function() {
  829. $(this).val(old_value);
  830. return true;
  831. });
  832. $(this).bind('change', function() {
  833. if ($(this).val() == 'default') {
  834. $submit.val(Drupal.t('Apply (all displays)'));
  835. }
  836. else if ($(this).val() == 'default_revert') {
  837. $submit.val(Drupal.t('Revert to default'));
  838. }
  839. else {
  840. $submit.val(Drupal.t('Apply (this display)'));
  841. }
  842. })
  843. .trigger('change');
  844. });
  845. };
  846. Drupal.viewsUi.resizeModal = function (e, no_shrink) {
  847. var $ = jQuery;
  848. var $modal = $('.views-ui-dialog');
  849. var $scroll = $('.scroll', $modal);
  850. if ($modal.length == 0 || $modal.css('display') == 'none') {
  851. return;
  852. }
  853. var maxWidth = parseInt($(window).width() * .85);
  854. // 70% of window.
  855. var minWidth = parseInt($(window).width() * .6);
  856. // 70% of window.
  857. // Set the modal to the minwidth so that our width calculation of
  858. // children works.
  859. $modal.css('width', minWidth);
  860. var width = minWidth;
  861. // Don't let the window get more than 80% of the display high.
  862. var maxHeight = parseInt($(window).height() * .8);
  863. var minHeight = 200;
  864. if (no_shrink) {
  865. minHeight = $modal.height();
  866. }
  867. if (minHeight > maxHeight) {
  868. minHeight = maxHeight;
  869. }
  870. var height = 0;
  871. // Calculate the height of the 'scroll' region.
  872. var scrollHeight = 0;
  873. scrollHeight += parseInt($scroll.css('padding-top'));
  874. scrollHeight += parseInt($scroll.css('padding-bottom'));
  875. $scroll.children().each(function() {
  876. var w = $(this).innerWidth();
  877. if (w > width) {
  878. width = w;
  879. }
  880. scrollHeight += $(this).outerHeight(true);
  881. });
  882. // Now, calculate what the difference between the scroll and the modal
  883. // will be.
  884. var difference = 0;
  885. difference += parseInt($scroll.css('padding-top'));
  886. difference += parseInt($scroll.css('padding-bottom'));
  887. difference += $('.views-override').outerHeight(true) || 0;
  888. difference += $('.views-messages').outerHeight(true) || 0;
  889. difference += $('#views-ajax-title').outerHeight(true) || 0;
  890. difference += $('.views-add-form-selected').outerHeight(true) || 0;
  891. difference += $('.form-buttons', $modal).outerHeight(true) || 0;
  892. height = scrollHeight + difference;
  893. if (height > maxHeight) {
  894. height = maxHeight;
  895. scrollHeight = maxHeight - difference;
  896. }
  897. else if (height < minHeight) {
  898. height = minHeight;
  899. scrollHeight = minHeight - difference;
  900. }
  901. if (width > maxWidth) {
  902. width = maxWidth;
  903. }
  904. // Get where we should move content to.
  905. var top = ($(window).height() / 2) - (height / 2);
  906. var left = ($(window).width() / 2) - (width / 2);
  907. $modal.css({
  908. 'top': top + 'px',
  909. 'left': left + 'px',
  910. 'width': width + 'px',
  911. 'height': height + 'px'
  912. });
  913. // Ensure inner popup height matches.
  914. $(Drupal.settings.views.ajax.popup).css('height', height + 'px');
  915. $scroll.css({
  916. 'height': scrollHeight + 'px',
  917. 'max-height': scrollHeight + 'px'
  918. });
  919. };
  920. jQuery(function() {
  921. jQuery(window).bind('resize', Drupal.viewsUi.resizeModal);
  922. jQuery(window).bind('scroll', Drupal.viewsUi.resizeModal);
  923. });