Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / library / custom_template / ckeditor / _source / plugins / templates / dialogs / templates.js
blob3ef835f6e2e2e5b44b5320fd1d848802757659b0
1 /*
2 Copyright (c) 2003-2011, 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 )
11                 {
12                         // Constructs the HTML view of the specified templates data.
13                         function renderTemplatesList( container, templatesDefinitions )
14                         {
15                                 // clear loading wait text.
16                                 container.setHtml( '' );
18                                 for ( var i = 0, totalDefs = templatesDefinitions.length ; i < totalDefs ; i++ )
19                                 {
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++ )
26                                         {
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 );
32                                         }
33                                 }
34                         }
36                         function createTemplateItem( template, imagesPath )
37                         {
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;
61                         }
63                         /**
64                          * Insert the specified template content into editor.
65                          * @param {Number} index
66                          */
67                         function insertTemplate( html )
68                         {
69                                 var dialog = CKEDITOR.dialog.getCurrent(),
70                                         isInsert = dialog.getValueOf( 'selectTpl', 'chkInsertOpt' );
72                                 if ( isInsert )
73                                 {
74                                         // Everything should happen after the document is loaded (#4073).
75                                         editor.on( 'contentDom', function( evt )
76                                         {
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( 1 );
84                                                 setTimeout( function()
85                                                 {
86                                                         editor.fire( 'saveSnapshot' );
87                                                 }, 0 );
88                                         });
90                                         editor.fire( 'saveSnapshot' );
91                                         editor.setData( html );
92                                 }
93                                 else
94                                 {
95                                         editor.insertHtml( html );
96                                         dialog.hide();
97                                 }
98                         }
100                         function keyNavigation( evt )
101                         {
102                                 var target = evt.data.getTarget(),
103                                                 onList = listContainer.equals( target );
105                                 // Keyboard navigation for template list.
106                                 if (  onList || listContainer.contains( target ) )
107                                 {
108                                         var keystroke = evt.data.getKeystroke(),
109                                                 items = listContainer.getElementsByTag( 'a' ),
110                                                 focusItem;
112                                         if ( items )
113                                         {
114                                                 // Focus not yet onto list items?
115                                                 if ( onList )
116                                                         focusItem = items.getItem( 0 );
117                                                 else
118                                                 {
119                                                         switch ( keystroke )
120                                                         {
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' );
132                                                         }
133                                                 }
135                                                 if ( focusItem )
136                                                 {
137                                                         focusItem.focus();
138                                                         evt.data.preventDefault();
139                                                 }
140                                         }
141                                 }
142                         }
144                         // Load skin at first.
145                         CKEDITOR.skins.load( editor, 'templates' );
147                         var listContainer;
149                         var templateListLabelId = 'cke_tpl_list_label_' + CKEDITOR.tools.getNextNumber(),
150                                 lang = editor.lang.templates,
151                                 config = editor.config;
152                         return {
153                                 title :editor.lang.templates.title,
155                                 minWidth : CKEDITOR.env.ie ? 440 : 400,
156                                 minHeight : 340,
158                                 contents :
159                                 [
160                                         {
161                                                 id :'selectTpl',
162                                                 label : lang.title,
163                                                 elements :
164                                                 [
165                                                         {
166                                                                 type : 'vbox',
167                                                                 padding : 5,
168                                                                 children :
169                                                                 [
170                                                                         {
171                                                                                 type : 'html',
172                                                                                 html :
173                                                                                         '<span>'  +
174                                                                                                 lang.selectPromptMsg +
175                                                                                         '</span>'
176                                                                         },
177                                                                         {
178                                                                                 id : 'templatesList',
179                                                                                 type : 'html',
180                                                                                 focus: true,
181                                                                                 html :
182                                                                                         '<div class="cke_tpl_list" tabIndex="-1" role="listbox" aria-labelledby="' + templateListLabelId+ '">' +
183                                                                                                 '<div class="cke_tpl_loading"><span></span></div>' +
184                                                                                         '</div>' +
185                                                                                         '<span class="cke_voice_label" id="' + templateListLabelId + '">' + lang.options+ '</span>'
186                                                                         },
187                                                                         {
188                                                                                 id : 'chkInsertOpt',
189                                                                                 type : 'checkbox',
190                                                                                 label : lang.insertOption,
191                                                                                 'default' : config.templates_replaceContent
192                                                                         }
193                                                                 ]
194                                                         }
195                                                 ]
196                                         }
197                                 ],
199                                 buttons : [ CKEDITOR.dialog.cancelButton ],
201                                 onShow : function()
202                                 {
203                                         var templatesListField = this.getContentElement( 'selectTpl' , 'templatesList' );
204                                         listContainer = templatesListField.getElement();
206                                         CKEDITOR.loadTemplates( config.templates_files, function()
207                                                 {
208                                                         var templates = ( config.templates || 'default' ).split( ',' );
210                                                         if ( templates.length )
211                                                         {
212                                                                 renderTemplatesList( listContainer, templates );
213                                                                 templatesListField.focus();
214                                                         }
215                                                         else
216                                                         {
217                                                                 listContainer.setHtml(
218                                                                         '<div class="cke_tpl_empty">' +
219                                                                                 '<span>' + lang.emptyListMsg + '</span>' +
220                                                                         '</div>' );
221                                                         }
222                                                 });
224                                         this._.element.on( 'keydown', keyNavigation );
225                                 },
227                                 onHide : function()
228                                 {
229                                         this._.element.removeListener( 'keydown', keyNavigation );
230                                 }
231                         };
232                 });
233 })();