/** * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or http://ckeditor.com/license */ ( function() { CKEDITOR.dialog.add( 'templates', function( editor ) { // Constructs the HTML view of the specified templates data. function renderTemplatesList( container, templatesDefinitions ) { // clear loading wait text. container.setHtml( '' ); for ( var i = 0, totalDefs = templatesDefinitions.length; i < totalDefs; i++ ) { var definition = CKEDITOR.getTemplates( templatesDefinitions[ i ] ), imagesPath = definition.imagesPath, templates = definition.templates, count = templates.length; for ( var j = 0; j < count; j++ ) { var template = templates[ j ], item = createTemplateItem( template, imagesPath ); item.setAttribute( 'aria-posinset', j + 1 ); item.setAttribute( 'aria-setsize', count ); container.append( item ); } } } function createTemplateItem( template, imagesPath ) { var item = CKEDITOR.dom.element.createFromHtml( '' + '
' + '
' ); // Build the inner HTML of our new item DIV. var html = ''; if ( template.image && imagesPath ) { html += ''; } html += ''; item.getFirst().setHtml( html ); item.on( 'click', function() { insertTemplate( template.html ); } ); return item; } // Insert the specified template content into editor. // @param {Number} index function insertTemplate( html ) { var dialog = CKEDITOR.dialog.getCurrent(), isReplace = dialog.getValueOf( 'selectTpl', 'chkInsertOpt' ); if ( isReplace ) { editor.fire( 'saveSnapshot' ); // Everything should happen after the document is loaded (#4073). editor.setData( html, function() { dialog.hide(); // Place the cursor at the first editable place. var range = editor.createRange(); range.moveToElementEditStart( editor.editable() ); range.select(); setTimeout( function() { editor.fire( 'saveSnapshot' ); }, 0 ); } ); } else { editor.insertHtml( html ); dialog.hide(); } } function keyNavigation( evt ) { var target = evt.data.getTarget(), onList = listContainer.equals( target ); // Keyboard navigation for template list. if ( onList || listContainer.contains( target ) ) { var keystroke = evt.data.getKeystroke(), items = listContainer.getElementsByTag( 'a' ), focusItem; if ( items ) { // Focus not yet onto list items? if ( onList ) focusItem = items.getItem( 0 ); else { switch ( keystroke ) { case 40: // ARROW-DOWN focusItem = target.getNext(); break; case 38: // ARROW-UP focusItem = target.getPrevious(); break; case 13: // ENTER case 32: // SPACE target.fire( 'click' ); } } if ( focusItem ) { focusItem.focus(); evt.data.preventDefault(); } } } } // Load skin at first. var plugin = CKEDITOR.plugins.get( 'templates' ); CKEDITOR.document.appendStyleSheet( CKEDITOR.getUrl( plugin.path + 'dialogs/templates.css' ) ); var listContainer; var templateListLabelId = 'cke_tpl_list_label_' + CKEDITOR.tools.getNextNumber(), lang = editor.lang.templates, config = editor.config; return { title: editor.lang.templates.title, minWidth: CKEDITOR.env.ie ? 440 : 400, minHeight: 340, contents: [ { id: 'selectTpl', label: lang.title, elements: [ { type: 'vbox', padding: 5, children: [ { id: 'selectTplText', type: 'html', html: '' + lang.selectPromptMsg + '' }, { id: 'templatesList', type: 'html', focus: true, html: '
' + '
' + '
' + '' + lang.options + '' }, { id: 'chkInsertOpt', type: 'checkbox', label: lang.insertOption, 'default': config.templates_replaceContent } ] } ] } ], buttons: [ CKEDITOR.dialog.cancelButton ], onShow: function() { var templatesListField = this.getContentElement( 'selectTpl', 'templatesList' ); listContainer = templatesListField.getElement(); CKEDITOR.loadTemplates( config.templates_files, function() { var templates = ( config.templates || 'default' ).split( ',' ); if ( templates.length ) { renderTemplatesList( listContainer, templates ); templatesListField.focus(); } else { listContainer.setHtml( '
' + '' + lang.emptyListMsg + '' + '
' ); } } ); this._.element.on( 'keydown', keyNavigation ); }, onHide: function() { this._.element.removeListener( 'keydown', keyNavigation ); } }; } ); } )();