2 Copyright (c) 2003-2010, 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 var listId = 'cke' + CKEDITOR.tools.getNextNumber();
\r
12 // Constructs the HTML view of the specified templates data.
\r
13 function renderTemplatesList( editor, templatesDefinitions )
\r
15 var listDiv = doc.getById( listId );
\r
17 // clear loading wait text.
\r
18 listDiv.setHtml( '' );
\r
20 for ( var i = 0 ; i < templatesDefinitions.length ; i++ )
\r
22 var definition = CKEDITOR.getTemplates( templatesDefinitions[ i ] ),
\r
23 imagesPath = definition.imagesPath,
\r
24 templates = definition.templates;
\r
26 for ( var j = 0 ; j < templates.length ; j++ )
\r
28 var template = templates[ j ];
\r
29 listDiv.append( createTemplateItem( editor, template, imagesPath ) );
\r
34 function createTemplateItem( editor, template, imagesPath )
\r
36 var div = doc.createElement( 'div' );
\r
37 div.setAttribute( 'class', 'cke_tpl_item' );
\r
39 // Build the inner HTML of our new item DIV.
\r
40 var html = '<table style="width:350px;" class="cke_tpl_preview"><tr>';
\r
42 if ( template.image && imagesPath )
\r
43 html += '<td class="cke_tpl_preview_img"><img src="' + CKEDITOR.getUrl( imagesPath + template.image ) + '"></td>';
\r
45 html += '<td style="white-space:normal;"><span class="cke_tpl_title">' + template.title + '</span><br/>';
\r
47 if ( template.description )
\r
48 html += '<span>' + template.description + '</span>';
\r
50 html += '</td></tr></table>';
\r
52 div.setHtml( html );
\r
54 div.on( 'mouseover', function()
\r
56 div.addClass( 'cke_tpl_hover' );
\r
59 div.on( 'mouseout', function()
\r
61 div.removeClass( 'cke_tpl_hover' );
\r
64 div.on( 'click', function()
\r
66 insertTemplate( editor, template.html );
\r
73 * Insert the specified template content
\r
75 * @param {Number} index
\r
77 function insertTemplate( editor, html )
\r
79 var dialog = CKEDITOR.dialog.getCurrent(),
\r
80 isInsert = dialog.getValueOf( 'selectTpl', 'chkInsertOpt' );
\r
84 // Everything should happen after the document is loaded (#4073).
\r
85 editor.on( 'contentDom', function( evt )
\r
87 evt.removeListener();
\r
90 // Place the cursor at the first editable place.
\r
91 var range = new CKEDITOR.dom.range( editor.document );
\r
92 range.moveToElementEditStart( editor.document.getBody() );
\r
93 range.select( true );
\r
94 setTimeout( function ()
\r
96 editor.fire( 'saveSnapshot' );
\r
100 editor.fire( 'saveSnapshot' );
\r
101 editor.setData( html );
\r
105 editor.insertHtml( html );
\r
110 CKEDITOR.dialog.add( 'templates', function( editor )
\r
112 // Load skin at first.
\r
113 CKEDITOR.skins.load( editor, 'templates' );
\r
116 * Load templates once.
\r
118 var isLoaded = false;
\r
121 title :editor.lang.templates.title,
\r
123 minWidth : CKEDITOR.env.ie ? 440 : 400,
\r
130 label : editor.lang.templates.title,
\r
142 editor.lang.templates.selectPromptMsg +
\r
148 '<div id="' + listId + '" class="cke_tpl_list">' +
\r
149 '<div class="cke_tpl_loading"><span></span></div>' +
\r
153 id : 'chkInsertOpt',
\r
155 label : editor.lang.templates.insertOption,
\r
156 'default' : editor.config.templates_replaceContent
\r
164 buttons : [ CKEDITOR.dialog.cancelButton ],
\r
166 onShow : function()
\r
168 CKEDITOR.loadTemplates( editor.config.templates_files, function()
\r
170 var templates = editor.config.templates.split( ',' );
\r
172 if ( templates.length )
\r
173 renderTemplatesList( editor, templates );
\r
176 var listCtEl = doc.getById( listId );
\r
178 '<div class="cke_tpl_empty">' +
\r
179 '<span>' + editor.lang.templates.emptyListMsg + '</span>' +
\r