X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmisc%2Fdropbutton%2Fdropbutton.es6.js;h=54fd3fe3f4b81278b5e066da5946192d81ec2d85;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=6fe2b67db7be68849fec587c0b814f9367baea6a;hpb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;p=yaffs-website diff --git a/web/core/misc/dropbutton/dropbutton.es6.js b/web/core/misc/dropbutton/dropbutton.es6.js index 6fe2b67db..54fd3fe3f 100644 --- a/web/core/misc/dropbutton/dropbutton.es6.js +++ b/web/core/misc/dropbutton/dropbutton.es6.js @@ -3,46 +3,7 @@ * Dropbutton feature. */ -(function ($, Drupal) { - /** - * Process elements with the .dropbutton class on page load. - * - * @type {Drupal~behavior} - * - * @prop {Drupal~behaviorAttach} attach - * Attaches dropButton behaviors. - */ - Drupal.behaviors.dropButton = { - attach(context, settings) { - const $dropbuttons = $(context).find('.dropbutton-wrapper').once('dropbutton'); - if ($dropbuttons.length) { - // Adds the delegated handler that will toggle dropdowns on click. - const $body = $('body').once('dropbutton-click'); - if ($body.length) { - $body.on('click', '.dropbutton-toggle', dropbuttonClickHandler); - } - // Initialize all buttons. - const il = $dropbuttons.length; - for (let i = 0; i < il; i++) { - DropButton.dropbuttons.push(new DropButton($dropbuttons[i], settings.dropbutton)); - } - } - }, - }; - - /** - * Delegated callback for opening and closing dropbutton secondary actions. - * - * @function Drupal.DropButton~dropbuttonClickHandler - * - * @param {jQuery.Event} e - * The event triggered. - */ - function dropbuttonClickHandler(e) { - e.preventDefault(); - $(e.target).closest('.dropbutton-wrapper').toggleClass('open'); - } - +(function($, Drupal) { /** * A DropButton presents an HTML list as a button with a primary action. * @@ -61,7 +22,10 @@ */ function DropButton(dropbutton, settings) { // Merge defaults with settings. - const options = $.extend({ title: Drupal.t('List additional actions') }, settings); + const options = $.extend( + { title: Drupal.t('List additional actions') }, + settings, + ); const $dropbutton = $(dropbutton); /** @@ -91,139 +55,189 @@ // Add toggle link. $primary.after(Drupal.theme('dropbuttonToggle', options)); // Bind mouse events. - this.$dropbutton - .addClass('dropbutton-multiple') - .on({ - - /** - * Adds a timeout to close the dropdown on mouseleave. - * - * @ignore - */ - 'mouseleave.dropbutton': $.proxy(this.hoverOut, this), - - /** - * Clears timeout when mouseout of the dropdown. - * - * @ignore - */ - 'mouseenter.dropbutton': $.proxy(this.hoverIn, this), - - /** - * Similar to mouseleave/mouseenter, but for keyboard navigation. - * - * @ignore - */ - 'focusout.dropbutton': $.proxy(this.focusOut, this), - - /** - * @ignore - */ - 'focusin.dropbutton': $.proxy(this.focusIn, this), - }); - } - else { + this.$dropbutton.addClass('dropbutton-multiple').on({ + /** + * Adds a timeout to close the dropdown on mouseleave. + * + * @ignore + */ + 'mouseleave.dropbutton': $.proxy(this.hoverOut, this), + + /** + * Clears timeout when mouseout of the dropdown. + * + * @ignore + */ + 'mouseenter.dropbutton': $.proxy(this.hoverIn, this), + + /** + * Similar to mouseleave/mouseenter, but for keyboard navigation. + * + * @ignore + */ + 'focusout.dropbutton': $.proxy(this.focusOut, this), + + /** + * @ignore + */ + 'focusin.dropbutton': $.proxy(this.focusIn, this), + }); + } else { this.$dropbutton.addClass('dropbutton-single'); } } /** - * Extend the DropButton constructor. + * Delegated callback for opening and closing dropbutton secondary actions. + * + * @function Drupal.DropButton~dropbuttonClickHandler + * + * @param {jQuery.Event} e + * The event triggered. */ - $.extend(DropButton, /** @lends Drupal.DropButton */{ - /** - * Store all processed DropButtons. - * - * @type {Array.} - */ - dropbuttons: [], - }); + function dropbuttonClickHandler(e) { + e.preventDefault(); + $(e.target) + .closest('.dropbutton-wrapper') + .toggleClass('open'); + } /** - * Extend the DropButton prototype. + * Process elements with the .dropbutton class on page load. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches dropButton behaviors. */ - $.extend(DropButton.prototype, /** @lends Drupal.DropButton# */{ - - /** - * Toggle the dropbutton open and closed. - * - * @param {bool} [show] - * Force the dropbutton to open by passing true or to close by - * passing false. - */ - toggle(show) { - const isBool = typeof show === 'boolean'; - show = isBool ? show : !this.$dropbutton.hasClass('open'); - this.$dropbutton.toggleClass('open', show); - }, - - /** - * @method - */ - hoverIn() { - // Clear any previous timer we were using. - if (this.timerID) { - window.clearTimeout(this.timerID); + Drupal.behaviors.dropButton = { + attach(context, settings) { + const $dropbuttons = $(context) + .find('.dropbutton-wrapper') + .once('dropbutton'); + if ($dropbuttons.length) { + // Adds the delegated handler that will toggle dropdowns on click. + const $body = $('body').once('dropbutton-click'); + if ($body.length) { + $body.on('click', '.dropbutton-toggle', dropbuttonClickHandler); + } + // Initialize all buttons. + const il = $dropbuttons.length; + for (let i = 0; i < il; i++) { + DropButton.dropbuttons.push( + new DropButton($dropbuttons[i], settings.dropbutton), + ); + } } }, + }; - /** - * @method - */ - hoverOut() { - // Wait half a second before closing. - this.timerID = window.setTimeout($.proxy(this, 'close'), 500); - }, - - /** - * @method - */ - open() { - this.toggle(true); - }, - - /** - * @method - */ - close() { - this.toggle(false); - }, - - /** - * @param {jQuery.Event} e - * The event triggered. - */ - focusOut(e) { - this.hoverOut.call(this, e); + /** + * Extend the DropButton constructor. + */ + $.extend( + DropButton, + /** @lends Drupal.DropButton */ { + /** + * Store all processed DropButtons. + * + * @type {Array.} + */ + dropbuttons: [], }, + ); - /** - * @param {jQuery.Event} e - * The event triggered. - */ - focusIn(e) { - this.hoverIn.call(this, e); + /** + * Extend the DropButton prototype. + */ + $.extend( + DropButton.prototype, + /** @lends Drupal.DropButton# */ { + /** + * Toggle the dropbutton open and closed. + * + * @param {bool} [show] + * Force the dropbutton to open by passing true or to close by + * passing false. + */ + toggle(show) { + const isBool = typeof show === 'boolean'; + show = isBool ? show : !this.$dropbutton.hasClass('open'); + this.$dropbutton.toggleClass('open', show); + }, + + /** + * @method + */ + hoverIn() { + // Clear any previous timer we were using. + if (this.timerID) { + window.clearTimeout(this.timerID); + } + }, + + /** + * @method + */ + hoverOut() { + // Wait half a second before closing. + this.timerID = window.setTimeout($.proxy(this, 'close'), 500); + }, + + /** + * @method + */ + open() { + this.toggle(true); + }, + + /** + * @method + */ + close() { + this.toggle(false); + }, + + /** + * @param {jQuery.Event} e + * The event triggered. + */ + focusOut(e) { + this.hoverOut.call(this, e); + }, + + /** + * @param {jQuery.Event} e + * The event triggered. + */ + focusIn(e) { + this.hoverIn.call(this, e); + }, }, - }); - - $.extend(Drupal.theme, /** @lends Drupal.theme */{ - - /** - * A toggle is an interactive element often bound to a click handler. - * - * @param {object} options - * Options object. - * @param {string} [options.title] - * The HTML anchor title attribute and text for the inner span element. - * - * @return {string} - * A string representing a DOM fragment. - */ - dropbuttonToggle(options) { - return `
  • `; + ); + + $.extend( + Drupal.theme, + /** @lends Drupal.theme */ { + /** + * A toggle is an interactive element often bound to a click handler. + * + * @param {object} options + * Options object. + * @param {string} [options.title] + * The button text. + * + * @return {string} + * A string representing a DOM fragment. + */ + dropbuttonToggle(options) { + return `
  • `; + }, }, - }); + ); // Expose constructor in the public space. Drupal.DropButton = DropButton; -}(jQuery, Drupal)); +})(jQuery, Drupal);