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