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