templates.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. (function()
  6. {
  7. var doc = CKEDITOR.document;
  8. CKEDITOR.dialog.add( 'templates', function( editor )
  9. {
  10. // Constructs the HTML view of the specified templates data.
  11. function renderTemplatesList( container, templatesDefinitions )
  12. {
  13. // clear loading wait text.
  14. container.setHtml( '' );
  15. for ( var i = 0, totalDefs = templatesDefinitions.length ; i < totalDefs ; i++ )
  16. {
  17. var definition = CKEDITOR.getTemplates( templatesDefinitions[ i ] ),
  18. imagesPath = definition.imagesPath,
  19. templates = definition.templates,
  20. count = templates.length;
  21. for ( var j = 0 ; j < count ; j++ )
  22. {
  23. var template = templates[ j ],
  24. item = createTemplateItem( template, imagesPath );
  25. item.setAttribute( 'aria-posinset', j + 1 );
  26. item.setAttribute( 'aria-setsize', count );
  27. container.append( item );
  28. }
  29. }
  30. }
  31. function createTemplateItem( template, imagesPath )
  32. {
  33. var item = CKEDITOR.dom.element.createFromHtml(
  34. '<a href="javascript:void(0)" tabIndex="-1" role="option" >' +
  35. '<div class="cke_tpl_item"></div>' +
  36. '</a>' );
  37. // Build the inner HTML of our new item DIV.
  38. var html = '<table style="width:350px;" class="cke_tpl_preview" role="presentation"><tr>';
  39. if ( template.image && imagesPath )
  40. html += '<td class="cke_tpl_preview_img"><img src="' + CKEDITOR.getUrl( imagesPath + template.image ) + '"' + ( CKEDITOR.env.ie6Compat ? ' onload="this.width=this.width"' : '' ) + ' alt="" title=""></td>';
  41. html += '<td style="white-space:normal;"><span class="cke_tpl_title">' + template.title + '</span><br/>';
  42. if ( template.description )
  43. html += '<span>' + template.description + '</span>';
  44. html += '</td></tr></table>';
  45. item.getFirst().setHtml( html );
  46. item.on( 'click', function() { insertTemplate( template.html ); } );
  47. return item;
  48. }
  49. /**
  50. * Insert the specified template content into editor.
  51. * @param {Number} index
  52. */
  53. function insertTemplate( html )
  54. {
  55. var dialog = CKEDITOR.dialog.getCurrent(),
  56. isInsert = dialog.getValueOf( 'selectTpl', 'chkInsertOpt' );
  57. if ( isInsert )
  58. {
  59. // Everything should happen after the document is loaded (#4073).
  60. editor.on( 'contentDom', function( evt )
  61. {
  62. evt.removeListener();
  63. dialog.hide();
  64. // Place the cursor at the first editable place.
  65. var range = new CKEDITOR.dom.range( editor.document );
  66. range.moveToElementEditStart( editor.document.getBody() );
  67. range.select( 1 );
  68. setTimeout( function()
  69. {
  70. editor.fire( 'saveSnapshot' );
  71. }, 0 );
  72. });
  73. editor.fire( 'saveSnapshot' );
  74. editor.setData( html );
  75. }
  76. else
  77. {
  78. editor.insertHtml( html );
  79. dialog.hide();
  80. }
  81. }
  82. function keyNavigation( evt )
  83. {
  84. var target = evt.data.getTarget(),
  85. onList = listContainer.equals( target );
  86. // Keyboard navigation for template list.
  87. if ( onList || listContainer.contains( target ) )
  88. {
  89. var keystroke = evt.data.getKeystroke(),
  90. items = listContainer.getElementsByTag( 'a' ),
  91. focusItem;
  92. if ( items )
  93. {
  94. // Focus not yet onto list items?
  95. if ( onList )
  96. focusItem = items.getItem( 0 );
  97. else
  98. {
  99. switch ( keystroke )
  100. {
  101. case 40 : // ARROW-DOWN
  102. focusItem = target.getNext();
  103. break;
  104. case 38 : // ARROW-UP
  105. focusItem = target.getPrevious();
  106. break;
  107. case 13 : // ENTER
  108. case 32 : // SPACE
  109. target.fire( 'click' );
  110. }
  111. }
  112. if ( focusItem )
  113. {
  114. focusItem.focus();
  115. evt.data.preventDefault();
  116. }
  117. }
  118. }
  119. }
  120. // Load skin at first.
  121. CKEDITOR.skins.load( editor, 'templates' );
  122. var listContainer;
  123. var templateListLabelId = 'cke_tpl_list_label_' + CKEDITOR.tools.getNextNumber(),
  124. lang = editor.lang.templates,
  125. config = editor.config;
  126. return {
  127. title :editor.lang.templates.title,
  128. minWidth : CKEDITOR.env.ie ? 440 : 400,
  129. minHeight : 340,
  130. contents :
  131. [
  132. {
  133. id :'selectTpl',
  134. label : lang.title,
  135. elements :
  136. [
  137. {
  138. type : 'vbox',
  139. padding : 5,
  140. children :
  141. [
  142. {
  143. id : 'selectTplText',
  144. type : 'html',
  145. html :
  146. '<span>' +
  147. lang.selectPromptMsg +
  148. '</span>'
  149. },
  150. {
  151. id : 'templatesList',
  152. type : 'html',
  153. focus: true,
  154. html :
  155. '<div class="cke_tpl_list" tabIndex="-1" role="listbox" aria-labelledby="' + templateListLabelId+ '">' +
  156. '<div class="cke_tpl_loading"><span></span></div>' +
  157. '</div>' +
  158. '<span class="cke_voice_label" id="' + templateListLabelId + '">' + lang.options+ '</span>'
  159. },
  160. {
  161. id : 'chkInsertOpt',
  162. type : 'checkbox',
  163. label : lang.insertOption,
  164. 'default' : config.templates_replaceContent
  165. }
  166. ]
  167. }
  168. ]
  169. }
  170. ],
  171. buttons : [ CKEDITOR.dialog.cancelButton ],
  172. onShow : function()
  173. {
  174. var templatesListField = this.getContentElement( 'selectTpl' , 'templatesList' );
  175. listContainer = templatesListField.getElement();
  176. CKEDITOR.loadTemplates( config.templates_files, function()
  177. {
  178. var templates = ( config.templates || 'default' ).split( ',' );
  179. if ( templates.length )
  180. {
  181. renderTemplatesList( listContainer, templates );
  182. templatesListField.focus();
  183. }
  184. else
  185. {
  186. listContainer.setHtml(
  187. '<div class="cke_tpl_empty">' +
  188. '<span>' + lang.emptyListMsg + '</span>' +
  189. '</div>' );
  190. }
  191. });
  192. this._.element.on( 'keydown', keyNavigation );
  193. },
  194. onHide : function()
  195. {
  196. this._.element.removeListener( 'keydown', keyNavigation );
  197. }
  198. };
  199. });
  200. })();