Update ckeditor to version 3.2.1
[gopost.git] / ckeditor / _source / plugins / templates / dialogs / templates.js
blobaf04e5548b8e416cb8adce300fe6bc1943d58e6b
1 /*
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 (function()
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 ; i < templatesDefinitions.length ; 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>' +
41 '</a>' );
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 ); } );
60 return item;
63 /**
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' );
72 if ( isInsert )
74 // Everything should happen after the document is loaded (#4073).
75 editor.on( 'contentDom', function( evt )
77 evt.removeListener();
78 dialog.hide();
80 // Place the cursor at the first editable place.
81 var range = new CKEDITOR.dom.range( editor.document );
82 range.moveToElementEditStart( editor.document.getBody() );
83 range.select( true );
84 setTimeout( function ()
86 editor.fire( 'saveSnapshot' );
87 }, 0 );
88 } );
90 editor.fire( 'saveSnapshot' );
91 editor.setData( html );
93 else
95 editor.insertHtml( html );
96 dialog.hide();
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' ),
110 focusItem;
112 if ( items )
114 // Focus not yet onto list items?
115 if ( onList )
116 focusItem = items.getItem( 0 );
117 else
119 switch ( keystroke )
121 case 40 : // ARROW-DOWN
122 focusItem = target.getNext();
123 break;
125 case 38 : // ARROW-UP
126 focusItem = target.getPrevious();
127 break;
129 case 13 : // ENTER
130 case 32 : // SPACE
131 target.fire( 'click' );
135 if ( focusItem )
137 focusItem.focus();
138 evt.data.preventDefault();
144 // Load skin at first.
145 CKEDITOR.skins.load( editor, 'templates' );
147 var listContainer;
149 return {
150 title :editor.lang.templates.title,
152 minWidth : CKEDITOR.env.ie ? 440 : 400,
153 minHeight : 340,
155 contents :
158 id :'selectTpl',
159 label : editor.lang.templates.title,
160 elements :
163 type : 'vbox',
164 padding : 5,
165 children :
168 type : 'html',
169 html :
170 '<span>' +
171 editor.lang.templates.selectPromptMsg +
172 '</span>'
175 id : "templatesList",
176 type : 'html',
177 focus: true,
178 html :
179 '<div class="cke_tpl_list" tabIndex="-1" role="listbox" aria-labelledby="cke_tpl_list_label">' +
180 '<div class="cke_tpl_loading"><span></span></div>' +
181 '</div>' +
182 '<span class="cke_voice_label" id="cke_tpl_list_label">' + editor.lang.templates.options+ '</span>'
185 id : 'chkInsertOpt',
186 type : 'checkbox',
187 label : editor.lang.templates.insertOption,
188 'default' : editor.config.templates_replaceContent
196 buttons : [ CKEDITOR.dialog.cancelButton ],
198 onShow : function()
200 var templatesListField = this.getContentElement( 'selectTpl' , 'templatesList' );
201 listContainer = templatesListField.getElement();
203 CKEDITOR.loadTemplates( editor.config.templates_files, function()
205 var templates = editor.config.templates.split( ',' );
207 if ( templates.length )
209 renderTemplatesList( listContainer, templates );
210 templatesListField.focus();
212 else
214 listContainer.setHtml(
215 '<div class="cke_tpl_empty">' +
216 '<span>' + editor.lang.templates.emptyListMsg + '</span>' +
217 '</div>' );
221 this._.element.on( 'keydown', keyNavigation );
224 onHide : function ()
226 this._.element.removeListener( 'keydown', keyNavigation );
230 })();