2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
8 var doc = CKEDITOR.document;
10 CKEDITOR.dialog.add( 'templates', function( editor )
12 // Constructs the HTML view of the specified templates data.
13 function renderTemplatesList( container, templatesDefinitions )
15 // clear loading wait text.
16 container.setHtml( '' );
18 for ( var i = 0, totalDefs = templatesDefinitions.length ; i < totalDefs ; i++ )
20 var definition = CKEDITOR.getTemplates( templatesDefinitions[ i ] ),
21 imagesPath = definition.imagesPath,
22 templates = definition.templates,
23 count = templates.length;
25 for ( var j = 0 ; j < count ; j++ )
27 var template = templates[ j ],
28 item = createTemplateItem( template, imagesPath );
29 item.setAttribute( 'aria-posinset', j + 1 );
30 item.setAttribute( 'aria-setsize', count );
31 container.append( item );
36 function createTemplateItem( template, imagesPath )
38 var item = CKEDITOR.dom.element.createFromHtml(
39 '<a href="javascript:void(0)" tabIndex="-1" role="option" >' +
40 '<div class="cke_tpl_item"></div>' +
43 // Build the inner HTML of our new item DIV.
44 var html = '<table style="width:350px;" class="cke_tpl_preview" role="presentation"><tr>';
46 if ( template.image && imagesPath )
47 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>';
49 html += '<td style="white-space:normal;"><span class="cke_tpl_title">' + template.title + '</span><br/>';
51 if ( template.description )
52 html += '<span>' + template.description + '</span>';
54 html += '</td></tr></table>';
56 item.getFirst().setHtml( html );
58 item.on( 'click', function() { insertTemplate( template.html ); } );
64 * Insert the specified template content into editor.
65 * @param {Number} index
67 function insertTemplate( html )
69 var dialog = CKEDITOR.dialog.getCurrent(),
70 isInsert = dialog.getValueOf( 'selectTpl', 'chkInsertOpt' );
74 // Everything should happen after the document is loaded (#4073).
75 editor.on( 'contentDom', function( evt )
80 // Place the cursor at the first editable place.
81 var range = new CKEDITOR.dom.range( editor.document );
82 range.moveToElementEditStart( editor.document.getBody() );
84 setTimeout( function()
86 editor.fire( 'saveSnapshot' );
90 editor.fire( 'saveSnapshot' );
91 editor.setData( html );
95 editor.insertHtml( html );
100 function keyNavigation( evt )
102 var target = evt.data.getTarget(),
103 onList = listContainer.equals( target );
105 // Keyboard navigation for template list.
106 if ( onList || listContainer.contains( target ) )
108 var keystroke = evt.data.getKeystroke(),
109 items = listContainer.getElementsByTag( 'a' ),
114 // Focus not yet onto list items?
116 focusItem = items.getItem( 0 );
121 case 40 : // ARROW-DOWN
122 focusItem = target.getNext();
125 case 38 : // ARROW-UP
126 focusItem = target.getPrevious();
131 target.fire( 'click' );
138 evt.data.preventDefault();
144 // Load skin at first.
145 CKEDITOR.skins.load( editor, 'templates' );
149 var templateListLabelId = 'cke_tpl_list_label_' + CKEDITOR.tools.getNextNumber(),
150 lang = editor.lang.templates,
151 config = editor.config;
153 title :editor.lang.templates.title,
155 minWidth : CKEDITOR.env.ie ? 440 : 400,
174 lang.selectPromptMsg +
178 id : 'templatesList',
182 '<div class="cke_tpl_list" tabIndex="-1" role="listbox" aria-labelledby="' + templateListLabelId+ '">' +
183 '<div class="cke_tpl_loading"><span></span></div>' +
185 '<span class="cke_voice_label" id="' + templateListLabelId + '">' + lang.options+ '</span>'
190 label : lang.insertOption,
191 'default' : config.templates_replaceContent
199 buttons : [ CKEDITOR.dialog.cancelButton ],
203 var templatesListField = this.getContentElement( 'selectTpl' , 'templatesList' );
204 listContainer = templatesListField.getElement();
206 CKEDITOR.loadTemplates( config.templates_files, function()
208 var templates = ( config.templates || 'default' ).split( ',' );
210 if ( templates.length )
212 renderTemplatesList( listContainer, templates );
213 templatesListField.focus();
217 listContainer.setHtml(
218 '<div class="cke_tpl_empty">' +
219 '<span>' + lang.emptyListMsg + '</span>' +
224 this._.element.on( 'keydown', keyNavigation );
229 this._.element.removeListener( 'keydown', keyNavigation );