From 4150d24e67d4cab6a9a6ff32c678c412c98e6b53 Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Wed, 27 Apr 2016 12:18:08 +0530 Subject: [PATCH] Compact links: Simply use the widget pattern instead of plugin We don't need plugin pattern here since it is not meant for a use in multiple contexts. Code simplified. Change-Id: Iee86069aaa82f5a9ab3a856c19360e9ea89211ad --- resources/js/ext.uls.compactlinks.js | 52 ++++++++-------------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/resources/js/ext.uls.compactlinks.js b/resources/js/ext.uls.compactlinks.js index 33f4c9a4..91469468 100644 --- a/resources/js/ext.uls.compactlinks.js +++ b/resources/js/ext.uls.compactlinks.js @@ -20,6 +20,7 @@ ( function ( $, mw ) { 'use strict'; + var DEFAULT_LIST_SIZE = 9; /** * For the given array, remove duplicates * @@ -43,13 +44,12 @@ */ function CompactInterlanguageList( interlanguageList, options ) { this.$interlanguageList = $( interlanguageList ); - this.options = $.extend( {}, $.fn.compactInterlanguageList.defaults, options ); + this.options = options || {}; this.interlanguageList = {}; this.compactList = {}; this.$trigger = null; this.compactSize = 0; this.listSize = 0; - this.init(); } CompactInterlanguageList.prototype = { @@ -58,7 +58,7 @@ */ init: function () { var self = this, - max = this.options.max; + max = this.options.max || DEFAULT_LIST_SIZE; this.interlanguageList = this.getInterlanguageList(); this.listSize = Object.keys( this.interlanguageList ).length; @@ -115,10 +115,10 @@ this.$menu.addClass( 'interlanguage-uls-menu' ); }, /** - * Language selection handler - * - * @param {string} language language code - */ + * Language selection handler + * + * @param {string} language language code + */ onSelect: function ( language ) { var previousLanguages = mw.uls.getPreviousLanguages(); @@ -345,37 +345,13 @@ } }; - /** - * CompactInterlanguageList Plugin - * - * @param {Object} [option] - */ - $.fn.compactInterlanguageList = function ( option ) { - return this.each( function () { - var $this = $( this ), - data = $this.data( 'compactinterlanguagelist' ), - options = typeof option === 'object' && option; - - if ( !data ) { - data = new CompactInterlanguageList( this, options ); - $this.data( 'compactinterlanguagelist', data ); - } - - if ( typeof option === 'string' ) { - data[ option ](); - } - } ); - }; - - /** - * Defaults - */ - $.fn.compactInterlanguageList.defaults = { - // Compact the list to this size - max: 9 - }; - $( document ).ready( function () { - $( '#p-lang ul' ).compactInterlanguageList(); + var compactList; + + compactList = new CompactInterlanguageList( $( '#p-lang ul' ), { + // Compact the list to this size + max: 9 + } ); + compactList.init(); } ); }( jQuery, mediaWiki ) );