PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/files/ckeditor/4.3.0/plugins/docprops/dialogs/docprops.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 608 lines | 582 code | 19 blank | 7 comment | 63 complexity | 7f5d3675e08290ef53b4829d2e0aa0ea MD5 | raw file
  1. /**
  2. * @license Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. CKEDITOR.dialog.add( 'docProps', function( editor ) {
  6. var lang = editor.lang.docprops,
  7. langCommon = editor.lang.common,
  8. metaHash = {};
  9. function getDialogValue( dialogName, callback ) {
  10. var onOk = function() {
  11. releaseHandlers( this );
  12. callback( this, this._.parentDialog );
  13. };
  14. var releaseHandlers = function( dialog ) {
  15. dialog.removeListener( 'ok', onOk );
  16. dialog.removeListener( 'cancel', releaseHandlers );
  17. };
  18. var bindToDialog = function( dialog ) {
  19. dialog.on( 'ok', onOk );
  20. dialog.on( 'cancel', releaseHandlers );
  21. };
  22. editor.execCommand( dialogName );
  23. if ( editor._.storedDialogs.colordialog )
  24. bindToDialog( editor._.storedDialogs.colordialog );
  25. else {
  26. CKEDITOR.on( 'dialogDefinition', function( e ) {
  27. if ( e.data.name != dialogName )
  28. return;
  29. var definition = e.data.definition;
  30. e.removeListener();
  31. definition.onLoad = CKEDITOR.tools.override( definition.onLoad, function( orginal ) {
  32. return function() {
  33. bindToDialog( this );
  34. definition.onLoad = orginal;
  35. if ( typeof orginal == 'function' )
  36. orginal.call( this );
  37. };
  38. });
  39. });
  40. }
  41. }
  42. function handleOther() {
  43. var dialog = this.getDialog(),
  44. other = dialog.getContentElement( 'general', this.id + 'Other' );
  45. if ( !other )
  46. return;
  47. if ( this.getValue() == 'other' ) {
  48. other.getInputElement().removeAttribute( 'readOnly' );
  49. other.focus();
  50. other.getElement().removeClass( 'cke_disabled' );
  51. } else {
  52. other.getInputElement().setAttribute( 'readOnly', true );
  53. other.getElement().addClass( 'cke_disabled' );
  54. }
  55. }
  56. function commitMeta( name, isHttp, value ) {
  57. return function( doc, html, head ) {
  58. var hash = metaHash,
  59. val = typeof value != 'undefined' ? value : this.getValue();
  60. if ( !val && ( name in hash ) )
  61. hash[ name ].remove();
  62. else if ( val && ( name in hash ) )
  63. hash[ name ].setAttribute( 'content', val );
  64. else if ( val ) {
  65. var meta = new CKEDITOR.dom.element( 'meta', editor.document );
  66. meta.setAttribute( isHttp ? 'http-equiv' : 'name', name );
  67. meta.setAttribute( 'content', val );
  68. head.append( meta );
  69. }
  70. };
  71. }
  72. function setupMeta( name, ret ) {
  73. return function() {
  74. var hash = metaHash,
  75. result = ( name in hash ) ? hash[ name ].getAttribute( 'content' ) || '' : '';
  76. if ( ret )
  77. return result;
  78. this.setValue( result );
  79. return null;
  80. };
  81. }
  82. function commitMargin( name ) {
  83. return function( doc, html, head, body ) {
  84. body.removeAttribute( 'margin' + name );
  85. var val = this.getValue();
  86. if ( val !== '' )
  87. body.setStyle( 'margin-' + name, CKEDITOR.tools.cssLength( val ) );
  88. else
  89. body.removeStyle( 'margin-' + name );
  90. };
  91. }
  92. function createMetaHash( doc ) {
  93. var hash = {},
  94. metas = doc.getElementsByTag( 'meta' ),
  95. count = metas.count();
  96. for ( var i = 0; i < count; i++ ) {
  97. var meta = metas.getItem( i );
  98. hash[ meta.getAttribute( meta.hasAttribute( 'http-equiv' ) ? 'http-equiv' : 'name' ).toLowerCase() ] = meta;
  99. }
  100. return hash;
  101. }
  102. // We cannot just remove the style from the element, as it might be affected from non-inline stylesheets.
  103. // To get the proper result, we should manually set the inline style to its default value.
  104. function resetStyle( element, prop, resetVal ) {
  105. element.removeStyle( prop );
  106. if ( element.getComputedStyle( prop ) != resetVal )
  107. element.setStyle( prop, resetVal );
  108. }
  109. // Utilty to shorten the creation of color fields in the dialog.
  110. var colorField = function( id, label, fieldProps ) {
  111. return {
  112. type: 'hbox',
  113. padding: 0,
  114. widths: [ '60%', '40%' ],
  115. children: [
  116. CKEDITOR.tools.extend({
  117. type: 'text',
  118. id: id,
  119. label: lang[ label ]
  120. }, fieldProps || {}, 1 ),
  121. {
  122. type: 'button',
  123. id: id + 'Choose',
  124. label: lang.chooseColor,
  125. className: 'colorChooser',
  126. onClick: function() {
  127. var self = this;
  128. getDialogValue( 'colordialog', function( colorDialog ) {
  129. var dialog = self.getDialog();
  130. dialog.getContentElement( dialog._.currentTabId, id ).setValue( colorDialog.getContentElement( 'picker', 'selectedColor' ).getValue() );
  131. });
  132. }
  133. }
  134. ]
  135. };
  136. };
  137. var previewSrc = 'javascript:' +
  138. 'void((function(){' + encodeURIComponent(
  139. 'document.open();' +
  140. ( CKEDITOR.env.ie ? '(' + CKEDITOR.tools.fixDomain + ')();' : '' ) +
  141. 'document.write( \'<html style="background-color: #ffffff; height: 100%"><head></head><body style="width: 100%; height: 100%; margin: 0px">' + lang.previewHtml + '</body></html>\' );' +
  142. 'document.close();'
  143. ) + '})())';
  144. return {
  145. title: lang.title,
  146. minHeight: 330,
  147. minWidth: 500,
  148. onShow: function() {
  149. var doc = editor.document,
  150. html = doc.getElementsByTag( 'html' ).getItem( 0 ),
  151. head = doc.getHead(),
  152. body = doc.getBody();
  153. metaHash = createMetaHash( doc );
  154. this.setupContent( doc, html, head, body );
  155. },
  156. onHide: function() {
  157. metaHash = {};
  158. },
  159. onOk: function() {
  160. var doc = editor.document,
  161. html = doc.getElementsByTag( 'html' ).getItem( 0 ),
  162. head = doc.getHead(),
  163. body = doc.getBody();
  164. this.commitContent( doc, html, head, body );
  165. },
  166. contents: [
  167. {
  168. id: 'general',
  169. label: langCommon.generalTab,
  170. elements: [
  171. {
  172. type: 'text',
  173. id: 'title',
  174. label: lang.docTitle,
  175. setup: function( doc ) {
  176. this.setValue( doc.getElementsByTag( 'title' ).getItem( 0 ).data( 'cke-title' ) );
  177. },
  178. commit: function( doc, html, head, body, isPreview ) {
  179. if ( isPreview )
  180. return;
  181. doc.getElementsByTag( 'title' ).getItem( 0 ).data( 'cke-title', this.getValue() );
  182. }
  183. },
  184. {
  185. type: 'hbox',
  186. children: [
  187. {
  188. type: 'select',
  189. id: 'dir',
  190. label: langCommon.langDir,
  191. style: 'width: 100%',
  192. items: [
  193. [ langCommon.notSet, '' ],
  194. [ langCommon.langDirLtr, 'ltr' ],
  195. [ langCommon.langDirRtl, 'rtl' ]
  196. ],
  197. setup: function( doc, html, head, body ) {
  198. this.setValue( body.getDirection() || '' );
  199. },
  200. commit: function( doc, html, head, body ) {
  201. var val = this.getValue();
  202. if ( val )
  203. body.setAttribute( 'dir', val );
  204. else
  205. body.removeAttribute( 'dir' );
  206. body.removeStyle( 'direction' );
  207. }
  208. },
  209. {
  210. type: 'text',
  211. id: 'langCode',
  212. label: langCommon.langCode,
  213. setup: function( doc, html ) {
  214. this.setValue( html.getAttribute( 'xml:lang' ) || html.getAttribute( 'lang' ) || '' );
  215. },
  216. commit: function( doc, html, head, body, isPreview ) {
  217. if ( isPreview )
  218. return;
  219. var val = this.getValue();
  220. if ( val )
  221. html.setAttributes({ 'xml:lang': val, lang: val } );
  222. else
  223. html.removeAttributes( { 'xml:lang':1,lang:1 } );
  224. }
  225. }
  226. ]
  227. },
  228. {
  229. type: 'hbox',
  230. children: [
  231. {
  232. type: 'select',
  233. id: 'charset',
  234. label: lang.charset,
  235. style: 'width: 100%',
  236. items: [
  237. [ langCommon.notSet, '' ],
  238. [ lang.charsetASCII, 'us-ascii' ],
  239. [ lang.charsetCE, 'iso-8859-2' ],
  240. [ lang.charsetCT, 'big5' ],
  241. [ lang.charsetCR, 'iso-8859-5' ],
  242. [ lang.charsetGR, 'iso-8859-7' ],
  243. [ lang.charsetJP, 'iso-2022-jp' ],
  244. [ lang.charsetKR, 'iso-2022-kr' ],
  245. [ lang.charsetTR, 'iso-8859-9' ],
  246. [ lang.charsetUN, 'utf-8' ],
  247. [ lang.charsetWE, 'iso-8859-1' ],
  248. [ lang.other, 'other' ]
  249. ],
  250. 'default': '',
  251. onChange: function() {
  252. this.getDialog().selectedCharset = this.getValue() != 'other' ? this.getValue() : '';
  253. handleOther.call( this );
  254. },
  255. setup: function() {
  256. this.metaCharset = ( 'charset' in metaHash );
  257. var func = setupMeta( this.metaCharset ? 'charset' : 'content-type', 1, 1 ),
  258. val = func.call( this );
  259. !this.metaCharset && val.match( /charset=[^=]+$/ ) && ( val = val.substring( val.indexOf( '=' ) + 1 ) );
  260. if ( val ) {
  261. this.setValue( val.toLowerCase() );
  262. if ( !this.getValue() ) {
  263. this.setValue( 'other' );
  264. var other = this.getDialog().getContentElement( 'general', 'charsetOther' );
  265. other && other.setValue( val );
  266. }
  267. this.getDialog().selectedCharset = val;
  268. }
  269. handleOther.call( this );
  270. },
  271. commit: function( doc, html, head, body, isPreview ) {
  272. if ( isPreview )
  273. return;
  274. var value = this.getValue(),
  275. other = this.getDialog().getContentElement( 'general', 'charsetOther' );
  276. value == 'other' && ( value = other ? other.getValue() : '' );
  277. value && !this.metaCharset && ( value = ( metaHash[ 'content-type' ] ? metaHash[ 'content-type' ].getAttribute( 'content' ).split( ';' )[ 0 ] : 'text/html' ) + '; charset=' + value );
  278. var func = commitMeta( this.metaCharset ? 'charset' : 'content-type', 1, value );
  279. func.call( this, doc, html, head );
  280. }
  281. },
  282. {
  283. type: 'text',
  284. id: 'charsetOther',
  285. label: lang.charsetOther,
  286. onChange: function() {
  287. this.getDialog().selectedCharset = this.getValue();
  288. }
  289. }
  290. ]
  291. },
  292. {
  293. type: 'hbox',
  294. children: [
  295. {
  296. type: 'select',
  297. id: 'docType',
  298. label: lang.docType,
  299. style: 'width: 100%',
  300. items: [
  301. [ langCommon.notSet, '' ],
  302. [ 'XHTML 1.1', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' ],
  303. [ 'XHTML 1.0 Transitional', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' ],
  304. [ 'XHTML 1.0 Strict', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' ],
  305. [ 'XHTML 1.0 Frameset', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">' ],
  306. [ 'HTML 5', '<!DOCTYPE html>' ],
  307. [ 'HTML 4.01 Transitional', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' ],
  308. [ 'HTML 4.01 Strict', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' ],
  309. [ 'HTML 4.01 Frameset', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">' ],
  310. [ 'HTML 3.2', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">' ],
  311. [ 'HTML 2.0', '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">' ],
  312. [ lang.other, 'other' ]
  313. ],
  314. onChange: handleOther,
  315. setup: function() {
  316. if ( editor.docType ) {
  317. this.setValue( editor.docType );
  318. if ( !this.getValue() ) {
  319. this.setValue( 'other' );
  320. var other = this.getDialog().getContentElement( 'general', 'docTypeOther' );
  321. other && other.setValue( editor.docType );
  322. }
  323. }
  324. handleOther.call( this );
  325. },
  326. commit: function( doc, html, head, body, isPreview ) {
  327. if ( isPreview )
  328. return;
  329. var value = this.getValue(),
  330. other = this.getDialog().getContentElement( 'general', 'docTypeOther' );
  331. editor.docType = value == 'other' ? ( other ? other.getValue() : '' ) : value;
  332. }
  333. },
  334. {
  335. type: 'text',
  336. id: 'docTypeOther',
  337. label: lang.docTypeOther
  338. }
  339. ]
  340. },
  341. {
  342. type: 'checkbox',
  343. id: 'xhtmlDec',
  344. label: lang.xhtmlDec,
  345. setup: function() {
  346. this.setValue( !!editor.xmlDeclaration );
  347. },
  348. commit: function( doc, html, head, body, isPreview ) {
  349. if ( isPreview )
  350. return;
  351. if ( this.getValue() ) {
  352. editor.xmlDeclaration = '<?xml version="1.0" encoding="' + ( this.getDialog().selectedCharset || 'utf-8' ) + '"?>';
  353. html.setAttribute( 'xmlns', 'http://www.w3.org/1999/xhtml' );
  354. } else {
  355. editor.xmlDeclaration = '';
  356. html.removeAttribute( 'xmlns' );
  357. }
  358. }
  359. }
  360. ]
  361. },
  362. {
  363. id: 'design',
  364. label: lang.design,
  365. elements: [
  366. {
  367. type: 'hbox',
  368. widths: [ '60%', '40%' ],
  369. children: [
  370. {
  371. type: 'vbox',
  372. children: [
  373. colorField( 'txtColor', 'txtColor', {
  374. setup: function( doc, html, head, body ) {
  375. this.setValue( body.getComputedStyle( 'color' ) );
  376. },
  377. commit: function( doc, html, head, body, isPreview ) {
  378. if ( this.isChanged() || isPreview ) {
  379. body.removeAttribute( 'text' );
  380. var val = this.getValue();
  381. if ( val )
  382. body.setStyle( 'color', val );
  383. else
  384. body.removeStyle( 'color' );
  385. }
  386. }
  387. }),
  388. colorField( 'bgColor', 'bgColor', {
  389. setup: function( doc, html, head, body ) {
  390. var val = body.getComputedStyle( 'background-color' ) || '';
  391. this.setValue( val == 'transparent' ? '' : val );
  392. },
  393. commit: function( doc, html, head, body, isPreview ) {
  394. if ( this.isChanged() || isPreview ) {
  395. body.removeAttribute( 'bgcolor' );
  396. var val = this.getValue();
  397. if ( val )
  398. body.setStyle( 'background-color', val );
  399. else
  400. resetStyle( body, 'background-color', 'transparent' );
  401. }
  402. }
  403. }),
  404. {
  405. type: 'hbox',
  406. widths: [ '60%', '40%' ],
  407. padding: 1,
  408. children: [
  409. {
  410. type: 'text',
  411. id: 'bgImage',
  412. label: lang.bgImage,
  413. setup: function( doc, html, head, body ) {
  414. var val = body.getComputedStyle( 'background-image' ) || '';
  415. if ( val == 'none' )
  416. val = '';
  417. else {
  418. val = val.replace( /url\(\s*(["']?)\s*([^\)]*)\s*\1\s*\)/i, function( match, quote, url ) {
  419. return url;
  420. });
  421. }
  422. this.setValue( val );
  423. },
  424. commit: function( doc, html, head, body ) {
  425. body.removeAttribute( 'background' );
  426. var val = this.getValue();
  427. if ( val )
  428. body.setStyle( 'background-image', 'url(' + val + ')' );
  429. else
  430. resetStyle( body, 'background-image', 'none' );
  431. }
  432. },
  433. {
  434. type: 'button',
  435. id: 'bgImageChoose',
  436. label: langCommon.browseServer,
  437. style: 'display:inline-block;margin-top:10px;',
  438. hidden: true,
  439. filebrowser: 'design:bgImage'
  440. }
  441. ]
  442. },
  443. {
  444. type: 'checkbox',
  445. id: 'bgFixed',
  446. label: lang.bgFixed,
  447. setup: function( doc, html, head, body ) {
  448. this.setValue( body.getComputedStyle( 'background-attachment' ) == 'fixed' );
  449. },
  450. commit: function( doc, html, head, body ) {
  451. if ( this.getValue() )
  452. body.setStyle( 'background-attachment', 'fixed' );
  453. else
  454. resetStyle( body, 'background-attachment', 'scroll' );
  455. }
  456. }
  457. ]
  458. },
  459. {
  460. type: 'vbox',
  461. children: [
  462. {
  463. type: 'html',
  464. id: 'marginTitle',
  465. html: '<div style="text-align: center; margin: 0px auto; font-weight: bold">' + lang.margin + '</div>'
  466. },
  467. {
  468. type: 'text',
  469. id: 'marginTop',
  470. label: lang.marginTop,
  471. style: 'width: 80px; text-align: center',
  472. align: 'center',
  473. inputStyle: 'text-align: center',
  474. setup: function( doc, html, head, body ) {
  475. this.setValue( body.getStyle( 'margin-top' ) || body.getAttribute( 'margintop' ) || '' );
  476. },
  477. commit: commitMargin( 'top' )
  478. },
  479. {
  480. type: 'hbox',
  481. children: [
  482. {
  483. type: 'text',
  484. id: 'marginLeft',
  485. label: lang.marginLeft,
  486. style: 'width: 80px; text-align: center',
  487. align: 'center',
  488. inputStyle: 'text-align: center',
  489. setup: function( doc, html, head, body ) {
  490. this.setValue( body.getStyle( 'margin-left' ) || body.getAttribute( 'marginleft' ) || '' );
  491. },
  492. commit: commitMargin( 'left' )
  493. },
  494. {
  495. type: 'text',
  496. id: 'marginRight',
  497. label: lang.marginRight,
  498. style: 'width: 80px; text-align: center',
  499. align: 'center',
  500. inputStyle: 'text-align: center',
  501. setup: function( doc, html, head, body ) {
  502. this.setValue( body.getStyle( 'margin-right' ) || body.getAttribute( 'marginright' ) || '' );
  503. },
  504. commit: commitMargin( 'right' )
  505. }
  506. ]
  507. },
  508. {
  509. type: 'text',
  510. id: 'marginBottom',
  511. label: lang.marginBottom,
  512. style: 'width: 80px; text-align: center',
  513. align: 'center',
  514. inputStyle: 'text-align: center',
  515. setup: function( doc, html, head, body ) {
  516. this.setValue( body.getStyle( 'margin-bottom' ) || body.getAttribute( 'marginbottom' ) || '' );
  517. },
  518. commit: commitMargin( 'bottom' )
  519. }
  520. ]
  521. }
  522. ]
  523. }
  524. ]
  525. },
  526. {
  527. id: 'meta',
  528. label: lang.meta,
  529. elements: [
  530. {
  531. type: 'textarea',
  532. id: 'metaKeywords',
  533. label: lang.metaKeywords,
  534. setup: setupMeta( 'keywords' ),
  535. commit: commitMeta( 'keywords' )
  536. },
  537. {
  538. type: 'textarea',
  539. id: 'metaDescription',
  540. label: lang.metaDescription,
  541. setup: setupMeta( 'description' ),
  542. commit: commitMeta( 'description' )
  543. },
  544. {
  545. type: 'text',
  546. id: 'metaAuthor',
  547. label: lang.metaAuthor,
  548. setup: setupMeta( 'author' ),
  549. commit: commitMeta( 'author' )
  550. },
  551. {
  552. type: 'text',
  553. id: 'metaCopyright',
  554. label: lang.metaCopyright,
  555. setup: setupMeta( 'copyright' ),
  556. commit: commitMeta( 'copyright' )
  557. }
  558. ]
  559. },
  560. {
  561. id: 'preview',
  562. label: langCommon.preview,
  563. elements: [
  564. {
  565. type: 'html',
  566. id: 'previewHtml',
  567. html: '<iframe src="' + previewSrc + '" style="width: 100%; height: 310px" hidefocus="true" frameborder="0" ' +
  568. 'id="cke_docProps_preview_iframe"></iframe>',
  569. onLoad: function() {
  570. this.getDialog().on( 'selectPage', function( ev ) {
  571. if ( ev.data.page == 'preview' ) {
  572. var self = this;
  573. setTimeout( function() {
  574. var doc = CKEDITOR.document.getById( 'cke_docProps_preview_iframe' ).getFrameDocument(),
  575. html = doc.getElementsByTag( 'html' ).getItem( 0 ),
  576. head = doc.getHead(),
  577. body = doc.getBody();
  578. self.commitContent( doc, html, head, body, 1 );
  579. }, 50 );
  580. }
  581. });
  582. CKEDITOR.document.getById( 'cke_docProps_preview_iframe' ).getAscendant( 'table' ).setStyle( 'height', '100%' );
  583. }
  584. }
  585. ]
  586. }
  587. ]
  588. };
  589. });