oojs-ui-toolbars.js 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366
  1. /*!
  2. * OOjs UI v0.21.1
  3. * https://www.mediawiki.org/wiki/OOjs_UI
  4. *
  5. * Copyright 2011–2017 OOjs UI Team and other contributors.
  6. * Released under the MIT license
  7. * http://oojs.mit-license.org
  8. *
  9. * Date: 2017-04-18T23:32:49Z
  10. */
  11. ( function ( OO ) {
  12. 'use strict';
  13. /**
  14. * Toolbars are complex interface components that permit users to easily access a variety
  15. * of {@link OO.ui.Tool tools} (e.g., formatting commands) and actions, which are additional commands that are
  16. * part of the toolbar, but not configured as tools.
  17. *
  18. * Individual tools are customized and then registered with a {@link OO.ui.ToolFactory tool factory}, which creates
  19. * the tools on demand. Each tool has a symbolic name (used when registering the tool), a title (e.g., ‘Insert
  20. * image’), and an icon.
  21. *
  22. * Individual tools are organized in {@link OO.ui.ToolGroup toolgroups}, which can be {@link OO.ui.MenuToolGroup menus}
  23. * of tools, {@link OO.ui.ListToolGroup lists} of tools, or a single {@link OO.ui.BarToolGroup bar} of tools.
  24. * The arrangement and order of the toolgroups is customized when the toolbar is set up. Tools can be presented in
  25. * any order, but each can only appear once in the toolbar.
  26. *
  27. * The toolbar can be synchronized with the state of the external "application", like a text
  28. * editor's editing area, marking tools as active/inactive (e.g. a 'bold' tool would be shown as
  29. * active when the text cursor was inside bolded text) or enabled/disabled (e.g. a table caption
  30. * tool would be disabled while the user is not editing a table). A state change is signalled by
  31. * emitting the {@link #event-updateState 'updateState' event}, which calls Tools'
  32. * {@link OO.ui.Tool#onUpdateState onUpdateState method}.
  33. *
  34. * The following is an example of a basic toolbar.
  35. *
  36. * @example
  37. * // Example of a toolbar
  38. * // Create the toolbar
  39. * var toolFactory = new OO.ui.ToolFactory();
  40. * var toolGroupFactory = new OO.ui.ToolGroupFactory();
  41. * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
  42. *
  43. * // We will be placing status text in this element when tools are used
  44. * var $area = $( '<p>' ).text( 'Toolbar example' );
  45. *
  46. * // Define the tools that we're going to place in our toolbar
  47. *
  48. * // Create a class inheriting from OO.ui.Tool
  49. * function SearchTool() {
  50. * SearchTool.parent.apply( this, arguments );
  51. * }
  52. * OO.inheritClass( SearchTool, OO.ui.Tool );
  53. * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
  54. * // of 'icon' and 'title' (displayed icon and text).
  55. * SearchTool.static.name = 'search';
  56. * SearchTool.static.icon = 'search';
  57. * SearchTool.static.title = 'Search...';
  58. * // Defines the action that will happen when this tool is selected (clicked).
  59. * SearchTool.prototype.onSelect = function () {
  60. * $area.text( 'Search tool clicked!' );
  61. * // Never display this tool as "active" (selected).
  62. * this.setActive( false );
  63. * };
  64. * SearchTool.prototype.onUpdateState = function () {};
  65. * // Make this tool available in our toolFactory and thus our toolbar
  66. * toolFactory.register( SearchTool );
  67. *
  68. * // Register two more tools, nothing interesting here
  69. * function SettingsTool() {
  70. * SettingsTool.parent.apply( this, arguments );
  71. * }
  72. * OO.inheritClass( SettingsTool, OO.ui.Tool );
  73. * SettingsTool.static.name = 'settings';
  74. * SettingsTool.static.icon = 'settings';
  75. * SettingsTool.static.title = 'Change settings';
  76. * SettingsTool.prototype.onSelect = function () {
  77. * $area.text( 'Settings tool clicked!' );
  78. * this.setActive( false );
  79. * };
  80. * SettingsTool.prototype.onUpdateState = function () {};
  81. * toolFactory.register( SettingsTool );
  82. *
  83. * // Register two more tools, nothing interesting here
  84. * function StuffTool() {
  85. * StuffTool.parent.apply( this, arguments );
  86. * }
  87. * OO.inheritClass( StuffTool, OO.ui.Tool );
  88. * StuffTool.static.name = 'stuff';
  89. * StuffTool.static.icon = 'ellipsis';
  90. * StuffTool.static.title = 'More stuff';
  91. * StuffTool.prototype.onSelect = function () {
  92. * $area.text( 'More stuff tool clicked!' );
  93. * this.setActive( false );
  94. * };
  95. * StuffTool.prototype.onUpdateState = function () {};
  96. * toolFactory.register( StuffTool );
  97. *
  98. * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
  99. * // little popup window (a PopupWidget).
  100. * function HelpTool( toolGroup, config ) {
  101. * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
  102. * padded: true,
  103. * label: 'Help',
  104. * head: true
  105. * } }, config ) );
  106. * this.popup.$body.append( '<p>I am helpful!</p>' );
  107. * }
  108. * OO.inheritClass( HelpTool, OO.ui.PopupTool );
  109. * HelpTool.static.name = 'help';
  110. * HelpTool.static.icon = 'help';
  111. * HelpTool.static.title = 'Help';
  112. * toolFactory.register( HelpTool );
  113. *
  114. * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
  115. * // used once (but not all defined tools must be used).
  116. * toolbar.setup( [
  117. * {
  118. * // 'bar' tool groups display tools' icons only, side-by-side.
  119. * type: 'bar',
  120. * include: [ 'search', 'help' ]
  121. * },
  122. * {
  123. * // 'list' tool groups display both the titles and icons, in a dropdown list.
  124. * type: 'list',
  125. * indicator: 'down',
  126. * label: 'More',
  127. * include: [ 'settings', 'stuff' ]
  128. * }
  129. * // Note how the tools themselves are toolgroup-agnostic - the same tool can be displayed
  130. * // either in a 'list' or a 'bar'. There is a 'menu' tool group too, not showcased here,
  131. * // since it's more complicated to use. (See the next example snippet on this page.)
  132. * ] );
  133. *
  134. * // Create some UI around the toolbar and place it in the document
  135. * var frame = new OO.ui.PanelLayout( {
  136. * expanded: false,
  137. * framed: true
  138. * } );
  139. * var contentFrame = new OO.ui.PanelLayout( {
  140. * expanded: false,
  141. * padded: true
  142. * } );
  143. * frame.$element.append(
  144. * toolbar.$element,
  145. * contentFrame.$element.append( $area )
  146. * );
  147. * $( 'body' ).append( frame.$element );
  148. *
  149. * // Here is where the toolbar is actually built. This must be done after inserting it into the
  150. * // document.
  151. * toolbar.initialize();
  152. * toolbar.emit( 'updateState' );
  153. *
  154. * The following example extends the previous one to illustrate 'menu' toolgroups and the usage of
  155. * {@link #event-updateState 'updateState' event}.
  156. *
  157. * @example
  158. * // Create the toolbar
  159. * var toolFactory = new OO.ui.ToolFactory();
  160. * var toolGroupFactory = new OO.ui.ToolGroupFactory();
  161. * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
  162. *
  163. * // We will be placing status text in this element when tools are used
  164. * var $area = $( '<p>' ).text( 'Toolbar example' );
  165. *
  166. * // Define the tools that we're going to place in our toolbar
  167. *
  168. * // Create a class inheriting from OO.ui.Tool
  169. * function SearchTool() {
  170. * SearchTool.parent.apply( this, arguments );
  171. * }
  172. * OO.inheritClass( SearchTool, OO.ui.Tool );
  173. * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
  174. * // of 'icon' and 'title' (displayed icon and text).
  175. * SearchTool.static.name = 'search';
  176. * SearchTool.static.icon = 'search';
  177. * SearchTool.static.title = 'Search...';
  178. * // Defines the action that will happen when this tool is selected (clicked).
  179. * SearchTool.prototype.onSelect = function () {
  180. * $area.text( 'Search tool clicked!' );
  181. * // Never display this tool as "active" (selected).
  182. * this.setActive( false );
  183. * };
  184. * SearchTool.prototype.onUpdateState = function () {};
  185. * // Make this tool available in our toolFactory and thus our toolbar
  186. * toolFactory.register( SearchTool );
  187. *
  188. * // Register two more tools, nothing interesting here
  189. * function SettingsTool() {
  190. * SettingsTool.parent.apply( this, arguments );
  191. * this.reallyActive = false;
  192. * }
  193. * OO.inheritClass( SettingsTool, OO.ui.Tool );
  194. * SettingsTool.static.name = 'settings';
  195. * SettingsTool.static.icon = 'settings';
  196. * SettingsTool.static.title = 'Change settings';
  197. * SettingsTool.prototype.onSelect = function () {
  198. * $area.text( 'Settings tool clicked!' );
  199. * // Toggle the active state on each click
  200. * this.reallyActive = !this.reallyActive;
  201. * this.setActive( this.reallyActive );
  202. * // To update the menu label
  203. * this.toolbar.emit( 'updateState' );
  204. * };
  205. * SettingsTool.prototype.onUpdateState = function () {};
  206. * toolFactory.register( SettingsTool );
  207. *
  208. * // Register two more tools, nothing interesting here
  209. * function StuffTool() {
  210. * StuffTool.parent.apply( this, arguments );
  211. * this.reallyActive = false;
  212. * }
  213. * OO.inheritClass( StuffTool, OO.ui.Tool );
  214. * StuffTool.static.name = 'stuff';
  215. * StuffTool.static.icon = 'ellipsis';
  216. * StuffTool.static.title = 'More stuff';
  217. * StuffTool.prototype.onSelect = function () {
  218. * $area.text( 'More stuff tool clicked!' );
  219. * // Toggle the active state on each click
  220. * this.reallyActive = !this.reallyActive;
  221. * this.setActive( this.reallyActive );
  222. * // To update the menu label
  223. * this.toolbar.emit( 'updateState' );
  224. * };
  225. * StuffTool.prototype.onUpdateState = function () {};
  226. * toolFactory.register( StuffTool );
  227. *
  228. * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
  229. * // little popup window (a PopupWidget). 'onUpdateState' is also already implemented.
  230. * function HelpTool( toolGroup, config ) {
  231. * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
  232. * padded: true,
  233. * label: 'Help',
  234. * head: true
  235. * } }, config ) );
  236. * this.popup.$body.append( '<p>I am helpful!</p>' );
  237. * }
  238. * OO.inheritClass( HelpTool, OO.ui.PopupTool );
  239. * HelpTool.static.name = 'help';
  240. * HelpTool.static.icon = 'help';
  241. * HelpTool.static.title = 'Help';
  242. * toolFactory.register( HelpTool );
  243. *
  244. * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
  245. * // used once (but not all defined tools must be used).
  246. * toolbar.setup( [
  247. * {
  248. * // 'bar' tool groups display tools' icons only, side-by-side.
  249. * type: 'bar',
  250. * include: [ 'search', 'help' ]
  251. * },
  252. * {
  253. * // 'menu' tool groups display both the titles and icons, in a dropdown menu.
  254. * // Menu label indicates which items are selected.
  255. * type: 'menu',
  256. * indicator: 'down',
  257. * include: [ 'settings', 'stuff' ]
  258. * }
  259. * ] );
  260. *
  261. * // Create some UI around the toolbar and place it in the document
  262. * var frame = new OO.ui.PanelLayout( {
  263. * expanded: false,
  264. * framed: true
  265. * } );
  266. * var contentFrame = new OO.ui.PanelLayout( {
  267. * expanded: false,
  268. * padded: true
  269. * } );
  270. * frame.$element.append(
  271. * toolbar.$element,
  272. * contentFrame.$element.append( $area )
  273. * );
  274. * $( 'body' ).append( frame.$element );
  275. *
  276. * // Here is where the toolbar is actually built. This must be done after inserting it into the
  277. * // document.
  278. * toolbar.initialize();
  279. * toolbar.emit( 'updateState' );
  280. *
  281. * @class
  282. * @extends OO.ui.Element
  283. * @mixins OO.EventEmitter
  284. * @mixins OO.ui.mixin.GroupElement
  285. *
  286. * @constructor
  287. * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
  288. * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating toolgroups
  289. * @param {Object} [config] Configuration options
  290. * @cfg {boolean} [actions] Add an actions section to the toolbar. Actions are commands that are included
  291. * in the toolbar, but are not configured as tools. By default, actions are displayed on the right side of
  292. * the toolbar.
  293. * @cfg {string} [position='top'] Whether the toolbar is positioned above ('top') or below ('bottom') content.
  294. */
  295. OO.ui.Toolbar = function OoUiToolbar( toolFactory, toolGroupFactory, config ) {
  296. // Allow passing positional parameters inside the config object
  297. if ( OO.isPlainObject( toolFactory ) && config === undefined ) {
  298. config = toolFactory;
  299. toolFactory = config.toolFactory;
  300. toolGroupFactory = config.toolGroupFactory;
  301. }
  302. // Configuration initialization
  303. config = config || {};
  304. // Parent constructor
  305. OO.ui.Toolbar.parent.call( this, config );
  306. // Mixin constructors
  307. OO.EventEmitter.call( this );
  308. OO.ui.mixin.GroupElement.call( this, config );
  309. // Properties
  310. this.toolFactory = toolFactory;
  311. this.toolGroupFactory = toolGroupFactory;
  312. this.groups = [];
  313. this.tools = {};
  314. this.position = config.position || 'top';
  315. this.$bar = $( '<div>' );
  316. this.$actions = $( '<div>' );
  317. this.initialized = false;
  318. this.narrowThreshold = null;
  319. this.onWindowResizeHandler = this.onWindowResize.bind( this );
  320. // Events
  321. this.$element
  322. .add( this.$bar ).add( this.$group ).add( this.$actions )
  323. .on( 'mousedown keydown', this.onPointerDown.bind( this ) );
  324. // Initialization
  325. this.$group.addClass( 'oo-ui-toolbar-tools' );
  326. if ( config.actions ) {
  327. this.$bar.append( this.$actions.addClass( 'oo-ui-toolbar-actions' ) );
  328. }
  329. this.$bar
  330. .addClass( 'oo-ui-toolbar-bar' )
  331. .append( this.$group, '<div style="clear:both"></div>' );
  332. this.$element.addClass( 'oo-ui-toolbar oo-ui-toolbar-position-' + this.position ).append( this.$bar );
  333. };
  334. /* Setup */
  335. OO.inheritClass( OO.ui.Toolbar, OO.ui.Element );
  336. OO.mixinClass( OO.ui.Toolbar, OO.EventEmitter );
  337. OO.mixinClass( OO.ui.Toolbar, OO.ui.mixin.GroupElement );
  338. /* Events */
  339. /**
  340. * @event updateState
  341. *
  342. * An 'updateState' event must be emitted on the Toolbar (by calling `toolbar.emit( 'updateState' )`)
  343. * every time the state of the application using the toolbar changes, and an update to the state of
  344. * tools is required.
  345. *
  346. * @param {...Mixed} data Application-defined parameters
  347. */
  348. /* Methods */
  349. /**
  350. * Get the tool factory.
  351. *
  352. * @return {OO.ui.ToolFactory} Tool factory
  353. */
  354. OO.ui.Toolbar.prototype.getToolFactory = function () {
  355. return this.toolFactory;
  356. };
  357. /**
  358. * Get the toolgroup factory.
  359. *
  360. * @return {OO.Factory} Toolgroup factory
  361. */
  362. OO.ui.Toolbar.prototype.getToolGroupFactory = function () {
  363. return this.toolGroupFactory;
  364. };
  365. /**
  366. * Handles mouse down events.
  367. *
  368. * @private
  369. * @param {jQuery.Event} e Mouse down event
  370. */
  371. OO.ui.Toolbar.prototype.onPointerDown = function ( e ) {
  372. var $closestWidgetToEvent = $( e.target ).closest( '.oo-ui-widget' ),
  373. $closestWidgetToToolbar = this.$element.closest( '.oo-ui-widget' );
  374. if ( !$closestWidgetToEvent.length || $closestWidgetToEvent[ 0 ] === $closestWidgetToToolbar[ 0 ] ) {
  375. return false;
  376. }
  377. };
  378. /**
  379. * Handle window resize event.
  380. *
  381. * @private
  382. * @param {jQuery.Event} e Window resize event
  383. */
  384. OO.ui.Toolbar.prototype.onWindowResize = function () {
  385. this.$element.toggleClass(
  386. 'oo-ui-toolbar-narrow',
  387. this.$bar.width() <= this.getNarrowThreshold()
  388. );
  389. };
  390. /**
  391. * Get the (lazily-computed) width threshold for applying the oo-ui-toolbar-narrow
  392. * class.
  393. *
  394. * @private
  395. * @return {number} Width threshold in pixels
  396. */
  397. OO.ui.Toolbar.prototype.getNarrowThreshold = function () {
  398. if ( this.narrowThreshold === null ) {
  399. this.narrowThreshold = this.$group.width() + this.$actions.width();
  400. }
  401. return this.narrowThreshold;
  402. };
  403. /**
  404. * Sets up handles and preloads required information for the toolbar to work.
  405. * This must be called after it is attached to a visible document and before doing anything else.
  406. */
  407. OO.ui.Toolbar.prototype.initialize = function () {
  408. if ( !this.initialized ) {
  409. this.initialized = true;
  410. $( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler );
  411. this.onWindowResize();
  412. }
  413. };
  414. /**
  415. * Set up the toolbar.
  416. *
  417. * The toolbar is set up with a list of toolgroup configurations that specify the type of
  418. * toolgroup ({@link OO.ui.BarToolGroup bar}, {@link OO.ui.MenuToolGroup menu}, or {@link OO.ui.ListToolGroup list})
  419. * to add and which tools to include, exclude, promote, or demote within that toolgroup. Please
  420. * see {@link OO.ui.ToolGroup toolgroups} for more information about including tools in toolgroups.
  421. *
  422. * @param {Object.<string,Array>} groups List of toolgroup configurations
  423. * @param {Array|string} [groups.include] Tools to include in the toolgroup
  424. * @param {Array|string} [groups.exclude] Tools to exclude from the toolgroup
  425. * @param {Array|string} [groups.promote] Tools to promote to the beginning of the toolgroup
  426. * @param {Array|string} [groups.demote] Tools to demote to the end of the toolgroup
  427. */
  428. OO.ui.Toolbar.prototype.setup = function ( groups ) {
  429. var i, len, type, group,
  430. items = [],
  431. defaultType = 'bar';
  432. // Cleanup previous groups
  433. this.reset();
  434. // Build out new groups
  435. for ( i = 0, len = groups.length; i < len; i++ ) {
  436. group = groups[ i ];
  437. if ( group.include === '*' ) {
  438. // Apply defaults to catch-all groups
  439. if ( group.type === undefined ) {
  440. group.type = 'list';
  441. }
  442. if ( group.label === undefined ) {
  443. group.label = OO.ui.msg( 'ooui-toolbar-more' );
  444. }
  445. }
  446. // Check type has been registered
  447. type = this.getToolGroupFactory().lookup( group.type ) ? group.type : defaultType;
  448. items.push(
  449. this.getToolGroupFactory().create( type, this, group )
  450. );
  451. }
  452. this.addItems( items );
  453. };
  454. /**
  455. * Remove all tools and toolgroups from the toolbar.
  456. */
  457. OO.ui.Toolbar.prototype.reset = function () {
  458. var i, len;
  459. this.groups = [];
  460. this.tools = {};
  461. for ( i = 0, len = this.items.length; i < len; i++ ) {
  462. this.items[ i ].destroy();
  463. }
  464. this.clearItems();
  465. };
  466. /**
  467. * Destroy the toolbar.
  468. *
  469. * Destroying the toolbar removes all event handlers and DOM elements that constitute the toolbar. Call
  470. * this method whenever you are done using a toolbar.
  471. */
  472. OO.ui.Toolbar.prototype.destroy = function () {
  473. $( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler );
  474. this.reset();
  475. this.$element.remove();
  476. };
  477. /**
  478. * Check if the tool is available.
  479. *
  480. * Available tools are ones that have not yet been added to the toolbar.
  481. *
  482. * @param {string} name Symbolic name of tool
  483. * @return {boolean} Tool is available
  484. */
  485. OO.ui.Toolbar.prototype.isToolAvailable = function ( name ) {
  486. return !this.tools[ name ];
  487. };
  488. /**
  489. * Prevent tool from being used again.
  490. *
  491. * @param {OO.ui.Tool} tool Tool to reserve
  492. */
  493. OO.ui.Toolbar.prototype.reserveTool = function ( tool ) {
  494. this.tools[ tool.getName() ] = tool;
  495. };
  496. /**
  497. * Allow tool to be used again.
  498. *
  499. * @param {OO.ui.Tool} tool Tool to release
  500. */
  501. OO.ui.Toolbar.prototype.releaseTool = function ( tool ) {
  502. delete this.tools[ tool.getName() ];
  503. };
  504. /**
  505. * Get accelerator label for tool.
  506. *
  507. * The OOjs UI library does not contain an accelerator system, but this is the hook for one. To
  508. * use an accelerator system, subclass the toolbar and override this method, which is meant to return a label
  509. * that describes the accelerator keys for the tool passed (by symbolic name) to the method.
  510. *
  511. * @param {string} name Symbolic name of tool
  512. * @return {string|undefined} Tool accelerator label if available
  513. */
  514. OO.ui.Toolbar.prototype.getToolAccelerator = function () {
  515. return undefined;
  516. };
  517. /**
  518. * Tools, together with {@link OO.ui.ToolGroup toolgroups}, constitute {@link OO.ui.Toolbar toolbars}.
  519. * Each tool is configured with a static name, title, and icon and is customized with the command to carry
  520. * out when the tool is selected. Tools must also be registered with a {@link OO.ui.ToolFactory tool factory},
  521. * which creates the tools on demand.
  522. *
  523. * Every Tool subclass must implement two methods:
  524. *
  525. * - {@link #onUpdateState}
  526. * - {@link #onSelect}
  527. *
  528. * Tools are added to toolgroups ({@link OO.ui.ListToolGroup ListToolGroup},
  529. * {@link OO.ui.BarToolGroup BarToolGroup}, or {@link OO.ui.MenuToolGroup MenuToolGroup}), which determine how
  530. * the tool is displayed in the toolbar. See {@link OO.ui.Toolbar toolbars} for an example.
  531. *
  532. * For more information, please see the [OOjs UI documentation on MediaWiki][1].
  533. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  534. *
  535. * @abstract
  536. * @class
  537. * @extends OO.ui.Widget
  538. * @mixins OO.ui.mixin.IconElement
  539. * @mixins OO.ui.mixin.FlaggedElement
  540. * @mixins OO.ui.mixin.TabIndexedElement
  541. *
  542. * @constructor
  543. * @param {OO.ui.ToolGroup} toolGroup
  544. * @param {Object} [config] Configuration options
  545. * @cfg {string|Function} [title] Title text or a function that returns text. If this config is omitted, the value of
  546. * the {@link #static-title static title} property is used.
  547. *
  548. * The title is used in different ways depending on the type of toolgroup that contains the tool. The
  549. * title is used as a tooltip if the tool is part of a {@link OO.ui.BarToolGroup bar} toolgroup, or as the label text if the tool is
  550. * part of a {@link OO.ui.ListToolGroup list} or {@link OO.ui.MenuToolGroup menu} toolgroup.
  551. *
  552. * For bar toolgroups, a description of the accelerator key is appended to the title if an accelerator key
  553. * is associated with an action by the same name as the tool and accelerator functionality has been added to the application.
  554. * To add accelerator key functionality, you must subclass OO.ui.Toolbar and override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method.
  555. */
  556. OO.ui.Tool = function OoUiTool( toolGroup, config ) {
  557. // Allow passing positional parameters inside the config object
  558. if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
  559. config = toolGroup;
  560. toolGroup = config.toolGroup;
  561. }
  562. // Configuration initialization
  563. config = config || {};
  564. // Parent constructor
  565. OO.ui.Tool.parent.call( this, config );
  566. // Properties
  567. this.toolGroup = toolGroup;
  568. this.toolbar = this.toolGroup.getToolbar();
  569. this.active = false;
  570. this.$title = $( '<span>' );
  571. this.$accel = $( '<span>' );
  572. this.$link = $( '<a>' );
  573. this.title = null;
  574. // Mixin constructors
  575. OO.ui.mixin.IconElement.call( this, config );
  576. OO.ui.mixin.FlaggedElement.call( this, config );
  577. OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$link } ) );
  578. // Events
  579. this.toolbar.connect( this, { updateState: 'onUpdateState' } );
  580. // Initialization
  581. this.$title.addClass( 'oo-ui-tool-title' );
  582. this.$accel
  583. .addClass( 'oo-ui-tool-accel' )
  584. .prop( {
  585. // This may need to be changed if the key names are ever localized,
  586. // but for now they are essentially written in English
  587. dir: 'ltr',
  588. lang: 'en'
  589. } );
  590. this.$link
  591. .addClass( 'oo-ui-tool-link' )
  592. .append( this.$icon, this.$title, this.$accel )
  593. .attr( 'role', 'button' );
  594. this.$element
  595. .data( 'oo-ui-tool', this )
  596. .addClass(
  597. 'oo-ui-tool ' + 'oo-ui-tool-name-' +
  598. this.constructor.static.name.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
  599. )
  600. .toggleClass( 'oo-ui-tool-with-label', this.constructor.static.displayBothIconAndLabel )
  601. .append( this.$link );
  602. this.setTitle( config.title || this.constructor.static.title );
  603. };
  604. /* Setup */
  605. OO.inheritClass( OO.ui.Tool, OO.ui.Widget );
  606. OO.mixinClass( OO.ui.Tool, OO.ui.mixin.IconElement );
  607. OO.mixinClass( OO.ui.Tool, OO.ui.mixin.FlaggedElement );
  608. OO.mixinClass( OO.ui.Tool, OO.ui.mixin.TabIndexedElement );
  609. /* Static Properties */
  610. /**
  611. * @static
  612. * @inheritdoc
  613. */
  614. OO.ui.Tool.static.tagName = 'span';
  615. /**
  616. * Symbolic name of tool.
  617. *
  618. * The symbolic name is used internally to register the tool with a {@link OO.ui.ToolFactory ToolFactory}. It can
  619. * also be used when adding tools to toolgroups.
  620. *
  621. * @abstract
  622. * @static
  623. * @inheritable
  624. * @property {string}
  625. */
  626. OO.ui.Tool.static.name = '';
  627. /**
  628. * Symbolic name of the group.
  629. *
  630. * The group name is used to associate tools with each other so that they can be selected later by
  631. * a {@link OO.ui.ToolGroup toolgroup}.
  632. *
  633. * @abstract
  634. * @static
  635. * @inheritable
  636. * @property {string}
  637. */
  638. OO.ui.Tool.static.group = '';
  639. /**
  640. * Tool title text or a function that returns title text. The value of the static property is overridden if the #title config option is used.
  641. *
  642. * @abstract
  643. * @static
  644. * @inheritable
  645. * @property {string|Function}
  646. */
  647. OO.ui.Tool.static.title = '';
  648. /**
  649. * Display both icon and label when the tool is used in a {@link OO.ui.BarToolGroup bar} toolgroup.
  650. * Normally only the icon is displayed, or only the label if no icon is given.
  651. *
  652. * @static
  653. * @inheritable
  654. * @property {boolean}
  655. */
  656. OO.ui.Tool.static.displayBothIconAndLabel = false;
  657. /**
  658. * Add tool to catch-all groups automatically.
  659. *
  660. * A catch-all group, which contains all tools that do not currently belong to a toolgroup,
  661. * can be included in a toolgroup using the wildcard selector, an asterisk (*).
  662. *
  663. * @static
  664. * @inheritable
  665. * @property {boolean}
  666. */
  667. OO.ui.Tool.static.autoAddToCatchall = true;
  668. /**
  669. * Add tool to named groups automatically.
  670. *
  671. * By default, tools that are configured with a static ‘group’ property are added
  672. * to that group and will be selected when the symbolic name of the group is specified (e.g., when
  673. * toolgroups include tools by group name).
  674. *
  675. * @static
  676. * @property {boolean}
  677. * @inheritable
  678. */
  679. OO.ui.Tool.static.autoAddToGroup = true;
  680. /**
  681. * Check if this tool is compatible with given data.
  682. *
  683. * This is a stub that can be overridden to provide support for filtering tools based on an
  684. * arbitrary piece of information (e.g., where the cursor is in a document). The implementation
  685. * must also call this method so that the compatibility check can be performed.
  686. *
  687. * @static
  688. * @inheritable
  689. * @param {Mixed} data Data to check
  690. * @return {boolean} Tool can be used with data
  691. */
  692. OO.ui.Tool.static.isCompatibleWith = function () {
  693. return false;
  694. };
  695. /* Methods */
  696. /**
  697. * Handle the toolbar state being updated. This method is called when the
  698. * {@link OO.ui.Toolbar#event-updateState 'updateState' event} is emitted on the
  699. * {@link OO.ui.Toolbar Toolbar} that uses this tool, and should set the state of this tool
  700. * depending on application state (usually by calling #setDisabled to enable or disable the tool,
  701. * or #setActive to mark is as currently in-use or not).
  702. *
  703. * This is an abstract method that must be overridden in a concrete subclass.
  704. *
  705. * @method
  706. * @protected
  707. * @abstract
  708. */
  709. OO.ui.Tool.prototype.onUpdateState = null;
  710. /**
  711. * Handle the tool being selected. This method is called when the user triggers this tool,
  712. * usually by clicking on its label/icon.
  713. *
  714. * This is an abstract method that must be overridden in a concrete subclass.
  715. *
  716. * @method
  717. * @protected
  718. * @abstract
  719. */
  720. OO.ui.Tool.prototype.onSelect = null;
  721. /**
  722. * Check if the tool is active.
  723. *
  724. * Tools become active when their #onSelect or #onUpdateState handlers change them to appear pressed
  725. * with the #setActive method. Additional CSS is applied to the tool to reflect the active state.
  726. *
  727. * @return {boolean} Tool is active
  728. */
  729. OO.ui.Tool.prototype.isActive = function () {
  730. return this.active;
  731. };
  732. /**
  733. * Make the tool appear active or inactive.
  734. *
  735. * This method should be called within #onSelect or #onUpdateState event handlers to make the tool
  736. * appear pressed or not.
  737. *
  738. * @param {boolean} state Make tool appear active
  739. */
  740. OO.ui.Tool.prototype.setActive = function ( state ) {
  741. this.active = !!state;
  742. if ( this.active ) {
  743. this.$element.addClass( 'oo-ui-tool-active' );
  744. this.setFlags( { progressive: true } );
  745. } else {
  746. this.$element.removeClass( 'oo-ui-tool-active' );
  747. this.setFlags( { progressive: false } );
  748. }
  749. };
  750. /**
  751. * Set the tool #title.
  752. *
  753. * @param {string|Function} title Title text or a function that returns text
  754. * @chainable
  755. */
  756. OO.ui.Tool.prototype.setTitle = function ( title ) {
  757. this.title = OO.ui.resolveMsg( title );
  758. this.updateTitle();
  759. return this;
  760. };
  761. /**
  762. * Get the tool #title.
  763. *
  764. * @return {string} Title text
  765. */
  766. OO.ui.Tool.prototype.getTitle = function () {
  767. return this.title;
  768. };
  769. /**
  770. * Get the tool's symbolic name.
  771. *
  772. * @return {string} Symbolic name of tool
  773. */
  774. OO.ui.Tool.prototype.getName = function () {
  775. return this.constructor.static.name;
  776. };
  777. /**
  778. * Update the title.
  779. */
  780. OO.ui.Tool.prototype.updateTitle = function () {
  781. var titleTooltips = this.toolGroup.constructor.static.titleTooltips,
  782. accelTooltips = this.toolGroup.constructor.static.accelTooltips,
  783. accel = this.toolbar.getToolAccelerator( this.constructor.static.name ),
  784. tooltipParts = [];
  785. this.$title.text( this.title );
  786. this.$accel.text( accel );
  787. if ( titleTooltips && typeof this.title === 'string' && this.title.length ) {
  788. tooltipParts.push( this.title );
  789. }
  790. if ( accelTooltips && typeof accel === 'string' && accel.length ) {
  791. tooltipParts.push( accel );
  792. }
  793. if ( tooltipParts.length ) {
  794. this.$link.attr( 'title', tooltipParts.join( ' ' ) );
  795. } else {
  796. this.$link.removeAttr( 'title' );
  797. }
  798. };
  799. /**
  800. * Destroy tool.
  801. *
  802. * Destroying the tool removes all event handlers and the tool’s DOM elements.
  803. * Call this method whenever you are done using a tool.
  804. */
  805. OO.ui.Tool.prototype.destroy = function () {
  806. this.toolbar.disconnect( this );
  807. this.$element.remove();
  808. };
  809. /**
  810. * ToolGroups are collections of {@link OO.ui.Tool tools} that are used in a {@link OO.ui.Toolbar toolbar}.
  811. * The type of toolgroup ({@link OO.ui.ListToolGroup list}, {@link OO.ui.BarToolGroup bar}, or {@link OO.ui.MenuToolGroup menu})
  812. * to which a tool belongs determines how the tool is arranged and displayed in the toolbar. Toolgroups
  813. * themselves are created on demand with a {@link OO.ui.ToolGroupFactory toolgroup factory}.
  814. *
  815. * Toolgroups can contain individual tools, groups of tools, or all available tools, as specified
  816. * using the `include` config option. See OO.ui.ToolFactory#extract on documentation of the format.
  817. * The options `exclude`, `promote`, and `demote` support the same formats.
  818. *
  819. * See {@link OO.ui.Toolbar toolbars} for a full example. For more information about toolbars in general,
  820. * please see the [OOjs UI documentation on MediaWiki][1].
  821. *
  822. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  823. *
  824. * @abstract
  825. * @class
  826. * @extends OO.ui.Widget
  827. * @mixins OO.ui.mixin.GroupElement
  828. *
  829. * @constructor
  830. * @param {OO.ui.Toolbar} toolbar
  831. * @param {Object} [config] Configuration options
  832. * @cfg {Array|string} [include] List of tools to include in the toolgroup, see above.
  833. * @cfg {Array|string} [exclude] List of tools to exclude from the toolgroup, see above.
  834. * @cfg {Array|string} [promote] List of tools to promote to the beginning of the toolgroup, see above.
  835. * @cfg {Array|string} [demote] List of tools to demote to the end of the toolgroup, see above.
  836. * This setting is particularly useful when tools have been added to the toolgroup
  837. * en masse (e.g., via the catch-all selector).
  838. */
  839. OO.ui.ToolGroup = function OoUiToolGroup( toolbar, config ) {
  840. // Allow passing positional parameters inside the config object
  841. if ( OO.isPlainObject( toolbar ) && config === undefined ) {
  842. config = toolbar;
  843. toolbar = config.toolbar;
  844. }
  845. // Configuration initialization
  846. config = config || {};
  847. // Parent constructor
  848. OO.ui.ToolGroup.parent.call( this, config );
  849. // Mixin constructors
  850. OO.ui.mixin.GroupElement.call( this, config );
  851. // Properties
  852. this.toolbar = toolbar;
  853. this.tools = {};
  854. this.pressed = null;
  855. this.autoDisabled = false;
  856. this.include = config.include || [];
  857. this.exclude = config.exclude || [];
  858. this.promote = config.promote || [];
  859. this.demote = config.demote || [];
  860. this.onCapturedMouseKeyUpHandler = this.onCapturedMouseKeyUp.bind( this );
  861. // Events
  862. this.$element.on( {
  863. mousedown: this.onMouseKeyDown.bind( this ),
  864. mouseup: this.onMouseKeyUp.bind( this ),
  865. keydown: this.onMouseKeyDown.bind( this ),
  866. keyup: this.onMouseKeyUp.bind( this ),
  867. focus: this.onMouseOverFocus.bind( this ),
  868. blur: this.onMouseOutBlur.bind( this ),
  869. mouseover: this.onMouseOverFocus.bind( this ),
  870. mouseout: this.onMouseOutBlur.bind( this )
  871. } );
  872. this.toolbar.getToolFactory().connect( this, { register: 'onToolFactoryRegister' } );
  873. this.aggregate( { disable: 'itemDisable' } );
  874. this.connect( this, { itemDisable: 'updateDisabled' } );
  875. // Initialization
  876. this.$group.addClass( 'oo-ui-toolGroup-tools' );
  877. this.$element
  878. .addClass( 'oo-ui-toolGroup' )
  879. .append( this.$group );
  880. this.populate();
  881. };
  882. /* Setup */
  883. OO.inheritClass( OO.ui.ToolGroup, OO.ui.Widget );
  884. OO.mixinClass( OO.ui.ToolGroup, OO.ui.mixin.GroupElement );
  885. /* Events */
  886. /**
  887. * @event update
  888. */
  889. /* Static Properties */
  890. /**
  891. * Show labels in tooltips.
  892. *
  893. * @static
  894. * @inheritable
  895. * @property {boolean}
  896. */
  897. OO.ui.ToolGroup.static.titleTooltips = false;
  898. /**
  899. * Show acceleration labels in tooltips.
  900. *
  901. * Note: The OOjs UI library does not include an accelerator system, but does contain
  902. * a hook for one. To use an accelerator system, subclass the {@link OO.ui.Toolbar toolbar} and
  903. * override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method, which is
  904. * meant to return a label that describes the accelerator keys for a given tool (e.g., 'Ctrl + M').
  905. *
  906. * @static
  907. * @inheritable
  908. * @property {boolean}
  909. */
  910. OO.ui.ToolGroup.static.accelTooltips = false;
  911. /**
  912. * Automatically disable the toolgroup when all tools are disabled
  913. *
  914. * @static
  915. * @inheritable
  916. * @property {boolean}
  917. */
  918. OO.ui.ToolGroup.static.autoDisable = true;
  919. /**
  920. * @abstract
  921. * @static
  922. * @inheritable
  923. * @property {string}
  924. */
  925. OO.ui.ToolGroup.static.name = null;
  926. /* Methods */
  927. /**
  928. * @inheritdoc
  929. */
  930. OO.ui.ToolGroup.prototype.isDisabled = function () {
  931. return this.autoDisabled || OO.ui.ToolGroup.parent.prototype.isDisabled.apply( this, arguments );
  932. };
  933. /**
  934. * @inheritdoc
  935. */
  936. OO.ui.ToolGroup.prototype.updateDisabled = function () {
  937. var i, item, allDisabled = true;
  938. if ( this.constructor.static.autoDisable ) {
  939. for ( i = this.items.length - 1; i >= 0; i-- ) {
  940. item = this.items[ i ];
  941. if ( !item.isDisabled() ) {
  942. allDisabled = false;
  943. break;
  944. }
  945. }
  946. this.autoDisabled = allDisabled;
  947. }
  948. OO.ui.ToolGroup.parent.prototype.updateDisabled.apply( this, arguments );
  949. };
  950. /**
  951. * Handle mouse down and key down events.
  952. *
  953. * @protected
  954. * @param {jQuery.Event} e Mouse down or key down event
  955. */
  956. OO.ui.ToolGroup.prototype.onMouseKeyDown = function ( e ) {
  957. if (
  958. !this.isDisabled() &&
  959. ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
  960. ) {
  961. this.pressed = this.getTargetTool( e );
  962. if ( this.pressed ) {
  963. this.pressed.setActive( true );
  964. this.getElementDocument().addEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler, true );
  965. this.getElementDocument().addEventListener( 'keyup', this.onCapturedMouseKeyUpHandler, true );
  966. }
  967. return false;
  968. }
  969. };
  970. /**
  971. * Handle captured mouse up and key up events.
  972. *
  973. * @protected
  974. * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
  975. */
  976. OO.ui.ToolGroup.prototype.onCapturedMouseKeyUp = function ( e ) {
  977. this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler, true );
  978. this.getElementDocument().removeEventListener( 'keyup', this.onCapturedMouseKeyUpHandler, true );
  979. // onMouseKeyUp may be called a second time, depending on where the mouse is when the button is
  980. // released, but since `this.pressed` will no longer be true, the second call will be ignored.
  981. this.onMouseKeyUp( e );
  982. };
  983. /**
  984. * Handle mouse up and key up events.
  985. *
  986. * @protected
  987. * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
  988. */
  989. OO.ui.ToolGroup.prototype.onMouseKeyUp = function ( e ) {
  990. var tool = this.getTargetTool( e );
  991. if (
  992. !this.isDisabled() && this.pressed && this.pressed === tool &&
  993. ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
  994. ) {
  995. this.pressed.onSelect();
  996. this.pressed = null;
  997. e.preventDefault();
  998. e.stopPropagation();
  999. }
  1000. this.pressed = null;
  1001. };
  1002. /**
  1003. * Handle mouse over and focus events.
  1004. *
  1005. * @protected
  1006. * @param {jQuery.Event} e Mouse over or focus event
  1007. */
  1008. OO.ui.ToolGroup.prototype.onMouseOverFocus = function ( e ) {
  1009. var tool = this.getTargetTool( e );
  1010. if ( this.pressed && this.pressed === tool ) {
  1011. this.pressed.setActive( true );
  1012. }
  1013. };
  1014. /**
  1015. * Handle mouse out and blur events.
  1016. *
  1017. * @protected
  1018. * @param {jQuery.Event} e Mouse out or blur event
  1019. */
  1020. OO.ui.ToolGroup.prototype.onMouseOutBlur = function ( e ) {
  1021. var tool = this.getTargetTool( e );
  1022. if ( this.pressed && this.pressed === tool ) {
  1023. this.pressed.setActive( false );
  1024. }
  1025. };
  1026. /**
  1027. * Get the closest tool to a jQuery.Event.
  1028. *
  1029. * Only tool links are considered, which prevents other elements in the tool such as popups from
  1030. * triggering tool group interactions.
  1031. *
  1032. * @private
  1033. * @param {jQuery.Event} e
  1034. * @return {OO.ui.Tool|null} Tool, `null` if none was found
  1035. */
  1036. OO.ui.ToolGroup.prototype.getTargetTool = function ( e ) {
  1037. var tool,
  1038. $item = $( e.target ).closest( '.oo-ui-tool-link' );
  1039. if ( $item.length ) {
  1040. tool = $item.parent().data( 'oo-ui-tool' );
  1041. }
  1042. return tool && !tool.isDisabled() ? tool : null;
  1043. };
  1044. /**
  1045. * Handle tool registry register events.
  1046. *
  1047. * If a tool is registered after the group is created, we must repopulate the list to account for:
  1048. *
  1049. * - a tool being added that may be included
  1050. * - a tool already included being overridden
  1051. *
  1052. * @protected
  1053. * @param {string} name Symbolic name of tool
  1054. */
  1055. OO.ui.ToolGroup.prototype.onToolFactoryRegister = function () {
  1056. this.populate();
  1057. };
  1058. /**
  1059. * Get the toolbar that contains the toolgroup.
  1060. *
  1061. * @return {OO.ui.Toolbar} Toolbar that contains the toolgroup
  1062. */
  1063. OO.ui.ToolGroup.prototype.getToolbar = function () {
  1064. return this.toolbar;
  1065. };
  1066. /**
  1067. * Add and remove tools based on configuration.
  1068. */
  1069. OO.ui.ToolGroup.prototype.populate = function () {
  1070. var i, len, name, tool,
  1071. toolFactory = this.toolbar.getToolFactory(),
  1072. names = {},
  1073. add = [],
  1074. remove = [],
  1075. list = this.toolbar.getToolFactory().getTools(
  1076. this.include, this.exclude, this.promote, this.demote
  1077. );
  1078. // Build a list of needed tools
  1079. for ( i = 0, len = list.length; i < len; i++ ) {
  1080. name = list[ i ];
  1081. if (
  1082. // Tool exists
  1083. toolFactory.lookup( name ) &&
  1084. // Tool is available or is already in this group
  1085. ( this.toolbar.isToolAvailable( name ) || this.tools[ name ] )
  1086. ) {
  1087. // Hack to prevent infinite recursion via ToolGroupTool. We need to reserve the tool before
  1088. // creating it, but we can't call reserveTool() yet because we haven't created the tool.
  1089. this.toolbar.tools[ name ] = true;
  1090. tool = this.tools[ name ];
  1091. if ( !tool ) {
  1092. // Auto-initialize tools on first use
  1093. this.tools[ name ] = tool = toolFactory.create( name, this );
  1094. tool.updateTitle();
  1095. }
  1096. this.toolbar.reserveTool( tool );
  1097. add.push( tool );
  1098. names[ name ] = true;
  1099. }
  1100. }
  1101. // Remove tools that are no longer needed
  1102. for ( name in this.tools ) {
  1103. if ( !names[ name ] ) {
  1104. this.tools[ name ].destroy();
  1105. this.toolbar.releaseTool( this.tools[ name ] );
  1106. remove.push( this.tools[ name ] );
  1107. delete this.tools[ name ];
  1108. }
  1109. }
  1110. if ( remove.length ) {
  1111. this.removeItems( remove );
  1112. }
  1113. // Update emptiness state
  1114. if ( add.length ) {
  1115. this.$element.removeClass( 'oo-ui-toolGroup-empty' );
  1116. } else {
  1117. this.$element.addClass( 'oo-ui-toolGroup-empty' );
  1118. }
  1119. // Re-add tools (moving existing ones to new locations)
  1120. this.addItems( add );
  1121. // Disabled state may depend on items
  1122. this.updateDisabled();
  1123. };
  1124. /**
  1125. * Destroy toolgroup.
  1126. */
  1127. OO.ui.ToolGroup.prototype.destroy = function () {
  1128. var name;
  1129. this.clearItems();
  1130. this.toolbar.getToolFactory().disconnect( this );
  1131. for ( name in this.tools ) {
  1132. this.toolbar.releaseTool( this.tools[ name ] );
  1133. this.tools[ name ].disconnect( this ).destroy();
  1134. delete this.tools[ name ];
  1135. }
  1136. this.$element.remove();
  1137. };
  1138. /**
  1139. * A ToolFactory creates tools on demand. All tools ({@link OO.ui.Tool Tools}, {@link OO.ui.PopupTool PopupTools},
  1140. * and {@link OO.ui.ToolGroupTool ToolGroupTools}) must be registered with a tool factory. Tools are
  1141. * registered by their symbolic name. See {@link OO.ui.Toolbar toolbars} for an example.
  1142. *
  1143. * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
  1144. *
  1145. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  1146. *
  1147. * @class
  1148. * @extends OO.Factory
  1149. * @constructor
  1150. */
  1151. OO.ui.ToolFactory = function OoUiToolFactory() {
  1152. // Parent constructor
  1153. OO.ui.ToolFactory.parent.call( this );
  1154. };
  1155. /* Setup */
  1156. OO.inheritClass( OO.ui.ToolFactory, OO.Factory );
  1157. /* Methods */
  1158. /**
  1159. * Get tools from the factory
  1160. *
  1161. * @param {Array|string} [include] Included tools, see #extract for format
  1162. * @param {Array|string} [exclude] Excluded tools, see #extract for format
  1163. * @param {Array|string} [promote] Promoted tools, see #extract for format
  1164. * @param {Array|string} [demote] Demoted tools, see #extract for format
  1165. * @return {string[]} List of tools
  1166. */
  1167. OO.ui.ToolFactory.prototype.getTools = function ( include, exclude, promote, demote ) {
  1168. var i, len, included, promoted, demoted,
  1169. auto = [],
  1170. used = {};
  1171. // Collect included and not excluded tools
  1172. included = OO.simpleArrayDifference( this.extract( include ), this.extract( exclude ) );
  1173. // Promotion
  1174. promoted = this.extract( promote, used );
  1175. demoted = this.extract( demote, used );
  1176. // Auto
  1177. for ( i = 0, len = included.length; i < len; i++ ) {
  1178. if ( !used[ included[ i ] ] ) {
  1179. auto.push( included[ i ] );
  1180. }
  1181. }
  1182. return promoted.concat( auto ).concat( demoted );
  1183. };
  1184. /**
  1185. * Get a flat list of names from a list of names or groups.
  1186. *
  1187. * Normally, `collection` is an array of tool specifications. Tools can be specified in the
  1188. * following ways:
  1189. *
  1190. * - To include an individual tool, use the symbolic name: `{ name: 'tool-name' }` or `'tool-name'`.
  1191. * - To include all tools in a group, use the group name: `{ group: 'group-name' }`. (To assign the
  1192. * tool to a group, use OO.ui.Tool.static.group.)
  1193. *
  1194. * Alternatively, to include all tools that are not yet assigned to any other toolgroup, use the
  1195. * catch-all selector `'*'`.
  1196. *
  1197. * If `used` is passed, tool names that appear as properties in this object will be considered
  1198. * already assigned, and will not be returned even if specified otherwise. The tool names extracted
  1199. * by this function call will be added as new properties in the object.
  1200. *
  1201. * @private
  1202. * @param {Array|string} collection List of tools, see above
  1203. * @param {Object} [used] Object containing information about used tools, see above
  1204. * @return {string[]} List of extracted tool names
  1205. */
  1206. OO.ui.ToolFactory.prototype.extract = function ( collection, used ) {
  1207. var i, len, item, name, tool,
  1208. names = [];
  1209. collection = !Array.isArray( collection ) ? [ collection ] : collection;
  1210. for ( i = 0, len = collection.length; i < len; i++ ) {
  1211. item = collection[ i ];
  1212. if ( item === '*' ) {
  1213. for ( name in this.registry ) {
  1214. tool = this.registry[ name ];
  1215. if (
  1216. // Only add tools by group name when auto-add is enabled
  1217. tool.static.autoAddToCatchall &&
  1218. // Exclude already used tools
  1219. ( !used || !used[ name ] )
  1220. ) {
  1221. names.push( name );
  1222. if ( used ) {
  1223. used[ name ] = true;
  1224. }
  1225. }
  1226. }
  1227. } else {
  1228. // Allow plain strings as shorthand for named tools
  1229. if ( typeof item === 'string' ) {
  1230. item = { name: item };
  1231. }
  1232. if ( OO.isPlainObject( item ) ) {
  1233. if ( item.group ) {
  1234. for ( name in this.registry ) {
  1235. tool = this.registry[ name ];
  1236. if (
  1237. // Include tools with matching group
  1238. tool.static.group === item.group &&
  1239. // Only add tools by group name when auto-add is enabled
  1240. tool.static.autoAddToGroup &&
  1241. // Exclude already used tools
  1242. ( !used || !used[ name ] )
  1243. ) {
  1244. names.push( name );
  1245. if ( used ) {
  1246. used[ name ] = true;
  1247. }
  1248. }
  1249. }
  1250. // Include tools with matching name and exclude already used tools
  1251. } else if ( item.name && ( !used || !used[ item.name ] ) ) {
  1252. names.push( item.name );
  1253. if ( used ) {
  1254. used[ item.name ] = true;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. }
  1260. return names;
  1261. };
  1262. /**
  1263. * ToolGroupFactories create {@link OO.ui.ToolGroup toolgroups} on demand. The toolgroup classes must
  1264. * specify a symbolic name and be registered with the factory. The following classes are registered by
  1265. * default:
  1266. *
  1267. * - {@link OO.ui.BarToolGroup BarToolGroups} (‘bar’)
  1268. * - {@link OO.ui.MenuToolGroup MenuToolGroups} (‘menu’)
  1269. * - {@link OO.ui.ListToolGroup ListToolGroups} (‘list’)
  1270. *
  1271. * See {@link OO.ui.Toolbar toolbars} for an example.
  1272. *
  1273. * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
  1274. *
  1275. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  1276. *
  1277. * @class
  1278. * @extends OO.Factory
  1279. * @constructor
  1280. */
  1281. OO.ui.ToolGroupFactory = function OoUiToolGroupFactory() {
  1282. var i, l, defaultClasses;
  1283. // Parent constructor
  1284. OO.Factory.call( this );
  1285. defaultClasses = this.constructor.static.getDefaultClasses();
  1286. // Register default toolgroups
  1287. for ( i = 0, l = defaultClasses.length; i < l; i++ ) {
  1288. this.register( defaultClasses[ i ] );
  1289. }
  1290. };
  1291. /* Setup */
  1292. OO.inheritClass( OO.ui.ToolGroupFactory, OO.Factory );
  1293. /* Static Methods */
  1294. /**
  1295. * Get a default set of classes to be registered on construction.
  1296. *
  1297. * @return {Function[]} Default classes
  1298. */
  1299. OO.ui.ToolGroupFactory.static.getDefaultClasses = function () {
  1300. return [
  1301. OO.ui.BarToolGroup,
  1302. OO.ui.ListToolGroup,
  1303. OO.ui.MenuToolGroup
  1304. ];
  1305. };
  1306. /**
  1307. * Popup tools open a popup window when they are selected from the {@link OO.ui.Toolbar toolbar}. Each popup tool is configured
  1308. * with a static name, title, and icon, as well with as any popup configurations. Unlike other tools, popup tools do not require that developers specify
  1309. * an #onSelect or #onUpdateState method, as these methods have been implemented already.
  1310. *
  1311. * // Example of a popup tool. When selected, a popup tool displays
  1312. * // a popup window.
  1313. * function HelpTool( toolGroup, config ) {
  1314. * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
  1315. * padded: true,
  1316. * label: 'Help',
  1317. * head: true
  1318. * } }, config ) );
  1319. * this.popup.$body.append( '<p>I am helpful!</p>' );
  1320. * };
  1321. * OO.inheritClass( HelpTool, OO.ui.PopupTool );
  1322. * HelpTool.static.name = 'help';
  1323. * HelpTool.static.icon = 'help';
  1324. * HelpTool.static.title = 'Help';
  1325. * toolFactory.register( HelpTool );
  1326. *
  1327. * For an example of a toolbar that contains a popup tool, see {@link OO.ui.Toolbar toolbars}. For more information about
  1328. * toolbars in genreral, please see the [OOjs UI documentation on MediaWiki][1].
  1329. *
  1330. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  1331. *
  1332. * @abstract
  1333. * @class
  1334. * @extends OO.ui.Tool
  1335. * @mixins OO.ui.mixin.PopupElement
  1336. *
  1337. * @constructor
  1338. * @param {OO.ui.ToolGroup} toolGroup
  1339. * @param {Object} [config] Configuration options
  1340. */
  1341. OO.ui.PopupTool = function OoUiPopupTool( toolGroup, config ) {
  1342. // Allow passing positional parameters inside the config object
  1343. if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
  1344. config = toolGroup;
  1345. toolGroup = config.toolGroup;
  1346. }
  1347. // Parent constructor
  1348. OO.ui.PopupTool.parent.call( this, toolGroup, config );
  1349. // Mixin constructors
  1350. OO.ui.mixin.PopupElement.call( this, config );
  1351. // Initialization
  1352. this.popup.setPosition( toolGroup.getToolbar().position === 'bottom' ? 'above' : 'below' );
  1353. this.$element
  1354. .addClass( 'oo-ui-popupTool' )
  1355. .append( this.popup.$element );
  1356. };
  1357. /* Setup */
  1358. OO.inheritClass( OO.ui.PopupTool, OO.ui.Tool );
  1359. OO.mixinClass( OO.ui.PopupTool, OO.ui.mixin.PopupElement );
  1360. /* Methods */
  1361. /**
  1362. * Handle the tool being selected.
  1363. *
  1364. * @inheritdoc
  1365. */
  1366. OO.ui.PopupTool.prototype.onSelect = function () {
  1367. if ( !this.isDisabled() ) {
  1368. this.popup.toggle();
  1369. }
  1370. this.setActive( false );
  1371. return false;
  1372. };
  1373. /**
  1374. * Handle the toolbar state being updated.
  1375. *
  1376. * @inheritdoc
  1377. */
  1378. OO.ui.PopupTool.prototype.onUpdateState = function () {
  1379. this.setActive( false );
  1380. };
  1381. /**
  1382. * A ToolGroupTool is a special sort of tool that can contain other {@link OO.ui.Tool tools}
  1383. * and {@link OO.ui.ToolGroup toolgroups}. The ToolGroupTool was specifically designed to be used
  1384. * inside a {@link OO.ui.BarToolGroup bar} toolgroup to provide access to additional tools from
  1385. * the bar item. Included tools will be displayed in a dropdown {@link OO.ui.ListToolGroup list}
  1386. * when the ToolGroupTool is selected.
  1387. *
  1388. * // Example: ToolGroupTool with two nested tools, 'setting1' and 'setting2', defined elsewhere.
  1389. *
  1390. * function SettingsTool() {
  1391. * SettingsTool.parent.apply( this, arguments );
  1392. * };
  1393. * OO.inheritClass( SettingsTool, OO.ui.ToolGroupTool );
  1394. * SettingsTool.static.name = 'settings';
  1395. * SettingsTool.static.title = 'Change settings';
  1396. * SettingsTool.static.groupConfig = {
  1397. * icon: 'settings',
  1398. * label: 'ToolGroupTool',
  1399. * include: [ 'setting1', 'setting2' ]
  1400. * };
  1401. * toolFactory.register( SettingsTool );
  1402. *
  1403. * For more information, please see the [OOjs UI documentation on MediaWiki][1].
  1404. *
  1405. * Please note that this implementation is subject to change per [T74159] [2].
  1406. *
  1407. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars#ToolGroupTool
  1408. * [2]: https://phabricator.wikimedia.org/T74159
  1409. *
  1410. * @abstract
  1411. * @class
  1412. * @extends OO.ui.Tool
  1413. *
  1414. * @constructor
  1415. * @param {OO.ui.ToolGroup} toolGroup
  1416. * @param {Object} [config] Configuration options
  1417. */
  1418. OO.ui.ToolGroupTool = function OoUiToolGroupTool( toolGroup, config ) {
  1419. // Allow passing positional parameters inside the config object
  1420. if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
  1421. config = toolGroup;
  1422. toolGroup = config.toolGroup;
  1423. }
  1424. // Parent constructor
  1425. OO.ui.ToolGroupTool.parent.call( this, toolGroup, config );
  1426. // Properties
  1427. this.innerToolGroup = this.createGroup( this.constructor.static.groupConfig );
  1428. // Events
  1429. this.innerToolGroup.connect( this, { disable: 'onToolGroupDisable' } );
  1430. // Initialization
  1431. this.$link.remove();
  1432. this.$element
  1433. .addClass( 'oo-ui-toolGroupTool' )
  1434. .append( this.innerToolGroup.$element );
  1435. };
  1436. /* Setup */
  1437. OO.inheritClass( OO.ui.ToolGroupTool, OO.ui.Tool );
  1438. /* Static Properties */
  1439. /**
  1440. * Toolgroup configuration.
  1441. *
  1442. * The toolgroup configuration consists of the tools to include, as well as an icon and label
  1443. * to use for the bar item. Tools can be included by symbolic name, group, or with the
  1444. * wildcard selector. Please see {@link OO.ui.ToolGroup toolgroup} for more information.
  1445. *
  1446. * @property {Object.<string,Array>}
  1447. */
  1448. OO.ui.ToolGroupTool.static.groupConfig = {};
  1449. /* Methods */
  1450. /**
  1451. * Handle the tool being selected.
  1452. *
  1453. * @inheritdoc
  1454. */
  1455. OO.ui.ToolGroupTool.prototype.onSelect = function () {
  1456. this.innerToolGroup.setActive( !this.innerToolGroup.active );
  1457. return false;
  1458. };
  1459. /**
  1460. * Synchronize disabledness state of the tool with the inner toolgroup.
  1461. *
  1462. * @private
  1463. * @param {boolean} disabled Element is disabled
  1464. */
  1465. OO.ui.ToolGroupTool.prototype.onToolGroupDisable = function ( disabled ) {
  1466. this.setDisabled( disabled );
  1467. };
  1468. /**
  1469. * Handle the toolbar state being updated.
  1470. *
  1471. * @inheritdoc
  1472. */
  1473. OO.ui.ToolGroupTool.prototype.onUpdateState = function () {
  1474. this.setActive( false );
  1475. };
  1476. /**
  1477. * Build a {@link OO.ui.ToolGroup toolgroup} from the specified configuration.
  1478. *
  1479. * @param {Object.<string,Array>} group Toolgroup configuration. Please see {@link OO.ui.ToolGroup toolgroup} for
  1480. * more information.
  1481. * @return {OO.ui.ListToolGroup}
  1482. */
  1483. OO.ui.ToolGroupTool.prototype.createGroup = function ( group ) {
  1484. if ( group.include === '*' ) {
  1485. // Apply defaults to catch-all groups
  1486. if ( group.label === undefined ) {
  1487. group.label = OO.ui.msg( 'ooui-toolbar-more' );
  1488. }
  1489. }
  1490. return this.toolbar.getToolGroupFactory().create( 'list', this.toolbar, group );
  1491. };
  1492. /**
  1493. * BarToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
  1494. * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
  1495. * and {@link OO.ui.ListToolGroup ListToolGroup}). The {@link OO.ui.Tool tools} in a BarToolGroup are
  1496. * displayed by icon in a single row. The title of the tool is displayed when users move the mouse over
  1497. * the tool.
  1498. *
  1499. * BarToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar is
  1500. * set up.
  1501. *
  1502. * @example
  1503. * // Example of a BarToolGroup with two tools
  1504. * var toolFactory = new OO.ui.ToolFactory();
  1505. * var toolGroupFactory = new OO.ui.ToolGroupFactory();
  1506. * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
  1507. *
  1508. * // We will be placing status text in this element when tools are used
  1509. * var $area = $( '<p>' ).text( 'Example of a BarToolGroup with two tools.' );
  1510. *
  1511. * // Define the tools that we're going to place in our toolbar
  1512. *
  1513. * // Create a class inheriting from OO.ui.Tool
  1514. * function SearchTool() {
  1515. * SearchTool.parent.apply( this, arguments );
  1516. * }
  1517. * OO.inheritClass( SearchTool, OO.ui.Tool );
  1518. * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
  1519. * // of 'icon' and 'title' (displayed icon and text).
  1520. * SearchTool.static.name = 'search';
  1521. * SearchTool.static.icon = 'search';
  1522. * SearchTool.static.title = 'Search...';
  1523. * // Defines the action that will happen when this tool is selected (clicked).
  1524. * SearchTool.prototype.onSelect = function () {
  1525. * $area.text( 'Search tool clicked!' );
  1526. * // Never display this tool as "active" (selected).
  1527. * this.setActive( false );
  1528. * };
  1529. * SearchTool.prototype.onUpdateState = function () {};
  1530. * // Make this tool available in our toolFactory and thus our toolbar
  1531. * toolFactory.register( SearchTool );
  1532. *
  1533. * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
  1534. * // little popup window (a PopupWidget).
  1535. * function HelpTool( toolGroup, config ) {
  1536. * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
  1537. * padded: true,
  1538. * label: 'Help',
  1539. * head: true
  1540. * } }, config ) );
  1541. * this.popup.$body.append( '<p>I am helpful!</p>' );
  1542. * }
  1543. * OO.inheritClass( HelpTool, OO.ui.PopupTool );
  1544. * HelpTool.static.name = 'help';
  1545. * HelpTool.static.icon = 'help';
  1546. * HelpTool.static.title = 'Help';
  1547. * toolFactory.register( HelpTool );
  1548. *
  1549. * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
  1550. * // used once (but not all defined tools must be used).
  1551. * toolbar.setup( [
  1552. * {
  1553. * // 'bar' tool groups display tools by icon only
  1554. * type: 'bar',
  1555. * include: [ 'search', 'help' ]
  1556. * }
  1557. * ] );
  1558. *
  1559. * // Create some UI around the toolbar and place it in the document
  1560. * var frame = new OO.ui.PanelLayout( {
  1561. * expanded: false,
  1562. * framed: true
  1563. * } );
  1564. * var contentFrame = new OO.ui.PanelLayout( {
  1565. * expanded: false,
  1566. * padded: true
  1567. * } );
  1568. * frame.$element.append(
  1569. * toolbar.$element,
  1570. * contentFrame.$element.append( $area )
  1571. * );
  1572. * $( 'body' ).append( frame.$element );
  1573. *
  1574. * // Here is where the toolbar is actually built. This must be done after inserting it into the
  1575. * // document.
  1576. * toolbar.initialize();
  1577. *
  1578. * For more information about how to add tools to a bar tool group, please see {@link OO.ui.ToolGroup toolgroup}.
  1579. * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
  1580. *
  1581. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  1582. *
  1583. * @class
  1584. * @extends OO.ui.ToolGroup
  1585. *
  1586. * @constructor
  1587. * @param {OO.ui.Toolbar} toolbar
  1588. * @param {Object} [config] Configuration options
  1589. */
  1590. OO.ui.BarToolGroup = function OoUiBarToolGroup( toolbar, config ) {
  1591. // Allow passing positional parameters inside the config object
  1592. if ( OO.isPlainObject( toolbar ) && config === undefined ) {
  1593. config = toolbar;
  1594. toolbar = config.toolbar;
  1595. }
  1596. // Parent constructor
  1597. OO.ui.BarToolGroup.parent.call( this, toolbar, config );
  1598. // Initialization
  1599. this.$element.addClass( 'oo-ui-barToolGroup' );
  1600. };
  1601. /* Setup */
  1602. OO.inheritClass( OO.ui.BarToolGroup, OO.ui.ToolGroup );
  1603. /* Static Properties */
  1604. /**
  1605. * @static
  1606. * @inheritdoc
  1607. */
  1608. OO.ui.BarToolGroup.static.titleTooltips = true;
  1609. /**
  1610. * @static
  1611. * @inheritdoc
  1612. */
  1613. OO.ui.BarToolGroup.static.accelTooltips = true;
  1614. /**
  1615. * @static
  1616. * @inheritdoc
  1617. */
  1618. OO.ui.BarToolGroup.static.name = 'bar';
  1619. /**
  1620. * PopupToolGroup is an abstract base class used by both {@link OO.ui.MenuToolGroup MenuToolGroup}
  1621. * and {@link OO.ui.ListToolGroup ListToolGroup} to provide a popup--an overlaid menu or list of tools with an
  1622. * optional icon and label. This class can be used for other base classes that also use this functionality.
  1623. *
  1624. * @abstract
  1625. * @class
  1626. * @extends OO.ui.ToolGroup
  1627. * @mixins OO.ui.mixin.IconElement
  1628. * @mixins OO.ui.mixin.IndicatorElement
  1629. * @mixins OO.ui.mixin.LabelElement
  1630. * @mixins OO.ui.mixin.TitledElement
  1631. * @mixins OO.ui.mixin.FlaggedElement
  1632. * @mixins OO.ui.mixin.ClippableElement
  1633. * @mixins OO.ui.mixin.TabIndexedElement
  1634. *
  1635. * @constructor
  1636. * @param {OO.ui.Toolbar} toolbar
  1637. * @param {Object} [config] Configuration options
  1638. * @cfg {string} [header] Text to display at the top of the popup
  1639. */
  1640. OO.ui.PopupToolGroup = function OoUiPopupToolGroup( toolbar, config ) {
  1641. // Allow passing positional parameters inside the config object
  1642. if ( OO.isPlainObject( toolbar ) && config === undefined ) {
  1643. config = toolbar;
  1644. toolbar = config.toolbar;
  1645. }
  1646. // Configuration initialization
  1647. config = $.extend( {
  1648. indicator: config.indicator === undefined ? ( toolbar.position === 'bottom' ? 'up' : 'down' ) : config.indicator
  1649. }, config );
  1650. // Parent constructor
  1651. OO.ui.PopupToolGroup.parent.call( this, toolbar, config );
  1652. // Properties
  1653. this.active = false;
  1654. this.dragging = false;
  1655. this.onBlurHandler = this.onBlur.bind( this );
  1656. this.$handle = $( '<span>' );
  1657. // Mixin constructors
  1658. OO.ui.mixin.IconElement.call( this, config );
  1659. OO.ui.mixin.IndicatorElement.call( this, config );
  1660. OO.ui.mixin.LabelElement.call( this, config );
  1661. OO.ui.mixin.TitledElement.call( this, config );
  1662. OO.ui.mixin.FlaggedElement.call( this, config );
  1663. OO.ui.mixin.ClippableElement.call( this, $.extend( {}, config, { $clippable: this.$group } ) );
  1664. OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$handle } ) );
  1665. // Events
  1666. this.$handle.on( {
  1667. keydown: this.onHandleMouseKeyDown.bind( this ),
  1668. keyup: this.onHandleMouseKeyUp.bind( this ),
  1669. mousedown: this.onHandleMouseKeyDown.bind( this ),
  1670. mouseup: this.onHandleMouseKeyUp.bind( this )
  1671. } );
  1672. // Initialization
  1673. this.$handle
  1674. .addClass( 'oo-ui-popupToolGroup-handle' )
  1675. .attr( 'role', 'button' )
  1676. .append( this.$icon, this.$label, this.$indicator );
  1677. // If the pop-up should have a header, add it to the top of the toolGroup.
  1678. // Note: If this feature is useful for other widgets, we could abstract it into an
  1679. // OO.ui.HeaderedElement mixin constructor.
  1680. if ( config.header !== undefined ) {
  1681. this.$group
  1682. .prepend( $( '<span>' )
  1683. .addClass( 'oo-ui-popupToolGroup-header' )
  1684. .text( config.header )
  1685. );
  1686. }
  1687. this.$element
  1688. .addClass( 'oo-ui-popupToolGroup' )
  1689. .prepend( this.$handle );
  1690. };
  1691. /* Setup */
  1692. OO.inheritClass( OO.ui.PopupToolGroup, OO.ui.ToolGroup );
  1693. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IconElement );
  1694. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IndicatorElement );
  1695. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.LabelElement );
  1696. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TitledElement );
  1697. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.FlaggedElement );
  1698. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.ClippableElement );
  1699. OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TabIndexedElement );
  1700. /* Methods */
  1701. /**
  1702. * @inheritdoc
  1703. */
  1704. OO.ui.PopupToolGroup.prototype.setDisabled = function () {
  1705. // Parent method
  1706. OO.ui.PopupToolGroup.parent.prototype.setDisabled.apply( this, arguments );
  1707. if ( this.isDisabled() && this.isElementAttached() ) {
  1708. this.setActive( false );
  1709. }
  1710. };
  1711. /**
  1712. * Handle focus being lost.
  1713. *
  1714. * The event is actually generated from a mouseup/keyup, so it is not a normal blur event object.
  1715. *
  1716. * @protected
  1717. * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
  1718. */
  1719. OO.ui.PopupToolGroup.prototype.onBlur = function ( e ) {
  1720. // Only deactivate when clicking outside the dropdown element
  1721. if ( $( e.target ).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element[ 0 ] ) {
  1722. this.setActive( false );
  1723. }
  1724. };
  1725. /**
  1726. * @inheritdoc
  1727. */
  1728. OO.ui.PopupToolGroup.prototype.onMouseKeyUp = function ( e ) {
  1729. // Only close toolgroup when a tool was actually selected
  1730. if (
  1731. !this.isDisabled() && this.pressed && this.pressed === this.getTargetTool( e ) &&
  1732. ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
  1733. ) {
  1734. this.setActive( false );
  1735. }
  1736. return OO.ui.PopupToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
  1737. };
  1738. /**
  1739. * Handle mouse up and key up events.
  1740. *
  1741. * @protected
  1742. * @param {jQuery.Event} e Mouse up or key up event
  1743. */
  1744. OO.ui.PopupToolGroup.prototype.onHandleMouseKeyUp = function ( e ) {
  1745. if (
  1746. !this.isDisabled() &&
  1747. ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
  1748. ) {
  1749. return false;
  1750. }
  1751. };
  1752. /**
  1753. * Handle mouse down and key down events.
  1754. *
  1755. * @protected
  1756. * @param {jQuery.Event} e Mouse down or key down event
  1757. */
  1758. OO.ui.PopupToolGroup.prototype.onHandleMouseKeyDown = function ( e ) {
  1759. if (
  1760. !this.isDisabled() &&
  1761. ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
  1762. ) {
  1763. this.setActive( !this.active );
  1764. return false;
  1765. }
  1766. };
  1767. /**
  1768. * Switch into 'active' mode.
  1769. *
  1770. * When active, the popup is visible. A mouseup event anywhere in the document will trigger
  1771. * deactivation.
  1772. *
  1773. * @param {boolean} value The active state to set
  1774. */
  1775. OO.ui.PopupToolGroup.prototype.setActive = function ( value ) {
  1776. var containerWidth, containerLeft;
  1777. value = !!value;
  1778. if ( this.active !== value ) {
  1779. this.active = value;
  1780. if ( value ) {
  1781. this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler, true );
  1782. this.getElementDocument().addEventListener( 'keyup', this.onBlurHandler, true );
  1783. this.$clippable.css( 'left', '' );
  1784. // Try anchoring the popup to the left first
  1785. this.$element.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
  1786. this.toggleClipping( true );
  1787. if ( this.isClippedHorizontally() ) {
  1788. // Anchoring to the left caused the popup to clip, so anchor it to the right instead
  1789. this.toggleClipping( false );
  1790. this.$element
  1791. .removeClass( 'oo-ui-popupToolGroup-left' )
  1792. .addClass( 'oo-ui-popupToolGroup-right' );
  1793. this.toggleClipping( true );
  1794. }
  1795. if ( this.isClippedHorizontally() ) {
  1796. // Anchoring to the right also caused the popup to clip, so just make it fill the container
  1797. containerWidth = this.$clippableScrollableContainer.width();
  1798. containerLeft = this.$clippableScrollableContainer.offset().left;
  1799. this.toggleClipping( false );
  1800. this.$element.removeClass( 'oo-ui-popupToolGroup-right' );
  1801. this.$clippable.css( {
  1802. left: -( this.$element.offset().left - containerLeft ),
  1803. width: containerWidth
  1804. } );
  1805. }
  1806. } else {
  1807. this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler, true );
  1808. this.getElementDocument().removeEventListener( 'keyup', this.onBlurHandler, true );
  1809. this.$element.removeClass(
  1810. 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
  1811. );
  1812. this.toggleClipping( false );
  1813. }
  1814. }
  1815. };
  1816. /**
  1817. * ListToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
  1818. * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
  1819. * and {@link OO.ui.BarToolGroup BarToolGroup}). The {@link OO.ui.Tool tools} in a ListToolGroup are displayed
  1820. * by label in a dropdown menu. The title of the tool is used as the label text. The menu itself can be configured
  1821. * with a label, icon, indicator, header, and title.
  1822. *
  1823. * ListToolGroups can be configured to be expanded and collapsed. Collapsed lists will have a ‘More’ option that
  1824. * users can select to see the full list of tools. If a collapsed toolgroup is expanded, a ‘Fewer’ option permits
  1825. * users to collapse the list again.
  1826. *
  1827. * ListToolGroups are created by a {@link OO.ui.ToolGroupFactory toolgroup factory} when the toolbar is set up. The factory
  1828. * requires the ListToolGroup's symbolic name, 'list', which is specified along with the other configurations. For more
  1829. * information about how to add tools to a ListToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
  1830. *
  1831. * @example
  1832. * // Example of a ListToolGroup
  1833. * var toolFactory = new OO.ui.ToolFactory();
  1834. * var toolGroupFactory = new OO.ui.ToolGroupFactory();
  1835. * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
  1836. *
  1837. * // Configure and register two tools
  1838. * function SettingsTool() {
  1839. * SettingsTool.parent.apply( this, arguments );
  1840. * }
  1841. * OO.inheritClass( SettingsTool, OO.ui.Tool );
  1842. * SettingsTool.static.name = 'settings';
  1843. * SettingsTool.static.icon = 'settings';
  1844. * SettingsTool.static.title = 'Change settings';
  1845. * SettingsTool.prototype.onSelect = function () {
  1846. * this.setActive( false );
  1847. * };
  1848. * SettingsTool.prototype.onUpdateState = function () {};
  1849. * toolFactory.register( SettingsTool );
  1850. * // Register two more tools, nothing interesting here
  1851. * function StuffTool() {
  1852. * StuffTool.parent.apply( this, arguments );
  1853. * }
  1854. * OO.inheritClass( StuffTool, OO.ui.Tool );
  1855. * StuffTool.static.name = 'stuff';
  1856. * StuffTool.static.icon = 'search';
  1857. * StuffTool.static.title = 'Change the world';
  1858. * StuffTool.prototype.onSelect = function () {
  1859. * this.setActive( false );
  1860. * };
  1861. * StuffTool.prototype.onUpdateState = function () {};
  1862. * toolFactory.register( StuffTool );
  1863. * toolbar.setup( [
  1864. * {
  1865. * // Configurations for list toolgroup.
  1866. * type: 'list',
  1867. * label: 'ListToolGroup',
  1868. * icon: 'ellipsis',
  1869. * title: 'This is the title, displayed when user moves the mouse over the list toolgroup',
  1870. * header: 'This is the header',
  1871. * include: [ 'settings', 'stuff' ],
  1872. * allowCollapse: ['stuff']
  1873. * }
  1874. * ] );
  1875. *
  1876. * // Create some UI around the toolbar and place it in the document
  1877. * var frame = new OO.ui.PanelLayout( {
  1878. * expanded: false,
  1879. * framed: true
  1880. * } );
  1881. * frame.$element.append(
  1882. * toolbar.$element
  1883. * );
  1884. * $( 'body' ).append( frame.$element );
  1885. * // Build the toolbar. This must be done after the toolbar has been appended to the document.
  1886. * toolbar.initialize();
  1887. *
  1888. * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
  1889. *
  1890. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  1891. *
  1892. * @class
  1893. * @extends OO.ui.PopupToolGroup
  1894. *
  1895. * @constructor
  1896. * @param {OO.ui.Toolbar} toolbar
  1897. * @param {Object} [config] Configuration options
  1898. * @cfg {Array} [allowCollapse] Allow the specified tools to be collapsed. By default, collapsible tools
  1899. * will only be displayed if users click the ‘More’ option displayed at the bottom of the list. If
  1900. * the list is expanded, a ‘Fewer’ option permits users to collapse the list again. Any tools that
  1901. * are included in the toolgroup, but are not designated as collapsible, will always be displayed.
  1902. * To open a collapsible list in its expanded state, set #expanded to 'true'.
  1903. * @cfg {Array} [forceExpand] Expand the specified tools. All other tools will be designated as collapsible.
  1904. * Unless #expanded is set to true, the collapsible tools will be collapsed when the list is first opened.
  1905. * @cfg {boolean} [expanded=false] Expand collapsible tools. This config is only relevant if tools have
  1906. * been designated as collapsible. When expanded is set to true, all tools in the group will be displayed
  1907. * when the list is first opened. Users can collapse the list with a ‘Fewer’ option at the bottom.
  1908. */
  1909. OO.ui.ListToolGroup = function OoUiListToolGroup( toolbar, config ) {
  1910. // Allow passing positional parameters inside the config object
  1911. if ( OO.isPlainObject( toolbar ) && config === undefined ) {
  1912. config = toolbar;
  1913. toolbar = config.toolbar;
  1914. }
  1915. // Configuration initialization
  1916. config = config || {};
  1917. // Properties (must be set before parent constructor, which calls #populate)
  1918. this.allowCollapse = config.allowCollapse;
  1919. this.forceExpand = config.forceExpand;
  1920. this.expanded = config.expanded !== undefined ? config.expanded : false;
  1921. this.collapsibleTools = [];
  1922. // Parent constructor
  1923. OO.ui.ListToolGroup.parent.call( this, toolbar, config );
  1924. // Initialization
  1925. this.$element.addClass( 'oo-ui-listToolGroup' );
  1926. };
  1927. /* Setup */
  1928. OO.inheritClass( OO.ui.ListToolGroup, OO.ui.PopupToolGroup );
  1929. /* Static Properties */
  1930. /**
  1931. * @static
  1932. * @inheritdoc
  1933. */
  1934. OO.ui.ListToolGroup.static.name = 'list';
  1935. /* Methods */
  1936. /**
  1937. * @inheritdoc
  1938. */
  1939. OO.ui.ListToolGroup.prototype.populate = function () {
  1940. var i, len, allowCollapse = [];
  1941. OO.ui.ListToolGroup.parent.prototype.populate.call( this );
  1942. // Update the list of collapsible tools
  1943. if ( this.allowCollapse !== undefined ) {
  1944. allowCollapse = this.allowCollapse;
  1945. } else if ( this.forceExpand !== undefined ) {
  1946. allowCollapse = OO.simpleArrayDifference( Object.keys( this.tools ), this.forceExpand );
  1947. }
  1948. this.collapsibleTools = [];
  1949. for ( i = 0, len = allowCollapse.length; i < len; i++ ) {
  1950. if ( this.tools[ allowCollapse[ i ] ] !== undefined ) {
  1951. this.collapsibleTools.push( this.tools[ allowCollapse[ i ] ] );
  1952. }
  1953. }
  1954. // Keep at the end, even when tools are added
  1955. this.$group.append( this.getExpandCollapseTool().$element );
  1956. this.getExpandCollapseTool().toggle( this.collapsibleTools.length !== 0 );
  1957. this.updateCollapsibleState();
  1958. };
  1959. /**
  1960. * Get the expand/collapse tool for this group
  1961. *
  1962. * @return {OO.ui.Tool} Expand collapse tool
  1963. */
  1964. OO.ui.ListToolGroup.prototype.getExpandCollapseTool = function () {
  1965. var ExpandCollapseTool;
  1966. if ( this.expandCollapseTool === undefined ) {
  1967. ExpandCollapseTool = function () {
  1968. ExpandCollapseTool.parent.apply( this, arguments );
  1969. };
  1970. OO.inheritClass( ExpandCollapseTool, OO.ui.Tool );
  1971. ExpandCollapseTool.prototype.onSelect = function () {
  1972. this.toolGroup.expanded = !this.toolGroup.expanded;
  1973. this.toolGroup.updateCollapsibleState();
  1974. this.setActive( false );
  1975. };
  1976. ExpandCollapseTool.prototype.onUpdateState = function () {
  1977. // Do nothing. Tool interface requires an implementation of this function.
  1978. };
  1979. ExpandCollapseTool.static.name = 'more-fewer';
  1980. this.expandCollapseTool = new ExpandCollapseTool( this );
  1981. }
  1982. return this.expandCollapseTool;
  1983. };
  1984. /**
  1985. * @inheritdoc
  1986. */
  1987. OO.ui.ListToolGroup.prototype.onMouseKeyUp = function ( e ) {
  1988. // Do not close the popup when the user wants to show more/fewer tools
  1989. if (
  1990. $( e.target ).closest( '.oo-ui-tool-name-more-fewer' ).length &&
  1991. ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
  1992. ) {
  1993. // HACK: Prevent the popup list from being hidden. Skip the PopupToolGroup implementation (which
  1994. // hides the popup list when a tool is selected) and call ToolGroup's implementation directly.
  1995. return OO.ui.ListToolGroup.parent.parent.prototype.onMouseKeyUp.call( this, e );
  1996. } else {
  1997. return OO.ui.ListToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
  1998. }
  1999. };
  2000. OO.ui.ListToolGroup.prototype.updateCollapsibleState = function () {
  2001. var i, len;
  2002. this.getExpandCollapseTool()
  2003. .setIcon( this.expanded ? 'collapse' : 'expand' )
  2004. .setTitle( OO.ui.msg( this.expanded ? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
  2005. for ( i = 0, len = this.collapsibleTools.length; i < len; i++ ) {
  2006. this.collapsibleTools[ i ].toggle( this.expanded );
  2007. }
  2008. // Re-evaluate clipping, because our height has changed
  2009. this.clip();
  2010. };
  2011. /**
  2012. * MenuToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
  2013. * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.BarToolGroup BarToolGroup}
  2014. * and {@link OO.ui.ListToolGroup ListToolGroup}). MenuToolGroups contain selectable {@link OO.ui.Tool tools},
  2015. * which are displayed by label in a dropdown menu. The tool's title is used as the label text, and the
  2016. * menu label is updated to reflect which tool or tools are currently selected. If no tools are selected,
  2017. * the menu label is empty. The menu can be configured with an indicator, icon, title, and/or header.
  2018. *
  2019. * MenuToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar
  2020. * is set up.
  2021. *
  2022. * @example
  2023. * // Example of a MenuToolGroup
  2024. * var toolFactory = new OO.ui.ToolFactory();
  2025. * var toolGroupFactory = new OO.ui.ToolGroupFactory();
  2026. * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
  2027. *
  2028. * // We will be placing status text in this element when tools are used
  2029. * var $area = $( '<p>' ).text( 'An example of a MenuToolGroup. Select a tool from the dropdown menu.' );
  2030. *
  2031. * // Define the tools that we're going to place in our toolbar
  2032. *
  2033. * function SettingsTool() {
  2034. * SettingsTool.parent.apply( this, arguments );
  2035. * this.reallyActive = false;
  2036. * }
  2037. * OO.inheritClass( SettingsTool, OO.ui.Tool );
  2038. * SettingsTool.static.name = 'settings';
  2039. * SettingsTool.static.icon = 'settings';
  2040. * SettingsTool.static.title = 'Change settings';
  2041. * SettingsTool.prototype.onSelect = function () {
  2042. * $area.text( 'Settings tool clicked!' );
  2043. * // Toggle the active state on each click
  2044. * this.reallyActive = !this.reallyActive;
  2045. * this.setActive( this.reallyActive );
  2046. * // To update the menu label
  2047. * this.toolbar.emit( 'updateState' );
  2048. * };
  2049. * SettingsTool.prototype.onUpdateState = function () {};
  2050. * toolFactory.register( SettingsTool );
  2051. *
  2052. * function StuffTool() {
  2053. * StuffTool.parent.apply( this, arguments );
  2054. * this.reallyActive = false;
  2055. * }
  2056. * OO.inheritClass( StuffTool, OO.ui.Tool );
  2057. * StuffTool.static.name = 'stuff';
  2058. * StuffTool.static.icon = 'ellipsis';
  2059. * StuffTool.static.title = 'More stuff';
  2060. * StuffTool.prototype.onSelect = function () {
  2061. * $area.text( 'More stuff tool clicked!' );
  2062. * // Toggle the active state on each click
  2063. * this.reallyActive = !this.reallyActive;
  2064. * this.setActive( this.reallyActive );
  2065. * // To update the menu label
  2066. * this.toolbar.emit( 'updateState' );
  2067. * };
  2068. * StuffTool.prototype.onUpdateState = function () {};
  2069. * toolFactory.register( StuffTool );
  2070. *
  2071. * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
  2072. * // used once (but not all defined tools must be used).
  2073. * toolbar.setup( [
  2074. * {
  2075. * type: 'menu',
  2076. * header: 'This is the (optional) header',
  2077. * title: 'This is the (optional) title',
  2078. * include: [ 'settings', 'stuff' ]
  2079. * }
  2080. * ] );
  2081. *
  2082. * // Create some UI around the toolbar and place it in the document
  2083. * var frame = new OO.ui.PanelLayout( {
  2084. * expanded: false,
  2085. * framed: true
  2086. * } );
  2087. * var contentFrame = new OO.ui.PanelLayout( {
  2088. * expanded: false,
  2089. * padded: true
  2090. * } );
  2091. * frame.$element.append(
  2092. * toolbar.$element,
  2093. * contentFrame.$element.append( $area )
  2094. * );
  2095. * $( 'body' ).append( frame.$element );
  2096. *
  2097. * // Here is where the toolbar is actually built. This must be done after inserting it into the
  2098. * // document.
  2099. * toolbar.initialize();
  2100. * toolbar.emit( 'updateState' );
  2101. *
  2102. * For more information about how to add tools to a MenuToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
  2103. * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki] [1].
  2104. *
  2105. * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
  2106. *
  2107. * @class
  2108. * @extends OO.ui.PopupToolGroup
  2109. *
  2110. * @constructor
  2111. * @param {OO.ui.Toolbar} toolbar
  2112. * @param {Object} [config] Configuration options
  2113. */
  2114. OO.ui.MenuToolGroup = function OoUiMenuToolGroup( toolbar, config ) {
  2115. // Allow passing positional parameters inside the config object
  2116. if ( OO.isPlainObject( toolbar ) && config === undefined ) {
  2117. config = toolbar;
  2118. toolbar = config.toolbar;
  2119. }
  2120. // Configuration initialization
  2121. config = config || {};
  2122. // Parent constructor
  2123. OO.ui.MenuToolGroup.parent.call( this, toolbar, config );
  2124. // Events
  2125. this.toolbar.connect( this, { updateState: 'onUpdateState' } );
  2126. // Initialization
  2127. this.$element.addClass( 'oo-ui-menuToolGroup' );
  2128. };
  2129. /* Setup */
  2130. OO.inheritClass( OO.ui.MenuToolGroup, OO.ui.PopupToolGroup );
  2131. /* Static Properties */
  2132. /**
  2133. * @static
  2134. * @inheritdoc
  2135. */
  2136. OO.ui.MenuToolGroup.static.name = 'menu';
  2137. /* Methods */
  2138. /**
  2139. * Handle the toolbar state being updated.
  2140. *
  2141. * When the state changes, the title of each active item in the menu will be joined together and
  2142. * used as a label for the group. The label will be empty if none of the items are active.
  2143. *
  2144. * @private
  2145. */
  2146. OO.ui.MenuToolGroup.prototype.onUpdateState = function () {
  2147. var name,
  2148. labelTexts = [];
  2149. for ( name in this.tools ) {
  2150. if ( this.tools[ name ].isActive() ) {
  2151. labelTexts.push( this.tools[ name ].getTitle() );
  2152. }
  2153. }
  2154. this.setLabel( labelTexts.join( ', ' ) || ' ' );
  2155. };
  2156. }( OO ) );