plugin.js 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview The floating dialog plugin.
  7. */
  8. /**
  9. * No resize for this dialog.
  10. *
  11. * @readonly
  12. * @property {Number} [=0]
  13. * @member CKEDITOR
  14. */
  15. CKEDITOR.DIALOG_RESIZE_NONE = 0;
  16. /**
  17. * Only allow horizontal resizing for this dialog, disable vertical resizing.
  18. *
  19. * @readonly
  20. * @property {Number} [=1]
  21. * @member CKEDITOR
  22. */
  23. CKEDITOR.DIALOG_RESIZE_WIDTH = 1;
  24. /**
  25. * Only allow vertical resizing for this dialog, disable horizontal resizing.
  26. *
  27. * @readonly
  28. * @property {Number} [=2]
  29. * @member CKEDITOR
  30. */
  31. CKEDITOR.DIALOG_RESIZE_HEIGHT = 2;
  32. /**
  33. * Allow the dialog to be resized in both directions.
  34. *
  35. * @readonly
  36. * @property {Number} [=3]
  37. * @member CKEDITOR
  38. */
  39. CKEDITOR.DIALOG_RESIZE_BOTH = 3;
  40. /**
  41. * Dialog state when idle.
  42. *
  43. * @readonly
  44. * @property {Number} [=1]
  45. * @member CKEDITOR
  46. */
  47. CKEDITOR.DIALOG_STATE_IDLE = 1;
  48. /**
  49. * Dialog state when busy.
  50. *
  51. * @readonly
  52. * @property {Number} [=2]
  53. * @member CKEDITOR
  54. */
  55. CKEDITOR.DIALOG_STATE_BUSY = 2;
  56. ( function() {
  57. var cssLength = CKEDITOR.tools.cssLength;
  58. function isTabVisible( tabId ) {
  59. return !!this._.tabs[ tabId ][ 0 ].$.offsetHeight;
  60. }
  61. function getPreviousVisibleTab() {
  62. var tabId = this._.currentTabId,
  63. length = this._.tabIdList.length,
  64. tabIndex = CKEDITOR.tools.indexOf( this._.tabIdList, tabId ) + length;
  65. for ( var i = tabIndex - 1; i > tabIndex - length; i-- ) {
  66. if ( isTabVisible.call( this, this._.tabIdList[ i % length ] ) )
  67. return this._.tabIdList[ i % length ];
  68. }
  69. return null;
  70. }
  71. function getNextVisibleTab() {
  72. var tabId = this._.currentTabId,
  73. length = this._.tabIdList.length,
  74. tabIndex = CKEDITOR.tools.indexOf( this._.tabIdList, tabId );
  75. for ( var i = tabIndex + 1; i < tabIndex + length; i++ ) {
  76. if ( isTabVisible.call( this, this._.tabIdList[ i % length ] ) )
  77. return this._.tabIdList[ i % length ];
  78. }
  79. return null;
  80. }
  81. function clearOrRecoverTextInputValue( container, isRecover ) {
  82. var inputs = container.$.getElementsByTagName( 'input' );
  83. for ( var i = 0, length = inputs.length; i < length; i++ ) {
  84. var item = new CKEDITOR.dom.element( inputs[ i ] );
  85. if ( item.getAttribute( 'type' ).toLowerCase() == 'text' ) {
  86. if ( isRecover ) {
  87. item.setAttribute( 'value', item.getCustomData( 'fake_value' ) || '' );
  88. item.removeCustomData( 'fake_value' );
  89. } else {
  90. item.setCustomData( 'fake_value', item.getAttribute( 'value' ) );
  91. item.setAttribute( 'value', '' );
  92. }
  93. }
  94. }
  95. }
  96. // Handle dialog element validation state UI changes.
  97. function handleFieldValidated( isValid, msg ) {
  98. var input = this.getInputElement();
  99. if ( input )
  100. isValid ? input.removeAttribute( 'aria-invalid' ) : input.setAttribute( 'aria-invalid', true );
  101. if ( !isValid ) {
  102. if ( this.select )
  103. this.select();
  104. else
  105. this.focus();
  106. }
  107. msg && alert( msg ); // jshint ignore:line
  108. this.fire( 'validated', { valid: isValid, msg: msg } );
  109. }
  110. function resetField() {
  111. var input = this.getInputElement();
  112. input && input.removeAttribute( 'aria-invalid' );
  113. }
  114. var templateSource = '<div class="cke_reset_all {editorId} {editorDialogClass} {hidpi}' +
  115. '" dir="{langDir}"' +
  116. ' lang="{langCode}"' +
  117. ' role="dialog"' +
  118. ' aria-labelledby="cke_dialog_title_{id}"' +
  119. '>' +
  120. '<table class="cke_dialog ' + CKEDITOR.env.cssClass + ' cke_{langDir}"' +
  121. ' style="position:absolute" role="presentation">' +
  122. '<tr><td role="presentation">' +
  123. '<div class="cke_dialog_body" role="presentation">' +
  124. '<div id="cke_dialog_title_{id}" class="cke_dialog_title" role="presentation"></div>' +
  125. '<a id="cke_dialog_close_button_{id}" class="cke_dialog_close_button" href="javascript:void(0)" title="{closeTitle}" role="button"><span class="cke_label">X</span></a>' +
  126. '<div id="cke_dialog_tabs_{id}" class="cke_dialog_tabs" role="tablist"></div>' +
  127. '<table class="cke_dialog_contents" role="presentation">' +
  128. '<tr>' +
  129. '<td id="cke_dialog_contents_{id}" class="cke_dialog_contents_body" role="presentation"></td>' +
  130. '</tr>' +
  131. '<tr>' +
  132. '<td id="cke_dialog_footer_{id}" class="cke_dialog_footer" role="presentation"></td>' +
  133. '</tr>' +
  134. '</table>' +
  135. '</div>' +
  136. '</td></tr>' +
  137. '</table>' +
  138. '</div>';
  139. function buildDialog( editor ) {
  140. var element = CKEDITOR.dom.element.createFromHtml( CKEDITOR.addTemplate( 'dialog', templateSource ).output( {
  141. id: CKEDITOR.tools.getNextNumber(),
  142. editorId: editor.id,
  143. langDir: editor.lang.dir,
  144. langCode: editor.langCode,
  145. editorDialogClass: 'cke_editor_' + editor.name.replace( /\./g, '\\.' ) + '_dialog',
  146. closeTitle: editor.lang.common.close,
  147. hidpi: CKEDITOR.env.hidpi ? 'cke_hidpi' : ''
  148. } ) );
  149. // TODO: Change this to getById(), so it'll support custom templates.
  150. var body = element.getChild( [ 0, 0, 0, 0, 0 ] ),
  151. title = body.getChild( 0 ),
  152. close = body.getChild( 1 );
  153. // Don't allow dragging on dialog (#13184).
  154. editor.plugins.clipboard && CKEDITOR.plugins.clipboard.preventDefaultDropOnElement( body );
  155. // IFrame shim for dialog that masks activeX in IE. (#7619)
  156. if ( CKEDITOR.env.ie && !CKEDITOR.env.quirks && !CKEDITOR.env.edge ) {
  157. var src = 'javascript:void(function(){' + encodeURIComponent( 'document.open();(' + CKEDITOR.tools.fixDomain + ')();document.close();' ) + '}())', // jshint ignore:line
  158. iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
  159. ' frameBorder="0"' +
  160. ' class="cke_iframe_shim"' +
  161. ' src="' + src + '"' +
  162. ' tabIndex="-1"' +
  163. '></iframe>' );
  164. iframe.appendTo( body.getParent() );
  165. }
  166. // Make the Title and Close Button unselectable.
  167. title.unselectable();
  168. close.unselectable();
  169. return {
  170. element: element,
  171. parts: {
  172. dialog: element.getChild( 0 ),
  173. title: title,
  174. close: close,
  175. tabs: body.getChild( 2 ),
  176. contents: body.getChild( [ 3, 0, 0, 0 ] ),
  177. footer: body.getChild( [ 3, 0, 1, 0 ] )
  178. }
  179. };
  180. }
  181. /**
  182. * This is the base class for runtime dialog objects. An instance of this
  183. * class represents a single named dialog for a single editor instance.
  184. *
  185. * var dialogObj = new CKEDITOR.dialog( editor, 'smiley' );
  186. *
  187. * @class
  188. * @constructor Creates a dialog class instance.
  189. * @param {Object} editor The editor which created the dialog.
  190. * @param {String} dialogName The dialog's registered name.
  191. */
  192. CKEDITOR.dialog = function( editor, dialogName ) {
  193. // Load the dialog definition.
  194. var definition = CKEDITOR.dialog._.dialogDefinitions[ dialogName ],
  195. defaultDefinition = CKEDITOR.tools.clone( defaultDialogDefinition ),
  196. buttonsOrder = editor.config.dialog_buttonsOrder || 'OS',
  197. dir = editor.lang.dir,
  198. tabsToRemove = {},
  199. i, processed, stopPropagation;
  200. if ( ( buttonsOrder == 'OS' && CKEDITOR.env.mac ) || // The buttons in MacOS Apps are in reverse order (#4750)
  201. ( buttonsOrder == 'rtl' && dir == 'ltr' ) || ( buttonsOrder == 'ltr' && dir == 'rtl' ) )
  202. defaultDefinition.buttons.reverse();
  203. // Completes the definition with the default values.
  204. definition = CKEDITOR.tools.extend( definition( editor ), defaultDefinition );
  205. // Clone a functionally independent copy for this dialog.
  206. definition = CKEDITOR.tools.clone( definition );
  207. // Create a complex definition object, extending it with the API
  208. // functions.
  209. definition = new definitionObject( this, definition );
  210. var themeBuilt = buildDialog( editor );
  211. // Initialize some basic parameters.
  212. this._ = {
  213. editor: editor,
  214. element: themeBuilt.element,
  215. name: dialogName,
  216. contentSize: { width: 0, height: 0 },
  217. size: { width: 0, height: 0 },
  218. contents: {},
  219. buttons: {},
  220. accessKeyMap: {},
  221. // Initialize the tab and page map.
  222. tabs: {},
  223. tabIdList: [],
  224. currentTabId: null,
  225. currentTabIndex: null,
  226. pageCount: 0,
  227. lastTab: null,
  228. tabBarMode: false,
  229. // Initialize the tab order array for input widgets.
  230. focusList: [],
  231. currentFocusIndex: 0,
  232. hasFocus: false
  233. };
  234. this.parts = themeBuilt.parts;
  235. CKEDITOR.tools.setTimeout( function() {
  236. editor.fire( 'ariaWidget', this.parts.contents );
  237. }, 0, this );
  238. // Set the startup styles for the dialog, avoiding it enlarging the
  239. // page size on the dialog creation.
  240. var startStyles = {
  241. position: CKEDITOR.env.ie6Compat ? 'absolute' : 'fixed',
  242. top: 0,
  243. visibility: 'hidden'
  244. };
  245. startStyles[ dir == 'rtl' ? 'right' : 'left' ] = 0;
  246. this.parts.dialog.setStyles( startStyles );
  247. // Call the CKEDITOR.event constructor to initialize this instance.
  248. CKEDITOR.event.call( this );
  249. // Fire the "dialogDefinition" event, making it possible to customize
  250. // the dialog definition.
  251. this.definition = definition = CKEDITOR.fire( 'dialogDefinition', {
  252. name: dialogName,
  253. definition: definition
  254. }, editor ).definition;
  255. // Cache tabs that should be removed.
  256. if ( !( 'removeDialogTabs' in editor._ ) && editor.config.removeDialogTabs ) {
  257. var removeContents = editor.config.removeDialogTabs.split( ';' );
  258. for ( i = 0; i < removeContents.length; i++ ) {
  259. var parts = removeContents[ i ].split( ':' );
  260. if ( parts.length == 2 ) {
  261. var removeDialogName = parts[ 0 ];
  262. if ( !tabsToRemove[ removeDialogName ] )
  263. tabsToRemove[ removeDialogName ] = [];
  264. tabsToRemove[ removeDialogName ].push( parts[ 1 ] );
  265. }
  266. }
  267. editor._.removeDialogTabs = tabsToRemove;
  268. }
  269. // Remove tabs of this dialog.
  270. if ( editor._.removeDialogTabs && ( tabsToRemove = editor._.removeDialogTabs[ dialogName ] ) ) {
  271. for ( i = 0; i < tabsToRemove.length; i++ )
  272. definition.removeContents( tabsToRemove[ i ] );
  273. }
  274. // Initialize load, show, hide, ok and cancel events.
  275. if ( definition.onLoad )
  276. this.on( 'load', definition.onLoad );
  277. if ( definition.onShow )
  278. this.on( 'show', definition.onShow );
  279. if ( definition.onHide )
  280. this.on( 'hide', definition.onHide );
  281. if ( definition.onOk ) {
  282. this.on( 'ok', function( evt ) {
  283. // Dialog confirm might probably introduce content changes (#5415).
  284. editor.fire( 'saveSnapshot' );
  285. setTimeout( function() {
  286. editor.fire( 'saveSnapshot' );
  287. }, 0 );
  288. if ( definition.onOk.call( this, evt ) === false )
  289. evt.data.hide = false;
  290. } );
  291. }
  292. // Set default dialog state.
  293. this.state = CKEDITOR.DIALOG_STATE_IDLE;
  294. if ( definition.onCancel ) {
  295. this.on( 'cancel', function( evt ) {
  296. if ( definition.onCancel.call( this, evt ) === false )
  297. evt.data.hide = false;
  298. } );
  299. }
  300. var me = this;
  301. // Iterates over all items inside all content in the dialog, calling a
  302. // function for each of them.
  303. var iterContents = function( func ) {
  304. var contents = me._.contents,
  305. stop = false;
  306. for ( var i in contents ) {
  307. for ( var j in contents[ i ] ) {
  308. stop = func.call( this, contents[ i ][ j ] );
  309. if ( stop )
  310. return;
  311. }
  312. }
  313. };
  314. this.on( 'ok', function( evt ) {
  315. iterContents( function( item ) {
  316. if ( item.validate ) {
  317. var retval = item.validate( this ),
  318. invalid = ( typeof retval == 'string' ) || retval === false;
  319. if ( invalid ) {
  320. evt.data.hide = false;
  321. evt.stop();
  322. }
  323. handleFieldValidated.call( item, !invalid, typeof retval == 'string' ? retval : undefined );
  324. return invalid;
  325. }
  326. } );
  327. }, this, null, 0 );
  328. this.on( 'cancel', function( evt ) {
  329. iterContents( function( item ) {
  330. if ( item.isChanged() ) {
  331. if ( !editor.config.dialog_noConfirmCancel && !confirm( editor.lang.common.confirmCancel ) ) // jshint ignore:line
  332. evt.data.hide = false;
  333. return true;
  334. }
  335. } );
  336. }, this, null, 0 );
  337. this.parts.close.on( 'click', function( evt ) {
  338. if ( this.fire( 'cancel', { hide: true } ).hide !== false )
  339. this.hide();
  340. evt.data.preventDefault();
  341. }, this );
  342. // Sort focus list according to tab order definitions.
  343. function setupFocus() {
  344. var focusList = me._.focusList;
  345. focusList.sort( function( a, b ) {
  346. // Mimics browser tab order logics;
  347. if ( a.tabIndex != b.tabIndex )
  348. return b.tabIndex - a.tabIndex;
  349. // Sort is not stable in some browsers,
  350. // fall-back the comparator to 'focusIndex';
  351. else
  352. return a.focusIndex - b.focusIndex;
  353. } );
  354. var size = focusList.length;
  355. for ( var i = 0; i < size; i++ )
  356. focusList[ i ].focusIndex = i;
  357. }
  358. // Expects 1 or -1 as an offset, meaning direction of the offset change.
  359. function changeFocus( offset ) {
  360. var focusList = me._.focusList;
  361. offset = offset || 0;
  362. if ( focusList.length < 1 )
  363. return;
  364. var startIndex = me._.currentFocusIndex;
  365. if ( me._.tabBarMode && offset < 0 ) {
  366. // If we are in tab mode, we need to mimic that we started tabbing back from the first
  367. // focusList (so it will go to the last one).
  368. startIndex = 0;
  369. }
  370. // Trigger the 'blur' event of any input element before anything,
  371. // since certain UI updates may depend on it.
  372. try {
  373. focusList[ startIndex ].getInputElement().$.blur();
  374. } catch ( e ) {}
  375. var currentIndex = startIndex,
  376. hasTabs = me._.pageCount > 1;
  377. do {
  378. currentIndex = currentIndex + offset;
  379. if ( hasTabs && !me._.tabBarMode && ( currentIndex == focusList.length || currentIndex == -1 ) ) {
  380. // If the dialog was not in tab mode, then focus the first tab (#13027).
  381. me._.tabBarMode = true;
  382. me._.tabs[ me._.currentTabId ][ 0 ].focus();
  383. me._.currentFocusIndex = -1;
  384. // Early return, in order to avoid accessing focusList[ -1 ].
  385. return;
  386. }
  387. currentIndex = ( currentIndex + focusList.length ) % focusList.length;
  388. if ( currentIndex == startIndex ) {
  389. break;
  390. }
  391. } while ( offset && !focusList[ currentIndex ].isFocusable() );
  392. focusList[ currentIndex ].focus();
  393. // Select whole field content.
  394. if ( focusList[ currentIndex ].type == 'text' )
  395. focusList[ currentIndex ].select();
  396. }
  397. this.changeFocus = changeFocus;
  398. function keydownHandler( evt ) {
  399. // If I'm not the top dialog, ignore.
  400. if ( me != CKEDITOR.dialog._.currentTop )
  401. return;
  402. var keystroke = evt.data.getKeystroke(),
  403. rtl = editor.lang.dir == 'rtl',
  404. arrowKeys = [ 37, 38, 39, 40 ],
  405. button;
  406. processed = stopPropagation = 0;
  407. if ( keystroke == 9 || keystroke == CKEDITOR.SHIFT + 9 ) {
  408. var shiftPressed = ( keystroke == CKEDITOR.SHIFT + 9 );
  409. changeFocus( shiftPressed ? -1 : 1 );
  410. processed = 1;
  411. } else if ( keystroke == CKEDITOR.ALT + 121 && !me._.tabBarMode && me.getPageCount() > 1 ) {
  412. // Alt-F10 puts focus into the current tab item in the tab bar.
  413. me._.tabBarMode = true;
  414. me._.tabs[ me._.currentTabId ][ 0 ].focus();
  415. me._.currentFocusIndex = -1;
  416. processed = 1;
  417. } else if ( CKEDITOR.tools.indexOf( arrowKeys, keystroke ) != -1 && me._.tabBarMode ) {
  418. // Array with key codes that activate previous tab.
  419. var prevKeyCodes = [
  420. // Depending on the lang dir: right or left key
  421. rtl ? 39 : 37,
  422. // Top/bot arrow: actually for both cases it's the same.
  423. 38
  424. ],
  425. nextId = CKEDITOR.tools.indexOf( prevKeyCodes, keystroke ) != -1 ? getPreviousVisibleTab.call( me ) : getNextVisibleTab.call( me );
  426. me.selectPage( nextId );
  427. me._.tabs[ nextId ][ 0 ].focus();
  428. processed = 1;
  429. } else if ( ( keystroke == 13 || keystroke == 32 ) && me._.tabBarMode ) {
  430. this.selectPage( this._.currentTabId );
  431. this._.tabBarMode = false;
  432. this._.currentFocusIndex = -1;
  433. changeFocus( 1 );
  434. processed = 1;
  435. }
  436. // If user presses enter key in a text box, it implies clicking OK for the dialog.
  437. else if ( keystroke == 13 /*ENTER*/ ) {
  438. // Don't do that for a target that handles ENTER.
  439. var target = evt.data.getTarget();
  440. if ( !target.is( 'a', 'button', 'select', 'textarea' ) && ( !target.is( 'input' ) || target.$.type != 'button' ) ) {
  441. button = this.getButton( 'ok' );
  442. button && CKEDITOR.tools.setTimeout( button.click, 0, button );
  443. processed = 1;
  444. }
  445. stopPropagation = 1; // Always block the propagation (#4269)
  446. } else if ( keystroke == 27 /*ESC*/ ) {
  447. button = this.getButton( 'cancel' );
  448. // If there's a Cancel button, click it, else just fire the cancel event and hide the dialog.
  449. if ( button )
  450. CKEDITOR.tools.setTimeout( button.click, 0, button );
  451. else {
  452. if ( this.fire( 'cancel', { hide: true } ).hide !== false )
  453. this.hide();
  454. }
  455. stopPropagation = 1; // Always block the propagation (#4269)
  456. } else {
  457. return;
  458. }
  459. keypressHandler( evt );
  460. }
  461. function keypressHandler( evt ) {
  462. if ( processed )
  463. evt.data.preventDefault( 1 );
  464. else if ( stopPropagation )
  465. evt.data.stopPropagation();
  466. }
  467. var dialogElement = this._.element;
  468. editor.focusManager.add( dialogElement, 1 );
  469. // Add the dialog keyboard handlers.
  470. this.on( 'show', function() {
  471. dialogElement.on( 'keydown', keydownHandler, this );
  472. // Some browsers instead, don't cancel key events in the keydown, but in the
  473. // keypress. So we must do a longer trip in those cases. (#4531,#8985)
  474. if ( CKEDITOR.env.gecko )
  475. dialogElement.on( 'keypress', keypressHandler, this );
  476. } );
  477. this.on( 'hide', function() {
  478. dialogElement.removeListener( 'keydown', keydownHandler );
  479. if ( CKEDITOR.env.gecko )
  480. dialogElement.removeListener( 'keypress', keypressHandler );
  481. // Reset fields state when closing dialog.
  482. iterContents( function( item ) {
  483. resetField.apply( item );
  484. } );
  485. } );
  486. this.on( 'iframeAdded', function( evt ) {
  487. var doc = new CKEDITOR.dom.document( evt.data.iframe.$.contentWindow.document );
  488. doc.on( 'keydown', keydownHandler, this, null, 0 );
  489. } );
  490. // Auto-focus logic in dialog.
  491. this.on( 'show', function() {
  492. // Setup tabIndex on showing the dialog instead of on loading
  493. // to allow dynamic tab order happen in dialog definition.
  494. setupFocus();
  495. var hasTabs = me._.pageCount > 1;
  496. if ( editor.config.dialog_startupFocusTab && hasTabs ) {
  497. me._.tabBarMode = true;
  498. me._.tabs[ me._.currentTabId ][ 0 ].focus();
  499. me._.currentFocusIndex = -1;
  500. } else if ( !this._.hasFocus ) {
  501. // http://dev.ckeditor.com/ticket/13114#comment:4.
  502. this._.currentFocusIndex = hasTabs ? -1 : this._.focusList.length - 1;
  503. // Decide where to put the initial focus.
  504. if ( definition.onFocus ) {
  505. var initialFocus = definition.onFocus.call( this );
  506. // Focus the field that the user specified.
  507. initialFocus && initialFocus.focus();
  508. }
  509. // Focus the first field in layout order.
  510. else {
  511. changeFocus( 1 );
  512. }
  513. }
  514. }, this, null, 0xffffffff );
  515. // IE6 BUG: Text fields and text areas are only half-rendered the first time the dialog appears in IE6 (#2661).
  516. // This is still needed after [2708] and [2709] because text fields in hidden TR tags are still broken.
  517. if ( CKEDITOR.env.ie6Compat ) {
  518. this.on( 'load', function() {
  519. var outer = this.getElement(),
  520. inner = outer.getFirst();
  521. inner.remove();
  522. inner.appendTo( outer );
  523. }, this );
  524. }
  525. initDragAndDrop( this );
  526. initResizeHandles( this );
  527. // Insert the title.
  528. ( new CKEDITOR.dom.text( definition.title, CKEDITOR.document ) ).appendTo( this.parts.title );
  529. // Insert the tabs and contents.
  530. for ( i = 0; i < definition.contents.length; i++ ) {
  531. var page = definition.contents[ i ];
  532. page && this.addPage( page );
  533. }
  534. this.parts.tabs.on( 'click', function( evt ) {
  535. var target = evt.data.getTarget();
  536. // If we aren't inside a tab, bail out.
  537. if ( target.hasClass( 'cke_dialog_tab' ) ) {
  538. // Get the ID of the tab, without the 'cke_' prefix and the unique number suffix.
  539. var id = target.$.id;
  540. this.selectPage( id.substring( 4, id.lastIndexOf( '_' ) ) );
  541. if ( this._.tabBarMode ) {
  542. this._.tabBarMode = false;
  543. this._.currentFocusIndex = -1;
  544. changeFocus( 1 );
  545. }
  546. evt.data.preventDefault();
  547. }
  548. }, this );
  549. // Insert buttons.
  550. var buttonsHtml = [],
  551. buttons = CKEDITOR.dialog._.uiElementBuilders.hbox.build( this, {
  552. type: 'hbox',
  553. className: 'cke_dialog_footer_buttons',
  554. widths: [],
  555. children: definition.buttons
  556. }, buttonsHtml ).getChild();
  557. this.parts.footer.setHtml( buttonsHtml.join( '' ) );
  558. for ( i = 0; i < buttons.length; i++ )
  559. this._.buttons[ buttons[ i ].id ] = buttons[ i ];
  560. /**
  561. * Current state of the dialog. Use the {@link #setState} method to update it.
  562. * See the {@link #event-state} event to know more.
  563. *
  564. * @readonly
  565. * @property {Number} [state=CKEDITOR.DIALOG_STATE_IDLE]
  566. */
  567. };
  568. // Focusable interface. Use it via dialog.addFocusable.
  569. function Focusable( dialog, element, index ) {
  570. this.element = element;
  571. this.focusIndex = index;
  572. // TODO: support tabIndex for focusables.
  573. this.tabIndex = 0;
  574. this.isFocusable = function() {
  575. return !element.getAttribute( 'disabled' ) && element.isVisible();
  576. };
  577. this.focus = function() {
  578. dialog._.currentFocusIndex = this.focusIndex;
  579. this.element.focus();
  580. };
  581. // Bind events
  582. element.on( 'keydown', function( e ) {
  583. if ( e.data.getKeystroke() in { 32: 1, 13: 1 } )
  584. this.fire( 'click' );
  585. } );
  586. element.on( 'focus', function() {
  587. this.fire( 'mouseover' );
  588. } );
  589. element.on( 'blur', function() {
  590. this.fire( 'mouseout' );
  591. } );
  592. }
  593. // Re-layout the dialog on window resize.
  594. function resizeWithWindow( dialog ) {
  595. var win = CKEDITOR.document.getWindow();
  596. function resizeHandler() {
  597. dialog.layout();
  598. }
  599. win.on( 'resize', resizeHandler );
  600. dialog.on( 'hide', function() {
  601. win.removeListener( 'resize', resizeHandler );
  602. } );
  603. }
  604. CKEDITOR.dialog.prototype = {
  605. destroy: function() {
  606. this.hide();
  607. this._.element.remove();
  608. },
  609. /**
  610. * Resizes the dialog.
  611. *
  612. * dialogObj.resize( 800, 640 );
  613. *
  614. * @method
  615. * @param {Number} width The width of the dialog in pixels.
  616. * @param {Number} height The height of the dialog in pixels.
  617. */
  618. resize: ( function() {
  619. return function( width, height ) {
  620. if ( this._.contentSize && this._.contentSize.width == width && this._.contentSize.height == height )
  621. return;
  622. CKEDITOR.dialog.fire( 'resize', {
  623. dialog: this,
  624. width: width,
  625. height: height
  626. }, this._.editor );
  627. this.fire( 'resize', {
  628. width: width,
  629. height: height
  630. }, this._.editor );
  631. var contents = this.parts.contents;
  632. contents.setStyles( {
  633. width: width + 'px',
  634. height: height + 'px'
  635. } );
  636. // Update dialog position when dimension get changed in RTL.
  637. if ( this._.editor.lang.dir == 'rtl' && this._.position )
  638. this._.position.x = CKEDITOR.document.getWindow().getViewPaneSize().width - this._.contentSize.width - parseInt( this._.element.getFirst().getStyle( 'right' ), 10 );
  639. this._.contentSize = { width: width, height: height };
  640. };
  641. } )(),
  642. /**
  643. * Gets the current size of the dialog in pixels.
  644. *
  645. * var width = dialogObj.getSize().width;
  646. *
  647. * @returns {Object}
  648. * @returns {Number} return.width
  649. * @returns {Number} return.height
  650. */
  651. getSize: function() {
  652. var element = this._.element.getFirst();
  653. return { width: element.$.offsetWidth || 0, height: element.$.offsetHeight || 0 };
  654. },
  655. /**
  656. * Moves the dialog to an `(x, y)` coordinate relative to the window.
  657. *
  658. * dialogObj.move( 10, 40 );
  659. *
  660. * @method
  661. * @param {Number} x The target x-coordinate.
  662. * @param {Number} y The target y-coordinate.
  663. * @param {Boolean} save Flag indicate whether the dialog position should be remembered on next open up.
  664. */
  665. move: function( x, y, save ) {
  666. // The dialog may be fixed positioned or absolute positioned. Ask the
  667. // browser what is the current situation first.
  668. var element = this._.element.getFirst(), rtl = this._.editor.lang.dir == 'rtl';
  669. var isFixed = element.getComputedStyle( 'position' ) == 'fixed';
  670. // (#8888) In some cases of a very small viewport, dialog is incorrectly
  671. // positioned in IE7. It also happens that it remains sticky and user cannot
  672. // scroll down/up to reveal dialog's content below/above the viewport; this is
  673. // cumbersome.
  674. // The only way to fix this is to move mouse out of the browser and
  675. // go back to see that dialog position is automagically fixed. No events,
  676. // no style change - pure magic. This is a IE7 rendering issue, which can be
  677. // fixed with dummy style redraw on each move.
  678. if ( CKEDITOR.env.ie )
  679. element.setStyle( 'zoom', '100%' );
  680. if ( isFixed && this._.position && this._.position.x == x && this._.position.y == y )
  681. return;
  682. // Save the current position.
  683. this._.position = { x: x, y: y };
  684. // If not fixed positioned, add scroll position to the coordinates.
  685. if ( !isFixed ) {
  686. var scrollPosition = CKEDITOR.document.getWindow().getScrollPosition();
  687. x += scrollPosition.x;
  688. y += scrollPosition.y;
  689. }
  690. // Translate coordinate for RTL.
  691. if ( rtl ) {
  692. var dialogSize = this.getSize(), viewPaneSize = CKEDITOR.document.getWindow().getViewPaneSize();
  693. x = viewPaneSize.width - dialogSize.width - x;
  694. }
  695. var styles = { 'top': ( y > 0 ? y : 0 ) + 'px' };
  696. styles[ rtl ? 'right' : 'left' ] = ( x > 0 ? x : 0 ) + 'px';
  697. element.setStyles( styles );
  698. save && ( this._.moved = 1 );
  699. },
  700. /**
  701. * Gets the dialog's position in the window.
  702. *
  703. * var dialogX = dialogObj.getPosition().x;
  704. *
  705. * @returns {Object}
  706. * @returns {Number} return.x
  707. * @returns {Number} return.y
  708. */
  709. getPosition: function() {
  710. return CKEDITOR.tools.extend( {}, this._.position );
  711. },
  712. /**
  713. * Shows the dialog box.
  714. *
  715. * dialogObj.show();
  716. */
  717. show: function() {
  718. // Insert the dialog's element to the root document.
  719. var element = this._.element;
  720. var definition = this.definition;
  721. if ( !( element.getParent() && element.getParent().equals( CKEDITOR.document.getBody() ) ) )
  722. element.appendTo( CKEDITOR.document.getBody() );
  723. else
  724. element.setStyle( 'display', 'block' );
  725. // First, set the dialog to an appropriate size.
  726. this.resize(
  727. this._.contentSize && this._.contentSize.width || definition.width || definition.minWidth,
  728. this._.contentSize && this._.contentSize.height || definition.height || definition.minHeight
  729. );
  730. // Reset all inputs back to their default value.
  731. this.reset();
  732. // Select the first tab by default.
  733. this.selectPage( this.definition.contents[ 0 ].id );
  734. // Set z-index.
  735. if ( CKEDITOR.dialog._.currentZIndex === null )
  736. CKEDITOR.dialog._.currentZIndex = this._.editor.config.baseFloatZIndex;
  737. this._.element.getFirst().setStyle( 'z-index', CKEDITOR.dialog._.currentZIndex += 10 );
  738. // Maintain the dialog ordering and dialog cover.
  739. if ( CKEDITOR.dialog._.currentTop === null ) {
  740. CKEDITOR.dialog._.currentTop = this;
  741. this._.parentDialog = null;
  742. showCover( this._.editor );
  743. } else {
  744. this._.parentDialog = CKEDITOR.dialog._.currentTop;
  745. var parentElement = this._.parentDialog.getElement().getFirst();
  746. parentElement.$.style.zIndex -= Math.floor( this._.editor.config.baseFloatZIndex / 2 );
  747. CKEDITOR.dialog._.currentTop = this;
  748. }
  749. element.on( 'keydown', accessKeyDownHandler );
  750. element.on( 'keyup', accessKeyUpHandler );
  751. // Reset the hasFocus state.
  752. this._.hasFocus = false;
  753. for ( var i in definition.contents ) {
  754. if ( !definition.contents[ i ] )
  755. continue;
  756. var content = definition.contents[ i ],
  757. tab = this._.tabs[ content.id ],
  758. requiredContent = content.requiredContent,
  759. enableElements = 0;
  760. if ( !tab )
  761. continue;
  762. for ( var j in this._.contents[ content.id ] ) {
  763. var elem = this._.contents[ content.id ][ j ];
  764. if ( elem.type == 'hbox' || elem.type == 'vbox' || !elem.getInputElement() )
  765. continue;
  766. if ( elem.requiredContent && !this._.editor.activeFilter.check( elem.requiredContent ) )
  767. elem.disable();
  768. else {
  769. elem.enable();
  770. enableElements++;
  771. }
  772. }
  773. if ( !enableElements || ( requiredContent && !this._.editor.activeFilter.check( requiredContent ) ) )
  774. tab[ 0 ].addClass( 'cke_dialog_tab_disabled' );
  775. else
  776. tab[ 0 ].removeClass( 'cke_dialog_tab_disabled' );
  777. }
  778. CKEDITOR.tools.setTimeout( function() {
  779. this.layout();
  780. resizeWithWindow( this );
  781. this.parts.dialog.setStyle( 'visibility', '' );
  782. // Execute onLoad for the first show.
  783. this.fireOnce( 'load', {} );
  784. CKEDITOR.ui.fire( 'ready', this );
  785. this.fire( 'show', {} );
  786. this._.editor.fire( 'dialogShow', this );
  787. if ( !this._.parentDialog )
  788. this._.editor.focusManager.lock();
  789. // Save the initial values of the dialog.
  790. this.foreach( function( contentObj ) {
  791. contentObj.setInitValue && contentObj.setInitValue();
  792. } );
  793. }, 100, this );
  794. },
  795. /**
  796. * Rearrange the dialog to its previous position or the middle of the window.
  797. *
  798. * @since 3.5
  799. */
  800. layout: function() {
  801. var el = this.parts.dialog;
  802. var dialogSize = this.getSize();
  803. var win = CKEDITOR.document.getWindow(),
  804. viewSize = win.getViewPaneSize();
  805. var posX = ( viewSize.width - dialogSize.width ) / 2,
  806. posY = ( viewSize.height - dialogSize.height ) / 2;
  807. // Switch to absolute position when viewport is smaller than dialog size.
  808. if ( !CKEDITOR.env.ie6Compat ) {
  809. if ( dialogSize.height + ( posY > 0 ? posY : 0 ) > viewSize.height || dialogSize.width + ( posX > 0 ? posX : 0 ) > viewSize.width ) {
  810. el.setStyle( 'position', 'absolute' );
  811. } else {
  812. el.setStyle( 'position', 'fixed' );
  813. }
  814. }
  815. this.move( this._.moved ? this._.position.x : posX, this._.moved ? this._.position.y : posY );
  816. },
  817. /**
  818. * Executes a function for each UI element.
  819. *
  820. * @param {Function} fn Function to execute for each UI element.
  821. * @returns {CKEDITOR.dialog} The current dialog object.
  822. */
  823. foreach: function( fn ) {
  824. for ( var i in this._.contents ) {
  825. for ( var j in this._.contents[ i ] ) {
  826. fn.call( this, this._.contents[i][j] );
  827. }
  828. }
  829. return this;
  830. },
  831. /**
  832. * Resets all input values in the dialog.
  833. *
  834. * dialogObj.reset();
  835. *
  836. * @method
  837. * @chainable
  838. */
  839. reset: ( function() {
  840. var fn = function( widget ) {
  841. if ( widget.reset )
  842. widget.reset( 1 );
  843. };
  844. return function() {
  845. this.foreach( fn );
  846. return this;
  847. };
  848. } )(),
  849. /**
  850. * Calls the {@link CKEDITOR.dialog.definition.uiElement#setup} method of each
  851. * of the UI elements, with the arguments passed through it.
  852. * It is usually being called when the dialog is opened, to put the initial value inside the field.
  853. *
  854. * dialogObj.setupContent();
  855. *
  856. * var timestamp = ( new Date() ).valueOf();
  857. * dialogObj.setupContent( timestamp );
  858. */
  859. setupContent: function() {
  860. var args = arguments;
  861. this.foreach( function( widget ) {
  862. if ( widget.setup )
  863. widget.setup.apply( widget, args );
  864. } );
  865. },
  866. /**
  867. * Calls the {@link CKEDITOR.dialog.definition.uiElement#commit} method of each
  868. * of the UI elements, with the arguments passed through it.
  869. * It is usually being called when the user confirms the dialog, to process the values.
  870. *
  871. * dialogObj.commitContent();
  872. *
  873. * var timestamp = ( new Date() ).valueOf();
  874. * dialogObj.commitContent( timestamp );
  875. */
  876. commitContent: function() {
  877. var args = arguments;
  878. this.foreach( function( widget ) {
  879. // Make sure IE triggers "change" event on last focused input before closing the dialog. (#7915)
  880. if ( CKEDITOR.env.ie && this._.currentFocusIndex == widget.focusIndex )
  881. widget.getInputElement().$.blur();
  882. if ( widget.commit )
  883. widget.commit.apply( widget, args );
  884. } );
  885. },
  886. /**
  887. * Hides the dialog box.
  888. *
  889. * dialogObj.hide();
  890. */
  891. hide: function() {
  892. if ( !this.parts.dialog.isVisible() )
  893. return;
  894. this.fire( 'hide', {} );
  895. this._.editor.fire( 'dialogHide', this );
  896. // Reset the tab page.
  897. this.selectPage( this._.tabIdList[ 0 ] );
  898. var element = this._.element;
  899. element.setStyle( 'display', 'none' );
  900. this.parts.dialog.setStyle( 'visibility', 'hidden' );
  901. // Unregister all access keys associated with this dialog.
  902. unregisterAccessKey( this );
  903. // Close any child(top) dialogs first.
  904. while ( CKEDITOR.dialog._.currentTop != this )
  905. CKEDITOR.dialog._.currentTop.hide();
  906. // Maintain dialog ordering and remove cover if needed.
  907. if ( !this._.parentDialog )
  908. hideCover( this._.editor );
  909. else {
  910. var parentElement = this._.parentDialog.getElement().getFirst();
  911. parentElement.setStyle( 'z-index', parseInt( parentElement.$.style.zIndex, 10 ) + Math.floor( this._.editor.config.baseFloatZIndex / 2 ) );
  912. }
  913. CKEDITOR.dialog._.currentTop = this._.parentDialog;
  914. // Deduct or clear the z-index.
  915. if ( !this._.parentDialog ) {
  916. CKEDITOR.dialog._.currentZIndex = null;
  917. // Remove access key handlers.
  918. element.removeListener( 'keydown', accessKeyDownHandler );
  919. element.removeListener( 'keyup', accessKeyUpHandler );
  920. var editor = this._.editor;
  921. editor.focus();
  922. // Give a while before unlock, waiting for focus to return to the editable. (#172)
  923. setTimeout( function() {
  924. editor.focusManager.unlock();
  925. // Fixed iOS focus issue (#12381).
  926. // Keep in mind that editor.focus() does not work in this case.
  927. if ( CKEDITOR.env.iOS ) {
  928. editor.window.focus();
  929. }
  930. }, 0 );
  931. } else {
  932. CKEDITOR.dialog._.currentZIndex -= 10;
  933. }
  934. delete this._.parentDialog;
  935. // Reset the initial values of the dialog.
  936. this.foreach( function( contentObj ) {
  937. contentObj.resetInitValue && contentObj.resetInitValue();
  938. } );
  939. // Reset dialog state back to IDLE, if busy (#13213).
  940. this.setState( CKEDITOR.DIALOG_STATE_IDLE );
  941. },
  942. /**
  943. * Adds a tabbed page into the dialog.
  944. *
  945. * @param {Object} contents Content definition.
  946. */
  947. addPage: function( contents ) {
  948. if ( contents.requiredContent && !this._.editor.filter.check( contents.requiredContent ) )
  949. return;
  950. var pageHtml = [],
  951. titleHtml = contents.label ? ' title="' + CKEDITOR.tools.htmlEncode( contents.label ) + '"' : '',
  952. vbox = CKEDITOR.dialog._.uiElementBuilders.vbox.build( this, {
  953. type: 'vbox',
  954. className: 'cke_dialog_page_contents',
  955. children: contents.elements,
  956. expand: !!contents.expand,
  957. padding: contents.padding,
  958. style: contents.style || 'width: 100%;'
  959. }, pageHtml );
  960. var contentMap = this._.contents[ contents.id ] = {},
  961. cursor,
  962. children = vbox.getChild(),
  963. enabledFields = 0;
  964. while ( ( cursor = children.shift() ) ) {
  965. // Count all allowed fields.
  966. if ( !cursor.notAllowed && cursor.type != 'hbox' && cursor.type != 'vbox' )
  967. enabledFields++;
  968. contentMap[ cursor.id ] = cursor;
  969. if ( typeof cursor.getChild == 'function' )
  970. children.push.apply( children, cursor.getChild() );
  971. }
  972. // If all fields are disabled (because they are not allowed) hide this tab.
  973. if ( !enabledFields )
  974. contents.hidden = true;
  975. // Create the HTML for the tab and the content block.
  976. var page = CKEDITOR.dom.element.createFromHtml( pageHtml.join( '' ) );
  977. page.setAttribute( 'role', 'tabpanel' );
  978. var env = CKEDITOR.env;
  979. var tabId = 'cke_' + contents.id + '_' + CKEDITOR.tools.getNextNumber(),
  980. tab = CKEDITOR.dom.element.createFromHtml( [
  981. '<a class="cke_dialog_tab"',
  982. ( this._.pageCount > 0 ? ' cke_last' : 'cke_first' ),
  983. titleHtml,
  984. ( !!contents.hidden ? ' style="display:none"' : '' ),
  985. ' id="', tabId, '"',
  986. env.gecko && !env.hc ? '' : ' href="javascript:void(0)"',
  987. ' tabIndex="-1"',
  988. ' hidefocus="true"',
  989. ' role="tab">',
  990. contents.label,
  991. '</a>'
  992. ].join( '' ) );
  993. page.setAttribute( 'aria-labelledby', tabId );
  994. // Take records for the tabs and elements created.
  995. this._.tabs[ contents.id ] = [ tab, page ];
  996. this._.tabIdList.push( contents.id );
  997. !contents.hidden && this._.pageCount++;
  998. this._.lastTab = tab;
  999. this.updateStyle();
  1000. // Attach the DOM nodes.
  1001. page.setAttribute( 'name', contents.id );
  1002. page.appendTo( this.parts.contents );
  1003. tab.unselectable();
  1004. this.parts.tabs.append( tab );
  1005. // Add access key handlers if access key is defined.
  1006. if ( contents.accessKey ) {
  1007. registerAccessKey( this, this, 'CTRL+' + contents.accessKey, tabAccessKeyDown, tabAccessKeyUp );
  1008. this._.accessKeyMap[ 'CTRL+' + contents.accessKey ] = contents.id;
  1009. }
  1010. },
  1011. /**
  1012. * Activates a tab page in the dialog by its id.
  1013. *
  1014. * dialogObj.selectPage( 'tab_1' );
  1015. *
  1016. * @param {String} id The id of the dialog tab to be activated.
  1017. */
  1018. selectPage: function( id ) {
  1019. if ( this._.currentTabId == id )
  1020. return;
  1021. if ( this._.tabs[ id ][ 0 ].hasClass( 'cke_dialog_tab_disabled' ) )
  1022. return;
  1023. // If event was canceled - do nothing.
  1024. if ( this.fire( 'selectPage', { page: id, currentPage: this._.currentTabId } ) === false )
  1025. return;
  1026. // Hide the non-selected tabs and pages.
  1027. for ( var i in this._.tabs ) {
  1028. var tab = this._.tabs[ i ][ 0 ],
  1029. page = this._.tabs[ i ][ 1 ];
  1030. if ( i != id ) {
  1031. tab.removeClass( 'cke_dialog_tab_selected' );
  1032. page.hide();
  1033. }
  1034. page.setAttribute( 'aria-hidden', i != id );
  1035. }
  1036. var selected = this._.tabs[ id ];
  1037. selected[ 0 ].addClass( 'cke_dialog_tab_selected' );
  1038. // [IE] an invisible input[type='text'] will enlarge it's width
  1039. // if it's value is long when it shows, so we clear it's value
  1040. // before it shows and then recover it (#5649)
  1041. if ( CKEDITOR.env.ie6Compat || CKEDITOR.env.ie7Compat ) {
  1042. clearOrRecoverTextInputValue( selected[ 1 ] );
  1043. selected[ 1 ].show();
  1044. setTimeout( function() {
  1045. clearOrRecoverTextInputValue( selected[ 1 ], 1 );
  1046. }, 0 );
  1047. } else {
  1048. selected[ 1 ].show();
  1049. }
  1050. this._.currentTabId = id;
  1051. this._.currentTabIndex = CKEDITOR.tools.indexOf( this._.tabIdList, id );
  1052. },
  1053. /**
  1054. * Dialog state-specific style updates.
  1055. */
  1056. updateStyle: function() {
  1057. // If only a single page shown, a different style is used in the central pane.
  1058. this.parts.dialog[ ( this._.pageCount === 1 ? 'add' : 'remove' ) + 'Class' ]( 'cke_single_page' );
  1059. },
  1060. /**
  1061. * Hides a page's tab away from the dialog.
  1062. *
  1063. * dialog.hidePage( 'tab_3' );
  1064. *
  1065. * @param {String} id The page's Id.
  1066. */
  1067. hidePage: function( id ) {
  1068. var tab = this._.tabs[ id ] && this._.tabs[ id ][ 0 ];
  1069. if ( !tab || this._.pageCount == 1 || !tab.isVisible() )
  1070. return;
  1071. // Switch to other tab first when we're hiding the active tab.
  1072. else if ( id == this._.currentTabId )
  1073. this.selectPage( getPreviousVisibleTab.call( this ) );
  1074. tab.hide();
  1075. this._.pageCount--;
  1076. this.updateStyle();
  1077. },
  1078. /**
  1079. * Unhides a page's tab.
  1080. *
  1081. * dialog.showPage( 'tab_2' );
  1082. *
  1083. * @param {String} id The page's Id.
  1084. */
  1085. showPage: function( id ) {
  1086. var tab = this._.tabs[ id ] && this._.tabs[ id ][ 0 ];
  1087. if ( !tab )
  1088. return;
  1089. tab.show();
  1090. this._.pageCount++;
  1091. this.updateStyle();
  1092. },
  1093. /**
  1094. * Gets the root DOM element of the dialog.
  1095. *
  1096. * var dialogElement = dialogObj.getElement().getFirst();
  1097. * dialogElement.setStyle( 'padding', '5px' );
  1098. *
  1099. * @returns {CKEDITOR.dom.element} The `<span>` element containing this dialog.
  1100. */
  1101. getElement: function() {
  1102. return this._.element;
  1103. },
  1104. /**
  1105. * Gets the name of the dialog.
  1106. *
  1107. * var dialogName = dialogObj.getName();
  1108. *
  1109. * @returns {String} The name of this dialog.
  1110. */
  1111. getName: function() {
  1112. return this._.name;
  1113. },
  1114. /**
  1115. * Gets a dialog UI element object from a dialog page.
  1116. *
  1117. * dialogObj.getContentElement( 'tabId', 'elementId' ).setValue( 'Example' );
  1118. *
  1119. * @param {String} pageId id of dialog page.
  1120. * @param {String} elementId id of UI element.
  1121. * @returns {CKEDITOR.ui.dialog.uiElement} The dialog UI element.
  1122. */
  1123. getContentElement: function( pageId, elementId ) {
  1124. var page = this._.contents[ pageId ];
  1125. return page && page[ elementId ];
  1126. },
  1127. /**
  1128. * Gets the value of a dialog UI element.
  1129. *
  1130. * alert( dialogObj.getValueOf( 'tabId', 'elementId' ) );
  1131. *
  1132. * @param {String} pageId id of dialog page.
  1133. * @param {String} elementId id of UI element.
  1134. * @returns {Object} The value of the UI element.
  1135. */
  1136. getValueOf: function( pageId, elementId ) {
  1137. return this.getContentElement( pageId, elementId ).getValue();
  1138. },
  1139. /**
  1140. * Sets the value of a dialog UI element.
  1141. *
  1142. * dialogObj.setValueOf( 'tabId', 'elementId', 'Example' );
  1143. *
  1144. * @param {String} pageId id of the dialog page.
  1145. * @param {String} elementId id of the UI element.
  1146. * @param {Object} value The new value of the UI element.
  1147. */
  1148. setValueOf: function( pageId, elementId, value ) {
  1149. return this.getContentElement( pageId, elementId ).setValue( value );
  1150. },
  1151. /**
  1152. * Gets the UI element of a button in the dialog's button row.
  1153. *
  1154. * @returns {CKEDITOR.ui.dialog.button} The button object.
  1155. *
  1156. * @param {String} id The id of the button.
  1157. */
  1158. getButton: function( id ) {
  1159. return this._.buttons[ id ];
  1160. },
  1161. /**
  1162. * Simulates a click to a dialog button in the dialog's button row.
  1163. *
  1164. * @returns The return value of the dialog's `click` event.
  1165. *
  1166. * @param {String} id The id of the button.
  1167. */
  1168. click: function( id ) {
  1169. return this._.buttons[ id ].click();
  1170. },
  1171. /**
  1172. * Disables a dialog button.
  1173. *
  1174. * @param {String} id The id of the button.
  1175. */
  1176. disableButton: function( id ) {
  1177. return this._.buttons[ id ].disable();
  1178. },
  1179. /**
  1180. * Enables a dialog button.
  1181. *
  1182. * @param {String} id The id of the button.
  1183. */
  1184. enableButton: function( id ) {
  1185. return this._.buttons[ id ].enable();
  1186. },
  1187. /**
  1188. * Gets the number of pages in the dialog.
  1189. *
  1190. * @returns {Number} Page count.
  1191. */
  1192. getPageCount: function() {
  1193. return this._.pageCount;
  1194. },
  1195. /**
  1196. * Gets the editor instance which opened this dialog.
  1197. *
  1198. * @returns {CKEDITOR.editor} Parent editor instances.
  1199. */
  1200. getParentEditor: function() {
  1201. return this._.editor;
  1202. },
  1203. /**
  1204. * Gets the element that was selected when opening the dialog, if any.
  1205. *
  1206. * @returns {CKEDITOR.dom.element} The element that was selected, or `null`.
  1207. */
  1208. getSelectedElement: function() {
  1209. return this.getParentEditor().getSelection().getSelectedElement();
  1210. },
  1211. /**
  1212. * Adds element to dialog's focusable list.
  1213. *
  1214. * @param {CKEDITOR.dom.element} element
  1215. * @param {Number} [index]
  1216. */
  1217. addFocusable: function( element, index ) {
  1218. if ( typeof index == 'undefined' ) {
  1219. index = this._.focusList.length;
  1220. this._.focusList.push( new Focusable( this, element, index ) );
  1221. } else {
  1222. this._.focusList.splice( index, 0, new Focusable( this, element, index ) );
  1223. for ( var i = index + 1; i < this._.focusList.length; i++ )
  1224. this._.focusList[ i ].focusIndex++;
  1225. }
  1226. },
  1227. /**
  1228. * Sets the dialog {@link #property-state}.
  1229. *
  1230. * @since 4.5
  1231. * @param {Number} state Either {@link CKEDITOR#DIALOG_STATE_IDLE} or {@link CKEDITOR#DIALOG_STATE_BUSY}.
  1232. */
  1233. setState: function( state ) {
  1234. var oldState = this.state;
  1235. if ( oldState == state ) {
  1236. return;
  1237. }
  1238. this.state = state;
  1239. if ( state == CKEDITOR.DIALOG_STATE_BUSY ) {
  1240. // Insert the spinner on demand.
  1241. if ( !this.parts.spinner ) {
  1242. var dir = this.getParentEditor().lang.dir,
  1243. spinnerDef = {
  1244. attributes: {
  1245. 'class': 'cke_dialog_spinner'
  1246. },
  1247. styles: {
  1248. 'float': dir == 'rtl' ? 'right' : 'left'
  1249. }
  1250. };
  1251. spinnerDef.styles[ 'margin-' + ( dir == 'rtl' ? 'left' : 'right' ) ] = '8px';
  1252. this.parts.spinner = CKEDITOR.document.createElement( 'div', spinnerDef );
  1253. this.parts.spinner.setHtml( '&#8987;' );
  1254. this.parts.spinner.appendTo( this.parts.title, 1 );
  1255. }
  1256. // Finally, show the spinner.
  1257. this.parts.spinner.show();
  1258. this.getButton( 'ok' ).disable();
  1259. } else if ( state == CKEDITOR.DIALOG_STATE_IDLE ) {
  1260. // Hide the spinner. But don't do anything if there is no spinner yet.
  1261. this.parts.spinner && this.parts.spinner.hide();
  1262. this.getButton( 'ok' ).enable();
  1263. }
  1264. this.fire( 'state', state );
  1265. }
  1266. };
  1267. CKEDITOR.tools.extend( CKEDITOR.dialog, {
  1268. /**
  1269. * Registers a dialog.
  1270. *
  1271. * // Full sample plugin, which does not only register a dialog window but also adds an item to the context menu.
  1272. * // To open the dialog window, choose "Open dialog" in the context menu.
  1273. * CKEDITOR.plugins.add( 'myplugin', {
  1274. * init: function( editor ) {
  1275. * editor.addCommand( 'mydialog',new CKEDITOR.dialogCommand( 'mydialog' ) );
  1276. *
  1277. * if ( editor.contextMenu ) {
  1278. * editor.addMenuGroup( 'mygroup', 10 );
  1279. * editor.addMenuItem( 'My Dialog', {
  1280. * label: 'Open dialog',
  1281. * command: 'mydialog',
  1282. * group: 'mygroup'
  1283. * } );
  1284. * editor.contextMenu.addListener( function( element ) {
  1285. * return { 'My Dialog': CKEDITOR.TRISTATE_OFF };
  1286. * } );
  1287. * }
  1288. *
  1289. * CKEDITOR.dialog.add( 'mydialog', function( api ) {
  1290. * // CKEDITOR.dialog.definition
  1291. * var dialogDefinition = {
  1292. * title: 'Sample dialog',
  1293. * minWidth: 390,
  1294. * minHeight: 130,
  1295. * contents: [
  1296. * {
  1297. * id: 'tab1',
  1298. * label: 'Label',
  1299. * title: 'Title',
  1300. * expand: true,
  1301. * padding: 0,
  1302. * elements: [
  1303. * {
  1304. * type: 'html',
  1305. * html: '<p>This is some sample HTML content.</p>'
  1306. * },
  1307. * {
  1308. * type: 'textarea',
  1309. * id: 'textareaId',
  1310. * rows: 4,
  1311. * cols: 40
  1312. * }
  1313. * ]
  1314. * }
  1315. * ],
  1316. * buttons: [ CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton ],
  1317. * onOk: function() {
  1318. * // "this" is now a CKEDITOR.dialog object.
  1319. * // Accessing dialog elements:
  1320. * var textareaObj = this.getContentElement( 'tab1', 'textareaId' );
  1321. * alert( "You have entered: " + textareaObj.getValue() );
  1322. * }
  1323. * };
  1324. *
  1325. * return dialogDefinition;
  1326. * } );
  1327. * }
  1328. * } );
  1329. *
  1330. * CKEDITOR.replace( 'editor1', { extraPlugins: 'myplugin' } );
  1331. *
  1332. * @static
  1333. * @param {String} name The dialog's name.
  1334. * @param {Function/String} dialogDefinition
  1335. * A function returning the dialog's definition, or the URL to the `.js` file holding the function.
  1336. * The function should accept an argument `editor` which is the current editor instance, and
  1337. * return an object conforming to {@link CKEDITOR.dialog.definition}.
  1338. * @see CKEDITOR.dialog.definition
  1339. */
  1340. add: function( name, dialogDefinition ) {
  1341. // Avoid path registration from multiple instances override definition.
  1342. if ( !this._.dialogDefinitions[ name ] || typeof dialogDefinition == 'function' )
  1343. this._.dialogDefinitions[ name ] = dialogDefinition;
  1344. },
  1345. /**
  1346. * @static
  1347. * @todo
  1348. */
  1349. exists: function( name ) {
  1350. return !!this._.dialogDefinitions[ name ];
  1351. },
  1352. /**
  1353. * @static
  1354. * @todo
  1355. */
  1356. getCurrent: function() {
  1357. return CKEDITOR.dialog._.currentTop;
  1358. },
  1359. /**
  1360. * Check whether tab wasn't removed by {@link CKEDITOR.config#removeDialogTabs}.
  1361. *
  1362. * @since 4.1
  1363. * @static
  1364. * @param {CKEDITOR.editor} editor
  1365. * @param {String} dialogName
  1366. * @param {String} tabName
  1367. * @returns {Boolean}
  1368. */
  1369. isTabEnabled: function( editor, dialogName, tabName ) {
  1370. var cfg = editor.config.removeDialogTabs;
  1371. return !( cfg && cfg.match( new RegExp( '(?:^|;)' + dialogName + ':' + tabName + '(?:$|;)', 'i' ) ) );
  1372. },
  1373. /**
  1374. * The default OK button for dialogs. Fires the `ok` event and closes the dialog if the event succeeds.
  1375. *
  1376. * @static
  1377. * @method
  1378. */
  1379. okButton: ( function() {
  1380. var retval = function( editor, override ) {
  1381. override = override || {};
  1382. return CKEDITOR.tools.extend( {
  1383. id: 'ok',
  1384. type: 'button',
  1385. label: editor.lang.common.ok,
  1386. 'class': 'cke_dialog_ui_button_ok',
  1387. onClick: function( evt ) {
  1388. var dialog = evt.data.dialog;
  1389. if ( dialog.fire( 'ok', { hide: true } ).hide !== false )
  1390. dialog.hide();
  1391. }
  1392. }, override, true );
  1393. };
  1394. retval.type = 'button';
  1395. retval.override = function( override ) {
  1396. return CKEDITOR.tools.extend( function( editor ) {
  1397. return retval( editor, override );
  1398. }, { type: 'button' }, true );
  1399. };
  1400. return retval;
  1401. } )(),
  1402. /**
  1403. * The default cancel button for dialogs. Fires the `cancel` event and
  1404. * closes the dialog if no UI element value changed.
  1405. *
  1406. * @static
  1407. * @method
  1408. */
  1409. cancelButton: ( function() {
  1410. var retval = function( editor, override ) {
  1411. override = override || {};
  1412. return CKEDITOR.tools.extend( {
  1413. id: 'cancel',
  1414. type: 'button',
  1415. label: editor.lang.common.cancel,
  1416. 'class': 'cke_dialog_ui_button_cancel',
  1417. onClick: function( evt ) {
  1418. var dialog = evt.data.dialog;
  1419. if ( dialog.fire( 'cancel', { hide: true } ).hide !== false )
  1420. dialog.hide();
  1421. }
  1422. }, override, true );
  1423. };
  1424. retval.type = 'button';
  1425. retval.override = function( override ) {
  1426. return CKEDITOR.tools.extend( function( editor ) {
  1427. return retval( editor, override );
  1428. }, { type: 'button' }, true );
  1429. };
  1430. return retval;
  1431. } )(),
  1432. /**
  1433. * Registers a dialog UI element.
  1434. *
  1435. * @static
  1436. * @param {String} typeName The name of the UI element.
  1437. * @param {Function} builder The function to build the UI element.
  1438. */
  1439. addUIElement: function( typeName, builder ) {
  1440. this._.uiElementBuilders[ typeName ] = builder;
  1441. }
  1442. } );
  1443. CKEDITOR.dialog._ = {
  1444. uiElementBuilders: {},
  1445. dialogDefinitions: {},
  1446. currentTop: null,
  1447. currentZIndex: null
  1448. };
  1449. // "Inherit" (copy actually) from CKEDITOR.event.
  1450. CKEDITOR.event.implementOn( CKEDITOR.dialog );
  1451. CKEDITOR.event.implementOn( CKEDITOR.dialog.prototype );
  1452. var defaultDialogDefinition = {
  1453. resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
  1454. minWidth: 600,
  1455. minHeight: 400,
  1456. buttons: [ CKEDITOR.dialog.okButton, CKEDITOR.dialog.cancelButton ]
  1457. };
  1458. // Tool function used to return an item from an array based on its id
  1459. // property.
  1460. var getById = function( array, id, recurse ) {
  1461. for ( var i = 0, item;
  1462. ( item = array[ i ] ); i++ ) {
  1463. if ( item.id == id )
  1464. return item;
  1465. if ( recurse && item[ recurse ] ) {
  1466. var retval = getById( item[ recurse ], id, recurse );
  1467. if ( retval )
  1468. return retval;
  1469. }
  1470. }
  1471. return null;
  1472. };
  1473. // Tool function used to add an item into an array.
  1474. var addById = function( array, newItem, nextSiblingId, recurse, nullIfNotFound ) {
  1475. if ( nextSiblingId ) {
  1476. for ( var i = 0, item;
  1477. ( item = array[ i ] ); i++ ) {
  1478. if ( item.id == nextSiblingId ) {
  1479. array.splice( i, 0, newItem );
  1480. return newItem;
  1481. }
  1482. if ( recurse && item[ recurse ] ) {
  1483. var retval = addById( item[ recurse ], newItem, nextSiblingId, recurse, true );
  1484. if ( retval )
  1485. return retval;
  1486. }
  1487. }
  1488. if ( nullIfNotFound )
  1489. return null;
  1490. }
  1491. array.push( newItem );
  1492. return newItem;
  1493. };
  1494. // Tool function used to remove an item from an array based on its id.
  1495. var removeById = function( array, id, recurse ) {
  1496. for ( var i = 0, item;
  1497. ( item = array[ i ] ); i++ ) {
  1498. if ( item.id == id )
  1499. return array.splice( i, 1 );
  1500. if ( recurse && item[ recurse ] ) {
  1501. var retval = removeById( item[ recurse ], id, recurse );
  1502. if ( retval )
  1503. return retval;
  1504. }
  1505. }
  1506. return null;
  1507. };
  1508. /**
  1509. * This class is not really part of the API. It is the `definition` property value
  1510. * passed to `dialogDefinition` event handlers.
  1511. *
  1512. * CKEDITOR.on( 'dialogDefinition', function( evt ) {
  1513. * var definition = evt.data.definition;
  1514. * var content = definition.getContents( 'page1' );
  1515. * // ...
  1516. * } );
  1517. *
  1518. * @private
  1519. * @class CKEDITOR.dialog.definitionObject
  1520. * @extends CKEDITOR.dialog.definition
  1521. * @constructor Creates a definitionObject class instance.
  1522. */
  1523. var definitionObject = function( dialog, dialogDefinition ) {
  1524. // TODO : Check if needed.
  1525. this.dialog = dialog;
  1526. // Transform the contents entries in contentObjects.
  1527. var contents = dialogDefinition.contents;
  1528. for ( var i = 0, content;
  1529. ( content = contents[ i ] ); i++ )
  1530. contents[ i ] = content && new contentObject( dialog, content );
  1531. CKEDITOR.tools.extend( this, dialogDefinition );
  1532. };
  1533. definitionObject.prototype = {
  1534. /**
  1535. * Gets a content definition.
  1536. *
  1537. * @param {String} id The id of the content definition.
  1538. * @returns {CKEDITOR.dialog.definition.content} The content definition matching id.
  1539. */
  1540. getContents: function( id ) {
  1541. return getById( this.contents, id );
  1542. },
  1543. /**
  1544. * Gets a button definition.
  1545. *
  1546. * @param {String} id The id of the button definition.
  1547. * @returns {CKEDITOR.dialog.definition.button} The button definition matching id.
  1548. */
  1549. getButton: function( id ) {
  1550. return getById( this.buttons, id );
  1551. },
  1552. /**
  1553. * Adds a content definition object under this dialog definition.
  1554. *
  1555. * @param {CKEDITOR.dialog.definition.content} contentDefinition The
  1556. * content definition.
  1557. * @param {String} [nextSiblingId] The id of an existing content
  1558. * definition which the new content definition will be inserted
  1559. * before. Omit if the new content definition is to be inserted as
  1560. * the last item.
  1561. * @returns {CKEDITOR.dialog.definition.content} The inserted content definition.
  1562. */
  1563. addContents: function( contentDefinition, nextSiblingId ) {
  1564. return addById( this.contents, contentDefinition, nextSiblingId );
  1565. },
  1566. /**
  1567. * Adds a button definition object under this dialog definition.
  1568. *
  1569. * @param {CKEDITOR.dialog.definition.button} buttonDefinition The
  1570. * button definition.
  1571. * @param {String} [nextSiblingId] The id of an existing button
  1572. * definition which the new button definition will be inserted
  1573. * before. Omit if the new button definition is to be inserted as
  1574. * the last item.
  1575. * @returns {CKEDITOR.dialog.definition.button} The inserted button definition.
  1576. */
  1577. addButton: function( buttonDefinition, nextSiblingId ) {
  1578. return addById( this.buttons, buttonDefinition, nextSiblingId );
  1579. },
  1580. /**
  1581. * Removes a content definition from this dialog definition.
  1582. *
  1583. * @param {String} id The id of the content definition to be removed.
  1584. * @returns {CKEDITOR.dialog.definition.content} The removed content definition.
  1585. */
  1586. removeContents: function( id ) {
  1587. removeById( this.contents, id );
  1588. },
  1589. /**
  1590. * Removes a button definition from the dialog definition.
  1591. *
  1592. * @param {String} id The id of the button definition to be removed.
  1593. * @returns {CKEDITOR.dialog.definition.button} The removed button definition.
  1594. */
  1595. removeButton: function( id ) {
  1596. removeById( this.buttons, id );
  1597. }
  1598. };
  1599. /**
  1600. * This class is not really part of the API. It is the template of the
  1601. * objects representing content pages inside the
  1602. * CKEDITOR.dialog.definitionObject.
  1603. *
  1604. * CKEDITOR.on( 'dialogDefinition', function( evt ) {
  1605. * var definition = evt.data.definition;
  1606. * var content = definition.getContents( 'page1' );
  1607. * content.remove( 'textInput1' );
  1608. * // ...
  1609. * } );
  1610. *
  1611. * @private
  1612. * @class CKEDITOR.dialog.definition.contentObject
  1613. * @constructor Creates a contentObject class instance.
  1614. */
  1615. function contentObject( dialog, contentDefinition ) {
  1616. this._ = {
  1617. dialog: dialog
  1618. };
  1619. CKEDITOR.tools.extend( this, contentDefinition );
  1620. }
  1621. contentObject.prototype = {
  1622. /**
  1623. * Gets a UI element definition under the content definition.
  1624. *
  1625. * @param {String} id The id of the UI element definition.
  1626. * @returns {CKEDITOR.dialog.definition.uiElement}
  1627. */
  1628. get: function( id ) {
  1629. return getById( this.elements, id, 'children' );
  1630. },
  1631. /**
  1632. * Adds a UI element definition to the content definition.
  1633. *
  1634. * @param {CKEDITOR.dialog.definition.uiElement} elementDefinition The
  1635. * UI elemnet definition to be added.
  1636. * @param {String} nextSiblingId The id of an existing UI element
  1637. * definition which the new UI element definition will be inserted
  1638. * before. Omit if the new button definition is to be inserted as
  1639. * the last item.
  1640. * @returns {CKEDITOR.dialog.definition.uiElement} The element definition inserted.
  1641. */
  1642. add: function( elementDefinition, nextSiblingId ) {
  1643. return addById( this.elements, elementDefinition, nextSiblingId, 'children' );
  1644. },
  1645. /**
  1646. * Removes a UI element definition from the content definition.
  1647. *
  1648. * @param {String} id The id of the UI element definition to be removed.
  1649. * @returns {CKEDITOR.dialog.definition.uiElement} The element definition removed.
  1650. */
  1651. remove: function( id ) {
  1652. removeById( this.elements, id, 'children' );
  1653. }
  1654. };
  1655. function initDragAndDrop( dialog ) {
  1656. var lastCoords = null,
  1657. abstractDialogCoords = null,
  1658. editor = dialog.getParentEditor(),
  1659. magnetDistance = editor.config.dialog_magnetDistance,
  1660. margins = CKEDITOR.skin.margins || [ 0, 0, 0, 0 ];
  1661. if ( typeof magnetDistance == 'undefined' )
  1662. magnetDistance = 20;
  1663. function mouseMoveHandler( evt ) {
  1664. var dialogSize = dialog.getSize(),
  1665. viewPaneSize = CKEDITOR.document.getWindow().getViewPaneSize(),
  1666. x = evt.data.$.screenX,
  1667. y = evt.data.$.screenY,
  1668. dx = x - lastCoords.x,
  1669. dy = y - lastCoords.y,
  1670. realX, realY;
  1671. lastCoords = { x: x, y: y };
  1672. abstractDialogCoords.x += dx;
  1673. abstractDialogCoords.y += dy;
  1674. if ( abstractDialogCoords.x + margins[ 3 ] < magnetDistance )
  1675. realX = -margins[ 3 ];
  1676. else if ( abstractDialogCoords.x - margins[ 1 ] > viewPaneSize.width - dialogSize.width - magnetDistance )
  1677. realX = viewPaneSize.width - dialogSize.width + ( editor.lang.dir == 'rtl' ? 0 : margins[ 1 ] );
  1678. else
  1679. realX = abstractDialogCoords.x;
  1680. if ( abstractDialogCoords.y + margins[ 0 ] < magnetDistance )
  1681. realY = -margins[ 0 ];
  1682. else if ( abstractDialogCoords.y - margins[ 2 ] > viewPaneSize.height - dialogSize.height - magnetDistance )
  1683. realY = viewPaneSize.height - dialogSize.height + margins[ 2 ];
  1684. else
  1685. realY = abstractDialogCoords.y;
  1686. dialog.move( realX, realY, 1 );
  1687. evt.data.preventDefault();
  1688. }
  1689. function mouseUpHandler() {
  1690. CKEDITOR.document.removeListener( 'mousemove', mouseMoveHandler );
  1691. CKEDITOR.document.removeListener( 'mouseup', mouseUpHandler );
  1692. if ( CKEDITOR.env.ie6Compat ) {
  1693. var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
  1694. coverDoc.removeListener( 'mousemove', mouseMoveHandler );
  1695. coverDoc.removeListener( 'mouseup', mouseUpHandler );
  1696. }
  1697. }
  1698. dialog.parts.title.on( 'mousedown', function( evt ) {
  1699. lastCoords = { x: evt.data.$.screenX, y: evt.data.$.screenY };
  1700. CKEDITOR.document.on( 'mousemove', mouseMoveHandler );
  1701. CKEDITOR.document.on( 'mouseup', mouseUpHandler );
  1702. abstractDialogCoords = dialog.getPosition();
  1703. if ( CKEDITOR.env.ie6Compat ) {
  1704. var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
  1705. coverDoc.on( 'mousemove', mouseMoveHandler );
  1706. coverDoc.on( 'mouseup', mouseUpHandler );
  1707. }
  1708. evt.data.preventDefault();
  1709. }, dialog );
  1710. }
  1711. function initResizeHandles( dialog ) {
  1712. var def = dialog.definition,
  1713. resizable = def.resizable;
  1714. if ( resizable == CKEDITOR.DIALOG_RESIZE_NONE )
  1715. return;
  1716. var editor = dialog.getParentEditor();
  1717. var wrapperWidth, wrapperHeight, viewSize, origin, startSize, dialogCover;
  1718. var mouseDownFn = CKEDITOR.tools.addFunction( function( $event ) {
  1719. startSize = dialog.getSize();
  1720. var content = dialog.parts.contents,
  1721. iframeDialog = content.$.getElementsByTagName( 'iframe' ).length;
  1722. // Shim to help capturing "mousemove" over iframe.
  1723. if ( iframeDialog ) {
  1724. dialogCover = CKEDITOR.dom.element.createFromHtml( '<div class="cke_dialog_resize_cover" style="height: 100%; position: absolute; width: 100%;"></div>' );
  1725. content.append( dialogCover );
  1726. }
  1727. // Calculate the offset between content and chrome size.
  1728. wrapperHeight = startSize.height - dialog.parts.contents.getSize( 'height', !( CKEDITOR.env.gecko || CKEDITOR.env.ie && CKEDITOR.env.quirks ) );
  1729. wrapperWidth = startSize.width - dialog.parts.contents.getSize( 'width', 1 );
  1730. origin = { x: $event.screenX, y: $event.screenY };
  1731. viewSize = CKEDITOR.document.getWindow().getViewPaneSize();
  1732. CKEDITOR.document.on( 'mousemove', mouseMoveHandler );
  1733. CKEDITOR.document.on( 'mouseup', mouseUpHandler );
  1734. if ( CKEDITOR.env.ie6Compat ) {
  1735. var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
  1736. coverDoc.on( 'mousemove', mouseMoveHandler );
  1737. coverDoc.on( 'mouseup', mouseUpHandler );
  1738. }
  1739. $event.preventDefault && $event.preventDefault();
  1740. } );
  1741. // Prepend the grip to the dialog.
  1742. dialog.on( 'load', function() {
  1743. var direction = '';
  1744. if ( resizable == CKEDITOR.DIALOG_RESIZE_WIDTH )
  1745. direction = ' cke_resizer_horizontal';
  1746. else if ( resizable == CKEDITOR.DIALOG_RESIZE_HEIGHT )
  1747. direction = ' cke_resizer_vertical';
  1748. var resizer = CKEDITOR.dom.element.createFromHtml(
  1749. '<div' +
  1750. ' class="cke_resizer' + direction + ' cke_resizer_' + editor.lang.dir + '"' +
  1751. ' title="' + CKEDITOR.tools.htmlEncode( editor.lang.common.resize ) + '"' +
  1752. ' onmousedown="CKEDITOR.tools.callFunction(' + mouseDownFn + ', event )">' +
  1753. // BLACK LOWER RIGHT TRIANGLE (ltr)
  1754. // BLACK LOWER LEFT TRIANGLE (rtl)
  1755. ( editor.lang.dir == 'ltr' ? '\u25E2' : '\u25E3' ) +
  1756. '</div>' );
  1757. dialog.parts.footer.append( resizer, 1 );
  1758. } );
  1759. editor.on( 'destroy', function() {
  1760. CKEDITOR.tools.removeFunction( mouseDownFn );
  1761. } );
  1762. function mouseMoveHandler( evt ) {
  1763. var rtl = editor.lang.dir == 'rtl',
  1764. dx = ( evt.data.$.screenX - origin.x ) * ( rtl ? -1 : 1 ),
  1765. dy = evt.data.$.screenY - origin.y,
  1766. width = startSize.width,
  1767. height = startSize.height,
  1768. internalWidth = width + dx * ( dialog._.moved ? 1 : 2 ),
  1769. internalHeight = height + dy * ( dialog._.moved ? 1 : 2 ),
  1770. element = dialog._.element.getFirst(),
  1771. right = rtl && element.getComputedStyle( 'right' ),
  1772. position = dialog.getPosition();
  1773. if ( position.y + internalHeight > viewSize.height )
  1774. internalHeight = viewSize.height - position.y;
  1775. if ( ( rtl ? right : position.x ) + internalWidth > viewSize.width )
  1776. internalWidth = viewSize.width - ( rtl ? right : position.x );
  1777. // Make sure the dialog will not be resized to the wrong side when it's in the leftmost position for RTL.
  1778. if ( ( resizable == CKEDITOR.DIALOG_RESIZE_WIDTH || resizable == CKEDITOR.DIALOG_RESIZE_BOTH ) )
  1779. width = Math.max( def.minWidth || 0, internalWidth - wrapperWidth );
  1780. if ( resizable == CKEDITOR.DIALOG_RESIZE_HEIGHT || resizable == CKEDITOR.DIALOG_RESIZE_BOTH )
  1781. height = Math.max( def.minHeight || 0, internalHeight - wrapperHeight );
  1782. dialog.resize( width, height );
  1783. if ( !dialog._.moved )
  1784. dialog.layout();
  1785. evt.data.preventDefault();
  1786. }
  1787. function mouseUpHandler() {
  1788. CKEDITOR.document.removeListener( 'mouseup', mouseUpHandler );
  1789. CKEDITOR.document.removeListener( 'mousemove', mouseMoveHandler );
  1790. if ( dialogCover ) {
  1791. dialogCover.remove();
  1792. dialogCover = null;
  1793. }
  1794. if ( CKEDITOR.env.ie6Compat ) {
  1795. var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
  1796. coverDoc.removeListener( 'mouseup', mouseUpHandler );
  1797. coverDoc.removeListener( 'mousemove', mouseMoveHandler );
  1798. }
  1799. }
  1800. }
  1801. var resizeCover;
  1802. // Caching resuable covers and allowing only one cover
  1803. // on screen.
  1804. var covers = {},
  1805. currentCover;
  1806. function cancelEvent( ev ) {
  1807. ev.data.preventDefault( 1 );
  1808. }
  1809. function showCover( editor ) {
  1810. var win = CKEDITOR.document.getWindow();
  1811. var config = editor.config,
  1812. backgroundColorStyle = config.dialog_backgroundCoverColor || 'white',
  1813. backgroundCoverOpacity = config.dialog_backgroundCoverOpacity,
  1814. baseFloatZIndex = config.baseFloatZIndex,
  1815. coverKey = CKEDITOR.tools.genKey( backgroundColorStyle, backgroundCoverOpacity, baseFloatZIndex ),
  1816. coverElement = covers[ coverKey ];
  1817. if ( !coverElement ) {
  1818. var html = [
  1819. '<div tabIndex="-1" style="position: ', ( CKEDITOR.env.ie6Compat ? 'absolute' : 'fixed' ),
  1820. '; z-index: ', baseFloatZIndex,
  1821. '; top: 0px; left: 0px; ',
  1822. ( !CKEDITOR.env.ie6Compat ? 'background-color: ' + backgroundColorStyle : '' ),
  1823. '" class="cke_dialog_background_cover">'
  1824. ];
  1825. if ( CKEDITOR.env.ie6Compat ) {
  1826. // Support for custom document.domain in IE.
  1827. var iframeHtml = '<html><body style=\\\'background-color:' + backgroundColorStyle + ';\\\'></body></html>';
  1828. html.push( '<iframe' +
  1829. ' hidefocus="true"' +
  1830. ' frameborder="0"' +
  1831. ' id="cke_dialog_background_iframe"' +
  1832. ' src="javascript:' );
  1833. html.push( 'void((function(){' + encodeURIComponent(
  1834. 'document.open();' +
  1835. // Support for custom document.domain in IE.
  1836. '(' + CKEDITOR.tools.fixDomain + ')();' +
  1837. 'document.write( \'' + iframeHtml + '\' );' +
  1838. 'document.close();'
  1839. ) + '})())' );
  1840. html.push( '"' +
  1841. ' style="' +
  1842. 'position:absolute;' +
  1843. 'left:0;' +
  1844. 'top:0;' +
  1845. 'width:100%;' +
  1846. 'height: 100%;' +
  1847. 'filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0)">' +
  1848. '</iframe>' );
  1849. }
  1850. html.push( '</div>' );
  1851. coverElement = CKEDITOR.dom.element.createFromHtml( html.join( '' ) );
  1852. coverElement.setOpacity( backgroundCoverOpacity !== undefined ? backgroundCoverOpacity : 0.5 );
  1853. coverElement.on( 'keydown', cancelEvent );
  1854. coverElement.on( 'keypress', cancelEvent );
  1855. coverElement.on( 'keyup', cancelEvent );
  1856. coverElement.appendTo( CKEDITOR.document.getBody() );
  1857. covers[ coverKey ] = coverElement;
  1858. } else {
  1859. coverElement.show();
  1860. }
  1861. // Makes the dialog cover a focus holder as well.
  1862. editor.focusManager.add( coverElement );
  1863. currentCover = coverElement;
  1864. var resizeFunc = function() {
  1865. var size = win.getViewPaneSize();
  1866. coverElement.setStyles( {
  1867. width: size.width + 'px',
  1868. height: size.height + 'px'
  1869. } );
  1870. };
  1871. var scrollFunc = function() {
  1872. var pos = win.getScrollPosition(),
  1873. cursor = CKEDITOR.dialog._.currentTop;
  1874. coverElement.setStyles( {
  1875. left: pos.x + 'px',
  1876. top: pos.y + 'px'
  1877. } );
  1878. if ( cursor ) {
  1879. do {
  1880. var dialogPos = cursor.getPosition();
  1881. cursor.move( dialogPos.x, dialogPos.y );
  1882. } while ( ( cursor = cursor._.parentDialog ) );
  1883. }
  1884. };
  1885. resizeCover = resizeFunc;
  1886. win.on( 'resize', resizeFunc );
  1887. resizeFunc();
  1888. // Using Safari/Mac, focus must be kept where it is (#7027)
  1889. if ( !( CKEDITOR.env.mac && CKEDITOR.env.webkit ) )
  1890. coverElement.focus();
  1891. if ( CKEDITOR.env.ie6Compat ) {
  1892. // IE BUG: win.$.onscroll assignment doesn't work.. it must be window.onscroll.
  1893. // So we need to invent a really funny way to make it work.
  1894. var myScrollHandler = function() {
  1895. scrollFunc();
  1896. arguments.callee.prevScrollHandler.apply( this, arguments );
  1897. };
  1898. win.$.setTimeout( function() {
  1899. myScrollHandler.prevScrollHandler = window.onscroll ||
  1900. function() {};
  1901. window.onscroll = myScrollHandler;
  1902. }, 0 );
  1903. scrollFunc();
  1904. }
  1905. }
  1906. function hideCover( editor ) {
  1907. if ( !currentCover )
  1908. return;
  1909. editor.focusManager.remove( currentCover );
  1910. var win = CKEDITOR.document.getWindow();
  1911. currentCover.hide();
  1912. win.removeListener( 'resize', resizeCover );
  1913. if ( CKEDITOR.env.ie6Compat ) {
  1914. win.$.setTimeout( function() {
  1915. var prevScrollHandler = window.onscroll && window.onscroll.prevScrollHandler;
  1916. window.onscroll = prevScrollHandler || null;
  1917. }, 0 );
  1918. }
  1919. resizeCover = null;
  1920. }
  1921. function removeCovers() {
  1922. for ( var coverId in covers )
  1923. covers[ coverId ].remove();
  1924. covers = {};
  1925. }
  1926. var accessKeyProcessors = {};
  1927. var accessKeyDownHandler = function( evt ) {
  1928. var ctrl = evt.data.$.ctrlKey || evt.data.$.metaKey,
  1929. alt = evt.data.$.altKey,
  1930. shift = evt.data.$.shiftKey,
  1931. key = String.fromCharCode( evt.data.$.keyCode ),
  1932. keyProcessor = accessKeyProcessors[ ( ctrl ? 'CTRL+' : '' ) + ( alt ? 'ALT+' : '' ) + ( shift ? 'SHIFT+' : '' ) + key ];
  1933. if ( !keyProcessor || !keyProcessor.length )
  1934. return;
  1935. keyProcessor = keyProcessor[ keyProcessor.length - 1 ];
  1936. keyProcessor.keydown && keyProcessor.keydown.call( keyProcessor.uiElement, keyProcessor.dialog, keyProcessor.key );
  1937. evt.data.preventDefault();
  1938. };
  1939. var accessKeyUpHandler = function( evt ) {
  1940. var ctrl = evt.data.$.ctrlKey || evt.data.$.metaKey,
  1941. alt = evt.data.$.altKey,
  1942. shift = evt.data.$.shiftKey,
  1943. key = String.fromCharCode( evt.data.$.keyCode ),
  1944. keyProcessor = accessKeyProcessors[ ( ctrl ? 'CTRL+' : '' ) + ( alt ? 'ALT+' : '' ) + ( shift ? 'SHIFT+' : '' ) + key ];
  1945. if ( !keyProcessor || !keyProcessor.length )
  1946. return;
  1947. keyProcessor = keyProcessor[ keyProcessor.length - 1 ];
  1948. if ( keyProcessor.keyup ) {
  1949. keyProcessor.keyup.call( keyProcessor.uiElement, keyProcessor.dialog, keyProcessor.key );
  1950. evt.data.preventDefault();
  1951. }
  1952. };
  1953. var registerAccessKey = function( uiElement, dialog, key, downFunc, upFunc ) {
  1954. var procList = accessKeyProcessors[ key ] || ( accessKeyProcessors[ key ] = [] );
  1955. procList.push( {
  1956. uiElement: uiElement,
  1957. dialog: dialog,
  1958. key: key,
  1959. keyup: upFunc || uiElement.accessKeyUp,
  1960. keydown: downFunc || uiElement.accessKeyDown
  1961. } );
  1962. };
  1963. var unregisterAccessKey = function( obj ) {
  1964. for ( var i in accessKeyProcessors ) {
  1965. var list = accessKeyProcessors[ i ];
  1966. for ( var j = list.length - 1; j >= 0; j-- ) {
  1967. if ( list[ j ].dialog == obj || list[ j ].uiElement == obj )
  1968. list.splice( j, 1 );
  1969. }
  1970. if ( list.length === 0 )
  1971. delete accessKeyProcessors[ i ];
  1972. }
  1973. };
  1974. var tabAccessKeyUp = function( dialog, key ) {
  1975. if ( dialog._.accessKeyMap[ key ] )
  1976. dialog.selectPage( dialog._.accessKeyMap[ key ] );
  1977. };
  1978. var tabAccessKeyDown = function() {};
  1979. ( function() {
  1980. CKEDITOR.ui.dialog = {
  1981. /**
  1982. * The base class of all dialog UI elements.
  1983. *
  1984. * @class CKEDITOR.ui.dialog.uiElement
  1985. * @constructor Creates a uiElement class instance.
  1986. * @param {CKEDITOR.dialog} dialog Parent dialog object.
  1987. * @param {CKEDITOR.dialog.definition.uiElement} elementDefinition Element
  1988. * definition.
  1989. *
  1990. * Accepted fields:
  1991. *
  1992. * * `id` (Required) The id of the UI element. See {@link CKEDITOR.dialog#getContentElement}.
  1993. * * `type` (Required) The type of the UI element. The
  1994. * value to this field specifies which UI element class will be used to
  1995. * generate the final widget.
  1996. * * `title` (Optional) The popup tooltip for the UI
  1997. * element.
  1998. * * `hidden` (Optional) A flag that tells if the element
  1999. * should be initially visible.
  2000. * * `className` (Optional) Additional CSS class names
  2001. * to add to the UI element. Separated by space.
  2002. * * `style` (Optional) Additional CSS inline styles
  2003. * to add to the UI element. A semicolon (;) is required after the last
  2004. * style declaration.
  2005. * * `accessKey` (Optional) The alphanumeric access key
  2006. * for this element. Access keys are automatically prefixed by CTRL.
  2007. * * `on*` (Optional) Any UI element definition field that
  2008. * starts with `on` followed immediately by a capital letter and
  2009. * probably more letters is an event handler. Event handlers may be further
  2010. * divided into registered event handlers and DOM event handlers. Please
  2011. * refer to {@link CKEDITOR.ui.dialog.uiElement#registerEvents} and
  2012. * {@link CKEDITOR.ui.dialog.uiElement#eventProcessors} for more information.
  2013. *
  2014. * @param {Array} htmlList
  2015. * List of HTML code to be added to the dialog's content area.
  2016. * @param {Function/String} [nodeNameArg='div']
  2017. * A function returning a string, or a simple string for the node name for
  2018. * the root DOM node.
  2019. * @param {Function/Object} [stylesArg={}]
  2020. * A function returning an object, or a simple object for CSS styles applied
  2021. * to the DOM node.
  2022. * @param {Function/Object} [attributesArg={}]
  2023. * A fucntion returning an object, or a simple object for attributes applied
  2024. * to the DOM node.
  2025. * @param {Function/String} [contentsArg='']
  2026. * A function returning a string, or a simple string for the HTML code inside
  2027. * the root DOM node. Default is empty string.
  2028. */
  2029. uiElement: function( dialog, elementDefinition, htmlList, nodeNameArg, stylesArg, attributesArg, contentsArg ) {
  2030. if ( arguments.length < 4 )
  2031. return;
  2032. var nodeName = ( nodeNameArg.call ? nodeNameArg( elementDefinition ) : nodeNameArg ) || 'div',
  2033. html = [ '<', nodeName, ' ' ],
  2034. styles = ( stylesArg && stylesArg.call ? stylesArg( elementDefinition ) : stylesArg ) || {},
  2035. attributes = ( attributesArg && attributesArg.call ? attributesArg( elementDefinition ) : attributesArg ) || {},
  2036. innerHTML = ( contentsArg && contentsArg.call ? contentsArg.call( this, dialog, elementDefinition ) : contentsArg ) || '',
  2037. domId = this.domId = attributes.id || CKEDITOR.tools.getNextId() + '_uiElement',
  2038. i;
  2039. if ( elementDefinition.requiredContent && !dialog.getParentEditor().filter.check( elementDefinition.requiredContent ) ) {
  2040. styles.display = 'none';
  2041. this.notAllowed = true;
  2042. }
  2043. // Set the id, a unique id is required for getElement() to work.
  2044. attributes.id = domId;
  2045. // Set the type and definition CSS class names.
  2046. var classes = {};
  2047. if ( elementDefinition.type )
  2048. classes[ 'cke_dialog_ui_' + elementDefinition.type ] = 1;
  2049. if ( elementDefinition.className )
  2050. classes[ elementDefinition.className ] = 1;
  2051. if ( elementDefinition.disabled )
  2052. classes.cke_disabled = 1;
  2053. var attributeClasses = ( attributes[ 'class' ] && attributes[ 'class' ].split ) ? attributes[ 'class' ].split( ' ' ) : [];
  2054. for ( i = 0; i < attributeClasses.length; i++ ) {
  2055. if ( attributeClasses[ i ] )
  2056. classes[ attributeClasses[ i ] ] = 1;
  2057. }
  2058. var finalClasses = [];
  2059. for ( i in classes )
  2060. finalClasses.push( i );
  2061. attributes[ 'class' ] = finalClasses.join( ' ' );
  2062. // Set the popup tooltop.
  2063. if ( elementDefinition.title )
  2064. attributes.title = elementDefinition.title;
  2065. // Write the inline CSS styles.
  2066. var styleStr = ( elementDefinition.style || '' ).split( ';' );
  2067. // Element alignment support.
  2068. if ( elementDefinition.align ) {
  2069. var align = elementDefinition.align;
  2070. styles[ 'margin-left' ] = align == 'left' ? 0 : 'auto';
  2071. styles[ 'margin-right' ] = align == 'right' ? 0 : 'auto';
  2072. }
  2073. for ( i in styles )
  2074. styleStr.push( i + ':' + styles[ i ] );
  2075. if ( elementDefinition.hidden )
  2076. styleStr.push( 'display:none' );
  2077. for ( i = styleStr.length - 1; i >= 0; i-- ) {
  2078. if ( styleStr[ i ] === '' )
  2079. styleStr.splice( i, 1 );
  2080. }
  2081. if ( styleStr.length > 0 )
  2082. attributes.style = ( attributes.style ? ( attributes.style + '; ' ) : '' ) + styleStr.join( '; ' );
  2083. // Write the attributes.
  2084. for ( i in attributes )
  2085. html.push( i + '="' + CKEDITOR.tools.htmlEncode( attributes[ i ] ) + '" ' );
  2086. // Write the content HTML.
  2087. html.push( '>', innerHTML, '</', nodeName, '>' );
  2088. // Add contents to the parent HTML array.
  2089. htmlList.push( html.join( '' ) );
  2090. ( this._ || ( this._ = {} ) ).dialog = dialog;
  2091. // Override isChanged if it is defined in element definition.
  2092. if ( typeof elementDefinition.isChanged == 'boolean' )
  2093. this.isChanged = function() {
  2094. return elementDefinition.isChanged;
  2095. };
  2096. if ( typeof elementDefinition.isChanged == 'function' )
  2097. this.isChanged = elementDefinition.isChanged;
  2098. // Overload 'get(set)Value' on definition.
  2099. if ( typeof elementDefinition.setValue == 'function' ) {
  2100. this.setValue = CKEDITOR.tools.override( this.setValue, function( org ) {
  2101. return function( val ) {
  2102. org.call( this, elementDefinition.setValue.call( this, val ) );
  2103. };
  2104. } );
  2105. }
  2106. if ( typeof elementDefinition.getValue == 'function' ) {
  2107. this.getValue = CKEDITOR.tools.override( this.getValue, function( org ) {
  2108. return function() {
  2109. return elementDefinition.getValue.call( this, org.call( this ) );
  2110. };
  2111. } );
  2112. }
  2113. // Add events.
  2114. CKEDITOR.event.implementOn( this );
  2115. this.registerEvents( elementDefinition );
  2116. if ( this.accessKeyUp && this.accessKeyDown && elementDefinition.accessKey )
  2117. registerAccessKey( this, dialog, 'CTRL+' + elementDefinition.accessKey );
  2118. var me = this;
  2119. dialog.on( 'load', function() {
  2120. var input = me.getInputElement();
  2121. if ( input ) {
  2122. var focusClass = me.type in { 'checkbox': 1, 'ratio': 1 } && CKEDITOR.env.ie && CKEDITOR.env.version < 8 ? 'cke_dialog_ui_focused' : '';
  2123. input.on( 'focus', function() {
  2124. dialog._.tabBarMode = false;
  2125. dialog._.hasFocus = true;
  2126. me.fire( 'focus' );
  2127. focusClass && this.addClass( focusClass );
  2128. } );
  2129. input.on( 'blur', function() {
  2130. me.fire( 'blur' );
  2131. focusClass && this.removeClass( focusClass );
  2132. } );
  2133. }
  2134. } );
  2135. // Completes this object with everything we have in the
  2136. // definition.
  2137. CKEDITOR.tools.extend( this, elementDefinition );
  2138. // Register the object as a tab focus if it can be included.
  2139. if ( this.keyboardFocusable ) {
  2140. this.tabIndex = elementDefinition.tabIndex || 0;
  2141. this.focusIndex = dialog._.focusList.push( this ) - 1;
  2142. this.on( 'focus', function() {
  2143. dialog._.currentFocusIndex = me.focusIndex;
  2144. } );
  2145. }
  2146. },
  2147. /**
  2148. * Horizontal layout box for dialog UI elements, auto-expends to available width of container.
  2149. *
  2150. * @class CKEDITOR.ui.dialog.hbox
  2151. * @extends CKEDITOR.ui.dialog.uiElement
  2152. * @constructor Creates a hbox class instance.
  2153. * @param {CKEDITOR.dialog} dialog Parent dialog object.
  2154. * @param {Array} childObjList
  2155. * Array of {@link CKEDITOR.ui.dialog.uiElement} objects inside this container.
  2156. * @param {Array} childHtmlList
  2157. * Array of HTML code that correspond to the HTML output of all the
  2158. * objects in childObjList.
  2159. * @param {Array} htmlList
  2160. * Array of HTML code that this element will output to.
  2161. * @param {CKEDITOR.dialog.definition.uiElement} elementDefinition
  2162. * The element definition. Accepted fields:
  2163. *
  2164. * * `widths` (Optional) The widths of child cells.
  2165. * * `height` (Optional) The height of the layout.
  2166. * * `padding` (Optional) The padding width inside child cells.
  2167. * * `align` (Optional) The alignment of the whole layout.
  2168. */
  2169. hbox: function( dialog, childObjList, childHtmlList, htmlList, elementDefinition ) {
  2170. if ( arguments.length < 4 )
  2171. return;
  2172. this._ || ( this._ = {} );
  2173. var children = this._.children = childObjList,
  2174. widths = elementDefinition && elementDefinition.widths || null,
  2175. height = elementDefinition && elementDefinition.height || null,
  2176. styles = {},
  2177. i;
  2178. /** @ignore */
  2179. var innerHTML = function() {
  2180. var html = [ '<tbody><tr class="cke_dialog_ui_hbox">' ];
  2181. for ( i = 0; i < childHtmlList.length; i++ ) {
  2182. var className = 'cke_dialog_ui_hbox_child',
  2183. styles = [];
  2184. if ( i === 0 ) {
  2185. className = 'cke_dialog_ui_hbox_first';
  2186. }
  2187. if ( i == childHtmlList.length - 1 ) {
  2188. className = 'cke_dialog_ui_hbox_last';
  2189. }
  2190. html.push( '<td class="', className, '" role="presentation" ' );
  2191. if ( widths ) {
  2192. if ( widths[ i ] ) {
  2193. styles.push( 'width:' + cssLength( widths[i] ) );
  2194. }
  2195. } else {
  2196. styles.push( 'width:' + Math.floor( 100 / childHtmlList.length ) + '%' );
  2197. }
  2198. if ( height ) {
  2199. styles.push( 'height:' + cssLength( height ) );
  2200. }
  2201. if ( elementDefinition && elementDefinition.padding !== undefined ) {
  2202. styles.push( 'padding:' + cssLength( elementDefinition.padding ) );
  2203. }
  2204. // In IE Quirks alignment has to be done on table cells. (#7324)
  2205. if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && children[ i ].align ) {
  2206. styles.push( 'text-align:' + children[ i ].align );
  2207. }
  2208. if ( styles.length > 0 ) {
  2209. html.push( 'style="' + styles.join( '; ' ) + '" ' );
  2210. }
  2211. html.push( '>', childHtmlList[ i ], '</td>' );
  2212. }
  2213. html.push( '</tr></tbody>' );
  2214. return html.join( '' );
  2215. };
  2216. var attribs = { role: 'presentation' };
  2217. elementDefinition && elementDefinition.align && ( attribs.align = elementDefinition.align );
  2218. CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition || { type: 'hbox' }, htmlList, 'table', styles, attribs, innerHTML );
  2219. },
  2220. /**
  2221. * Vertical layout box for dialog UI elements.
  2222. *
  2223. * @class CKEDITOR.ui.dialog.vbox
  2224. * @extends CKEDITOR.ui.dialog.hbox
  2225. * @constructor Creates a vbox class instance.
  2226. * @param {CKEDITOR.dialog} dialog Parent dialog object.
  2227. * @param {Array} childObjList
  2228. * Array of {@link CKEDITOR.ui.dialog.uiElement} objects inside this container.
  2229. * @param {Array} childHtmlList
  2230. * Array of HTML code that correspond to the HTML output of all the
  2231. * objects in childObjList.
  2232. * @param {Array} htmlList Array of HTML code that this element will output to.
  2233. * @param {CKEDITOR.dialog.definition.uiElement} elementDefinition
  2234. * The element definition. Accepted fields:
  2235. *
  2236. * * `width` (Optional) The width of the layout.
  2237. * * `heights` (Optional) The heights of individual cells.
  2238. * * `align` (Optional) The alignment of the layout.
  2239. * * `padding` (Optional) The padding width inside child cells.
  2240. * * `expand` (Optional) Whether the layout should expand
  2241. * vertically to fill its container.
  2242. */
  2243. vbox: function( dialog, childObjList, childHtmlList, htmlList, elementDefinition ) {
  2244. if ( arguments.length < 3 )
  2245. return;
  2246. this._ || ( this._ = {} );
  2247. var children = this._.children = childObjList,
  2248. width = elementDefinition && elementDefinition.width || null,
  2249. heights = elementDefinition && elementDefinition.heights || null;
  2250. /** @ignore */
  2251. var innerHTML = function() {
  2252. var html = [ '<table role="presentation" cellspacing="0" border="0" ' ];
  2253. html.push( 'style="' );
  2254. if ( elementDefinition && elementDefinition.expand )
  2255. html.push( 'height:100%;' );
  2256. html.push( 'width:' + cssLength( width || '100%' ), ';' );
  2257. // (#10123) Temp fix for dialog broken layout in latest webkit.
  2258. if ( CKEDITOR.env.webkit )
  2259. html.push( 'float:none;' );
  2260. html.push( '"' );
  2261. html.push( 'align="', CKEDITOR.tools.htmlEncode(
  2262. ( elementDefinition && elementDefinition.align ) || ( dialog.getParentEditor().lang.dir == 'ltr' ? 'left' : 'right' ) ), '" ' );
  2263. html.push( '><tbody>' );
  2264. for ( var i = 0; i < childHtmlList.length; i++ ) {
  2265. var styles = [];
  2266. html.push( '<tr><td role="presentation" ' );
  2267. if ( width )
  2268. styles.push( 'width:' + cssLength( width || '100%' ) );
  2269. if ( heights )
  2270. styles.push( 'height:' + cssLength( heights[ i ] ) );
  2271. else if ( elementDefinition && elementDefinition.expand )
  2272. styles.push( 'height:' + Math.floor( 100 / childHtmlList.length ) + '%' );
  2273. if ( elementDefinition && elementDefinition.padding !== undefined )
  2274. styles.push( 'padding:' + cssLength( elementDefinition.padding ) );
  2275. // In IE Quirks alignment has to be done on table cells. (#7324)
  2276. if ( CKEDITOR.env.ie && CKEDITOR.env.quirks && children[ i ].align )
  2277. styles.push( 'text-align:' + children[ i ].align );
  2278. if ( styles.length > 0 )
  2279. html.push( 'style="', styles.join( '; ' ), '" ' );
  2280. html.push( ' class="cke_dialog_ui_vbox_child">', childHtmlList[ i ], '</td></tr>' );
  2281. }
  2282. html.push( '</tbody></table>' );
  2283. return html.join( '' );
  2284. };
  2285. CKEDITOR.ui.dialog.uiElement.call( this, dialog, elementDefinition || { type: 'vbox' }, htmlList, 'div', null, { role: 'presentation' }, innerHTML );
  2286. }
  2287. };
  2288. } )();
  2289. /** @class CKEDITOR.ui.dialog.uiElement */
  2290. CKEDITOR.ui.dialog.uiElement.prototype = {
  2291. /**
  2292. * Gets the root DOM element of this dialog UI object.
  2293. *
  2294. * uiElement.getElement().hide();
  2295. *
  2296. * @returns {CKEDITOR.dom.element} Root DOM element of UI object.
  2297. */
  2298. getElement: function() {
  2299. return CKEDITOR.document.getById( this.domId );
  2300. },
  2301. /**
  2302. * Gets the DOM element that the user inputs values.
  2303. *
  2304. * This function is used by {@link #setValue}, {@link #getValue} and {@link #focus}. It should
  2305. * be overrided in child classes where the input element isn't the root
  2306. * element.
  2307. *
  2308. * var rawValue = textInput.getInputElement().$.value;
  2309. *
  2310. * @returns {CKEDITOR.dom.element} The element where the user input values.
  2311. */
  2312. getInputElement: function() {
  2313. return this.getElement();
  2314. },
  2315. /**
  2316. * Gets the parent dialog object containing this UI element.
  2317. *
  2318. * var dialog = uiElement.getDialog();
  2319. *
  2320. * @returns {CKEDITOR.dialog} Parent dialog object.
  2321. */
  2322. getDialog: function() {
  2323. return this._.dialog;
  2324. },
  2325. /**
  2326. * Sets the value of this dialog UI object.
  2327. *
  2328. * uiElement.setValue( 'Dingo' );
  2329. *
  2330. * @chainable
  2331. * @param {Object} value The new value.
  2332. * @param {Boolean} noChangeEvent Internal commit, to supress `change` event on this element.
  2333. */
  2334. setValue: function( value, noChangeEvent ) {
  2335. this.getInputElement().setValue( value );
  2336. !noChangeEvent && this.fire( 'change', { value: value } );
  2337. return this;
  2338. },
  2339. /**
  2340. * Gets the current value of this dialog UI object.
  2341. *
  2342. * var myValue = uiElement.getValue();
  2343. *
  2344. * @returns {Object} The current value.
  2345. */
  2346. getValue: function() {
  2347. return this.getInputElement().getValue();
  2348. },
  2349. /**
  2350. * Tells whether the UI object's value has changed.
  2351. *
  2352. * if ( uiElement.isChanged() )
  2353. * confirm( 'Value changed! Continue?' );
  2354. *
  2355. * @returns {Boolean} `true` if changed, `false` if not changed.
  2356. */
  2357. isChanged: function() {
  2358. // Override in input classes.
  2359. return false;
  2360. },
  2361. /**
  2362. * Selects the parent tab of this element. Usually called by focus() or overridden focus() methods.
  2363. *
  2364. * focus : function() {
  2365. * this.selectParentTab();
  2366. * // do something else.
  2367. * }
  2368. *
  2369. * @chainable
  2370. */
  2371. selectParentTab: function() {
  2372. var element = this.getInputElement(),
  2373. cursor = element,
  2374. tabId;
  2375. while ( ( cursor = cursor.getParent() ) && cursor.$.className.search( 'cke_dialog_page_contents' ) == -1 ) {
  2376. }
  2377. // Some widgets don't have parent tabs (e.g. OK and Cancel buttons).
  2378. if ( !cursor )
  2379. return this;
  2380. tabId = cursor.getAttribute( 'name' );
  2381. // Avoid duplicate select.
  2382. if ( this._.dialog._.currentTabId != tabId )
  2383. this._.dialog.selectPage( tabId );
  2384. return this;
  2385. },
  2386. /**
  2387. * Puts the focus to the UI object. Switches tabs if the UI object isn't in the active tab page.
  2388. *
  2389. * uiElement.focus();
  2390. *
  2391. * @chainable
  2392. */
  2393. focus: function() {
  2394. this.selectParentTab().getInputElement().focus();
  2395. return this;
  2396. },
  2397. /**
  2398. * Registers the `on*` event handlers defined in the element definition.
  2399. *
  2400. * The default behavior of this function is:
  2401. *
  2402. * 1. If the on* event is defined in the class's eventProcesors list,
  2403. * then the registration is delegated to the corresponding function
  2404. * in the eventProcessors list.
  2405. * 2. If the on* event is not defined in the eventProcessors list, then
  2406. * register the event handler under the corresponding DOM event of
  2407. * the UI element's input DOM element (as defined by the return value
  2408. * of {@link #getInputElement}).
  2409. *
  2410. * This function is only called at UI element instantiation, but can
  2411. * be overridded in child classes if they require more flexibility.
  2412. *
  2413. * @chainable
  2414. * @param {CKEDITOR.dialog.definition.uiElement} definition The UI element
  2415. * definition.
  2416. */
  2417. registerEvents: function( definition ) {
  2418. var regex = /^on([A-Z]\w+)/,
  2419. match;
  2420. var registerDomEvent = function( uiElement, dialog, eventName, func ) {
  2421. dialog.on( 'load', function() {
  2422. uiElement.getInputElement().on( eventName, func, uiElement );
  2423. } );
  2424. };
  2425. for ( var i in definition ) {
  2426. if ( !( match = i.match( regex ) ) )
  2427. continue;
  2428. if ( this.eventProcessors[ i ] )
  2429. this.eventProcessors[ i ].call( this, this._.dialog, definition[ i ] );
  2430. else
  2431. registerDomEvent( this, this._.dialog, match[ 1 ].toLowerCase(), definition[ i ] );
  2432. }
  2433. return this;
  2434. },
  2435. /**
  2436. * The event processor list used by
  2437. * {@link CKEDITOR.ui.dialog.uiElement#getInputElement} at UI element
  2438. * instantiation. The default list defines three `on*` events:
  2439. *
  2440. * 1. `onLoad` - Called when the element's parent dialog opens for the
  2441. * first time.
  2442. * 2. `onShow` - Called whenever the element's parent dialog opens.
  2443. * 3. `onHide` - Called whenever the element's parent dialog closes.
  2444. *
  2445. * // This connects the 'click' event in CKEDITOR.ui.dialog.button to onClick
  2446. * // handlers in the UI element's definitions.
  2447. * CKEDITOR.ui.dialog.button.eventProcessors = CKEDITOR.tools.extend( {},
  2448. * CKEDITOR.ui.dialog.uiElement.prototype.eventProcessors,
  2449. * { onClick : function( dialog, func ) { this.on( 'click', func ); } },
  2450. * true
  2451. * );
  2452. *
  2453. * @property {Object}
  2454. */
  2455. eventProcessors: {
  2456. onLoad: function( dialog, func ) {
  2457. dialog.on( 'load', func, this );
  2458. },
  2459. onShow: function( dialog, func ) {
  2460. dialog.on( 'show', func, this );
  2461. },
  2462. onHide: function( dialog, func ) {
  2463. dialog.on( 'hide', func, this );
  2464. }
  2465. },
  2466. /**
  2467. * The default handler for a UI element's access key down event, which
  2468. * tries to put focus to the UI element.
  2469. *
  2470. * Can be overridded in child classes for more sophisticaed behavior.
  2471. *
  2472. * @param {CKEDITOR.dialog} dialog The parent dialog object.
  2473. * @param {String} key The key combination pressed. Since access keys
  2474. * are defined to always include the `CTRL` key, its value should always
  2475. * include a `'CTRL+'` prefix.
  2476. */
  2477. accessKeyDown: function() {
  2478. this.focus();
  2479. },
  2480. /**
  2481. * The default handler for a UI element's access key up event, which
  2482. * does nothing.
  2483. *
  2484. * Can be overridded in child classes for more sophisticated behavior.
  2485. *
  2486. * @param {CKEDITOR.dialog} dialog The parent dialog object.
  2487. * @param {String} key The key combination pressed. Since access keys
  2488. * are defined to always include the `CTRL` key, its value should always
  2489. * include a `'CTRL+'` prefix.
  2490. */
  2491. accessKeyUp: function() {},
  2492. /**
  2493. * Disables a UI element.
  2494. */
  2495. disable: function() {
  2496. var element = this.getElement(),
  2497. input = this.getInputElement();
  2498. input.setAttribute( 'disabled', 'true' );
  2499. element.addClass( 'cke_disabled' );
  2500. },
  2501. /**
  2502. * Enables a UI element.
  2503. */
  2504. enable: function() {
  2505. var element = this.getElement(),
  2506. input = this.getInputElement();
  2507. input.removeAttribute( 'disabled' );
  2508. element.removeClass( 'cke_disabled' );
  2509. },
  2510. /**
  2511. * Determines whether an UI element is enabled or not.
  2512. *
  2513. * @returns {Boolean} Whether the UI element is enabled.
  2514. */
  2515. isEnabled: function() {
  2516. return !this.getElement().hasClass( 'cke_disabled' );
  2517. },
  2518. /**
  2519. * Determines whether an UI element is visible or not.
  2520. *
  2521. * @returns {Boolean} Whether the UI element is visible.
  2522. */
  2523. isVisible: function() {
  2524. return this.getInputElement().isVisible();
  2525. },
  2526. /**
  2527. * Determines whether an UI element is focus-able or not.
  2528. * Focus-able is defined as being both visible and enabled.
  2529. *
  2530. * @returns {Boolean} Whether the UI element can be focused.
  2531. */
  2532. isFocusable: function() {
  2533. if ( !this.isEnabled() || !this.isVisible() )
  2534. return false;
  2535. return true;
  2536. }
  2537. };
  2538. /** @class CKEDITOR.ui.dialog.hbox */
  2539. CKEDITOR.ui.dialog.hbox.prototype = CKEDITOR.tools.extend( new CKEDITOR.ui.dialog.uiElement(), {
  2540. /**
  2541. * Gets a child UI element inside this container.
  2542. *
  2543. * var checkbox = hbox.getChild( [0,1] );
  2544. * checkbox.setValue( true );
  2545. *
  2546. * @param {Array/Number} indices An array or a single number to indicate the child's
  2547. * position in the container's descendant tree. Omit to get all the children in an array.
  2548. * @returns {Array/CKEDITOR.ui.dialog.uiElement} Array of all UI elements in the container
  2549. * if no argument given, or the specified UI element if indices is given.
  2550. */
  2551. getChild: function( indices ) {
  2552. // If no arguments, return a clone of the children array.
  2553. if ( arguments.length < 1 )
  2554. return this._.children.concat();
  2555. // If indices isn't array, make it one.
  2556. if ( !indices.splice )
  2557. indices = [ indices ];
  2558. // Retrieve the child element according to tree position.
  2559. if ( indices.length < 2 )
  2560. return this._.children[ indices[ 0 ] ];
  2561. else
  2562. return ( this._.children[ indices[ 0 ] ] && this._.children[ indices[ 0 ] ].getChild ) ? this._.children[ indices[ 0 ] ].getChild( indices.slice( 1, indices.length ) ) : null;
  2563. }
  2564. }, true );
  2565. CKEDITOR.ui.dialog.vbox.prototype = new CKEDITOR.ui.dialog.hbox();
  2566. ( function() {
  2567. var commonBuilder = {
  2568. build: function( dialog, elementDefinition, output ) {
  2569. var children = elementDefinition.children,
  2570. child,
  2571. childHtmlList = [],
  2572. childObjList = [];
  2573. for ( var i = 0;
  2574. ( i < children.length && ( child = children[ i ] ) ); i++ ) {
  2575. var childHtml = [];
  2576. childHtmlList.push( childHtml );
  2577. childObjList.push( CKEDITOR.dialog._.uiElementBuilders[ child.type ].build( dialog, child, childHtml ) );
  2578. }
  2579. return new CKEDITOR.ui.dialog[ elementDefinition.type ]( dialog, childObjList, childHtmlList, output, elementDefinition );
  2580. }
  2581. };
  2582. CKEDITOR.dialog.addUIElement( 'hbox', commonBuilder );
  2583. CKEDITOR.dialog.addUIElement( 'vbox', commonBuilder );
  2584. } )();
  2585. /**
  2586. * Generic dialog command. It opens a specific dialog when executed.
  2587. *
  2588. * // Register the "link" command, which opens the "link" dialog.
  2589. * editor.addCommand( 'link', new CKEDITOR.dialogCommand( 'link' ) );
  2590. *
  2591. * @class
  2592. * @constructor Creates a dialogCommand class instance.
  2593. * @extends CKEDITOR.commandDefinition
  2594. * @param {String} dialogName The name of the dialog to open when executing
  2595. * this command.
  2596. * @param {Object} [ext] Additional command definition's properties.
  2597. */
  2598. CKEDITOR.dialogCommand = function( dialogName, ext ) {
  2599. this.dialogName = dialogName;
  2600. CKEDITOR.tools.extend( this, ext, true );
  2601. };
  2602. CKEDITOR.dialogCommand.prototype = {
  2603. exec: function( editor ) {
  2604. editor.openDialog( this.dialogName );
  2605. },
  2606. // Dialog commands just open a dialog ui, thus require no undo logic,
  2607. // undo support should dedicate to specific dialog implementation.
  2608. canUndo: false,
  2609. editorFocus: 1
  2610. };
  2611. ( function() {
  2612. var notEmptyRegex = /^([a]|[^a])+$/,
  2613. integerRegex = /^\d*$/,
  2614. numberRegex = /^\d*(?:\.\d+)?$/,
  2615. htmlLengthRegex = /^(((\d*(\.\d+))|(\d*))(px|\%)?)?$/,
  2616. cssLengthRegex = /^(((\d*(\.\d+))|(\d*))(px|em|ex|in|cm|mm|pt|pc|\%)?)?$/i,
  2617. inlineStyleRegex = /^(\s*[\w-]+\s*:\s*[^:;]+(?:;|$))*$/;
  2618. CKEDITOR.VALIDATE_OR = 1;
  2619. CKEDITOR.VALIDATE_AND = 2;
  2620. CKEDITOR.dialog.validate = {
  2621. functions: function() {
  2622. var args = arguments;
  2623. return function() {
  2624. /**
  2625. * It's important for validate functions to be able to accept the value
  2626. * as argument in addition to this.getValue(), so that it is possible to
  2627. * combine validate functions together to make more sophisticated
  2628. * validators.
  2629. */
  2630. var value = this && this.getValue ? this.getValue() : args[ 0 ];
  2631. var msg,
  2632. relation = CKEDITOR.VALIDATE_AND,
  2633. functions = [],
  2634. i;
  2635. for ( i = 0; i < args.length; i++ ) {
  2636. if ( typeof args[ i ] == 'function' )
  2637. functions.push( args[ i ] );
  2638. else
  2639. break;
  2640. }
  2641. if ( i < args.length && typeof args[ i ] == 'string' ) {
  2642. msg = args[ i ];
  2643. i++;
  2644. }
  2645. if ( i < args.length && typeof args[ i ] == 'number' )
  2646. relation = args[ i ];
  2647. var passed = ( relation == CKEDITOR.VALIDATE_AND ? true : false );
  2648. for ( i = 0; i < functions.length; i++ ) {
  2649. if ( relation == CKEDITOR.VALIDATE_AND )
  2650. passed = passed && functions[ i ]( value );
  2651. else
  2652. passed = passed || functions[ i ]( value );
  2653. }
  2654. return !passed ? msg : true;
  2655. };
  2656. },
  2657. regex: function( regex, msg ) {
  2658. /*
  2659. * Can be greatly shortened by deriving from functions validator if code size
  2660. * turns out to be more important than performance.
  2661. */
  2662. return function() {
  2663. var value = this && this.getValue ? this.getValue() : arguments[ 0 ];
  2664. return !regex.test( value ) ? msg : true;
  2665. };
  2666. },
  2667. notEmpty: function( msg ) {
  2668. return this.regex( notEmptyRegex, msg );
  2669. },
  2670. integer: function( msg ) {
  2671. return this.regex( integerRegex, msg );
  2672. },
  2673. 'number': function( msg ) {
  2674. return this.regex( numberRegex, msg );
  2675. },
  2676. 'cssLength': function( msg ) {
  2677. return this.functions( function( val ) {
  2678. return cssLengthRegex.test( CKEDITOR.tools.trim( val ) );
  2679. }, msg );
  2680. },
  2681. 'htmlLength': function( msg ) {
  2682. return this.functions( function( val ) {
  2683. return htmlLengthRegex.test( CKEDITOR.tools.trim( val ) );
  2684. }, msg );
  2685. },
  2686. 'inlineStyle': function( msg ) {
  2687. return this.functions( function( val ) {
  2688. return inlineStyleRegex.test( CKEDITOR.tools.trim( val ) );
  2689. }, msg );
  2690. },
  2691. equals: function( value, msg ) {
  2692. return this.functions( function( val ) {
  2693. return val == value;
  2694. }, msg );
  2695. },
  2696. notEqual: function( value, msg ) {
  2697. return this.functions( function( val ) {
  2698. return val != value;
  2699. }, msg );
  2700. }
  2701. };
  2702. CKEDITOR.on( 'instanceDestroyed', function( evt ) {
  2703. // Remove dialog cover on last instance destroy.
  2704. if ( CKEDITOR.tools.isEmpty( CKEDITOR.instances ) ) {
  2705. var currentTopDialog;
  2706. while ( ( currentTopDialog = CKEDITOR.dialog._.currentTop ) )
  2707. currentTopDialog.hide();
  2708. removeCovers();
  2709. }
  2710. var dialogs = evt.editor._.storedDialogs;
  2711. for ( var name in dialogs )
  2712. dialogs[ name ].destroy();
  2713. } );
  2714. } )();
  2715. // Extend the CKEDITOR.editor class with dialog specific functions.
  2716. CKEDITOR.tools.extend( CKEDITOR.editor.prototype, {
  2717. /**
  2718. * Loads and opens a registered dialog.
  2719. *
  2720. * CKEDITOR.instances.editor1.openDialog( 'smiley' );
  2721. *
  2722. * @member CKEDITOR.editor
  2723. * @param {String} dialogName The registered name of the dialog.
  2724. * @param {Function} callback The function to be invoked after dialog instance created.
  2725. * @returns {CKEDITOR.dialog} The dialog object corresponding to the dialog displayed.
  2726. * `null` if the dialog name is not registered.
  2727. * @see CKEDITOR.dialog#add
  2728. */
  2729. openDialog: function( dialogName, callback ) {
  2730. var dialog = null, dialogDefinitions = CKEDITOR.dialog._.dialogDefinitions[ dialogName ];
  2731. if ( CKEDITOR.dialog._.currentTop === null )
  2732. showCover( this );
  2733. // If the dialogDefinition is already loaded, open it immediately.
  2734. if ( typeof dialogDefinitions == 'function' ) {
  2735. var storedDialogs = this._.storedDialogs || ( this._.storedDialogs = {} );
  2736. dialog = storedDialogs[ dialogName ] || ( storedDialogs[ dialogName ] = new CKEDITOR.dialog( this, dialogName ) );
  2737. callback && callback.call( dialog, dialog );
  2738. dialog.show();
  2739. } else if ( dialogDefinitions == 'failed' ) {
  2740. hideCover( this );
  2741. throw new Error( '[CKEDITOR.dialog.openDialog] Dialog "' + dialogName + '" failed when loading definition.' );
  2742. } else if ( typeof dialogDefinitions == 'string' ) {
  2743. CKEDITOR.scriptLoader.load( CKEDITOR.getUrl( dialogDefinitions ),
  2744. function() {
  2745. var dialogDefinition = CKEDITOR.dialog._.dialogDefinitions[ dialogName ];
  2746. // In case of plugin error, mark it as loading failed.
  2747. if ( typeof dialogDefinition != 'function' )
  2748. CKEDITOR.dialog._.dialogDefinitions[ dialogName ] = 'failed';
  2749. this.openDialog( dialogName, callback );
  2750. }, this, 0, 1 );
  2751. }
  2752. CKEDITOR.skin.loadPart( 'dialog' );
  2753. return dialog;
  2754. }
  2755. } );
  2756. } )();
  2757. CKEDITOR.plugins.add( 'dialog', {
  2758. requires: 'dialogui',
  2759. init: function( editor ) {
  2760. editor.on( 'doubleclick', function( evt ) {
  2761. if ( evt.data.dialog )
  2762. editor.openDialog( evt.data.dialog );
  2763. }, null, null, 999 );
  2764. }
  2765. } );
  2766. // Dialog related configurations.
  2767. /**
  2768. * The color of the dialog background cover. It should be a valid CSS color string.
  2769. *
  2770. * config.dialog_backgroundCoverColor = 'rgb(255, 254, 253)';
  2771. *
  2772. * @cfg {String} [dialog_backgroundCoverColor='white']
  2773. * @member CKEDITOR.config
  2774. */
  2775. /**
  2776. * The opacity of the dialog background cover. It should be a number within the
  2777. * range `[0.0, 1.0]`.
  2778. *
  2779. * config.dialog_backgroundCoverOpacity = 0.7;
  2780. *
  2781. * @cfg {Number} [dialog_backgroundCoverOpacity=0.5]
  2782. * @member CKEDITOR.config
  2783. */
  2784. /**
  2785. * If the dialog has more than one tab, put focus into the first tab as soon as dialog is opened.
  2786. *
  2787. * config.dialog_startupFocusTab = true;
  2788. *
  2789. * @cfg {Boolean} [dialog_startupFocusTab=false]
  2790. * @member CKEDITOR.config
  2791. */
  2792. /**
  2793. * The distance of magnetic borders used in moving and resizing dialogs,
  2794. * measured in pixels.
  2795. *
  2796. * config.dialog_magnetDistance = 30;
  2797. *
  2798. * @cfg {Number} [dialog_magnetDistance=20]
  2799. * @member CKEDITOR.config
  2800. */
  2801. /**
  2802. * The guideline to follow when generating the dialog buttons. There are 3 possible options:
  2803. *
  2804. * * `'OS'` - the buttons will be displayed in the default order of the user's OS;
  2805. * * `'ltr'` - for Left-To-Right order;
  2806. * * `'rtl'` - for Right-To-Left order.
  2807. *
  2808. * Example:
  2809. *
  2810. * config.dialog_buttonsOrder = 'rtl';
  2811. *
  2812. * @since 3.5
  2813. * @cfg {String} [dialog_buttonsOrder='OS']
  2814. * @member CKEDITOR.config
  2815. */
  2816. /**
  2817. * The dialog contents to removed. It's a string composed by dialog name and tab name with a colon between them.
  2818. *
  2819. * Separate each pair with semicolon (see example).
  2820. *
  2821. * **Note:** All names are case-sensitive.
  2822. *
  2823. * **Note:** Be cautious when specifying dialog tabs that are mandatory,
  2824. * like `'info'`, dialog functionality might be broken because of this!
  2825. *
  2826. * config.removeDialogTabs = 'flash:advanced;image:Link';
  2827. *
  2828. * @since 3.5
  2829. * @cfg {String} [removeDialogTabs='']
  2830. * @member CKEDITOR.config
  2831. */
  2832. /**
  2833. * Tells if user should not be asked to confirm close, if any dialog field was modified.
  2834. * By default it is set to `false` meaning that the confirmation dialog will be shown.
  2835. *
  2836. * config.dialog_noConfirmCancel = true;
  2837. *
  2838. * @since 4.3
  2839. * @cfg {Boolean} [dialog_noConfirmCancel=false]
  2840. * @member CKEDITOR.config
  2841. */
  2842. /**
  2843. * Event fired when a dialog definition is about to be used to create a dialog into
  2844. * an editor instance. This event makes it possible to customize the definition
  2845. * before creating it.
  2846. *
  2847. * Note that this event is called only the first time a specific dialog is
  2848. * opened. Successive openings will use the cached dialog, and this event will
  2849. * not get fired.
  2850. *
  2851. * @event dialogDefinition
  2852. * @member CKEDITOR
  2853. * @param {CKEDITOR.dialog.definition} data The dialog defination that
  2854. * is being loaded.
  2855. * @param {CKEDITOR.editor} editor The editor instance that will use the dialog.
  2856. */
  2857. /**
  2858. * Event fired when a tab is going to be selected in a dialog.
  2859. *
  2860. * @event selectPage
  2861. * @member CKEDITOR.dialog
  2862. * @param data
  2863. * @param {String} data.page The id of the page that it's gonna be selected.
  2864. * @param {String} data.currentPage The id of the current page.
  2865. */
  2866. /**
  2867. * Event fired when the user tries to dismiss a dialog.
  2868. *
  2869. * @event cancel
  2870. * @member CKEDITOR.dialog
  2871. * @param data
  2872. * @param {Boolean} data.hide Whether the event should proceed or not.
  2873. */
  2874. /**
  2875. * Event fired when the user tries to confirm a dialog.
  2876. *
  2877. * @event ok
  2878. * @member CKEDITOR.dialog
  2879. * @param data
  2880. * @param {Boolean} data.hide Whether the event should proceed or not.
  2881. */
  2882. /**
  2883. * Event fired when a dialog is shown.
  2884. *
  2885. * @event show
  2886. * @member CKEDITOR.dialog
  2887. */
  2888. /**
  2889. * Event fired when a dialog is shown.
  2890. *
  2891. * @event dialogShow
  2892. * @member CKEDITOR.editor
  2893. * @param {CKEDITOR.editor} editor This editor instance.
  2894. * @param {CKEDITOR.dialog} data The opened dialog instance.
  2895. */
  2896. /**
  2897. * Event fired when a dialog is hidden.
  2898. *
  2899. * @event hide
  2900. * @member CKEDITOR.dialog
  2901. */
  2902. /**
  2903. * Event fired when a dialog is hidden.
  2904. *
  2905. * @event dialogHide
  2906. * @member CKEDITOR.editor
  2907. * @param {CKEDITOR.editor} editor This editor instance.
  2908. * @param {CKEDITOR.dialog} data The hidden dialog instance.
  2909. */
  2910. /**
  2911. * Event fired when a dialog is being resized. The event is fired on
  2912. * both the {@link CKEDITOR.dialog} object and the dialog instance
  2913. * since 3.5.3, previously it was only available in the global object.
  2914. *
  2915. * @static
  2916. * @event resize
  2917. * @member CKEDITOR.dialog
  2918. * @param data
  2919. * @param {CKEDITOR.dialog} data.dialog The dialog being resized (if
  2920. * it is fired on the dialog itself, this parameter is not sent).
  2921. * @param {String} data.skin The skin name.
  2922. * @param {Number} data.width The new width.
  2923. * @param {Number} data.height The new height.
  2924. */
  2925. /**
  2926. * Event fired when a dialog is being resized. The event is fired on
  2927. * both the {@link CKEDITOR.dialog} object and the dialog instance
  2928. * since 3.5.3, previously it was only available in the global object.
  2929. *
  2930. * @since 3.5
  2931. * @event resize
  2932. * @member CKEDITOR.dialog
  2933. * @param data
  2934. * @param {Number} data.width The new width.
  2935. * @param {Number} data.height The new height.
  2936. */
  2937. /**
  2938. * Event fired when the dialog state changes, usually by {@link CKEDITOR.dialog#setState}.
  2939. *
  2940. * @since 4.5
  2941. * @event state
  2942. * @member CKEDITOR.dialog
  2943. * @param data
  2944. * @param {Number} data The new state. Either {@link CKEDITOR#DIALOG_STATE_IDLE} or {@link CKEDITOR#DIALOG_STATE_BUSY}.
  2945. */