views-admin.js 37 KB

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