Roo/form/HtmlEditor/ToolbarStandard.js
[roojs1] / Roo / form / HtmlEditor / ToolbarStandard.js
1 // <script type="text/javascript">
2 /*
3  * Based on
4  * Ext JS Library 1.1.1
5  * Copyright(c) 2006-2007, Ext JS, LLC.
6  *  
7  
8  */
9
10 /**
11  * @class Roo.form.HtmlEditorToolbar1
12  * Basic Toolbar
13  * 
14  * Usage:
15  *
16  new Roo.form.HtmlEditor({
17     ....
18     toolbars : [
19         new Roo.form.HtmlEditorToolbar1({
20             disable : { fonts: 1 , format: 1, ..., ... , ...],
21             btns : [ .... ]
22         })
23     }
24      
25  * 
26  * @cfg {Object} disable List of elements to disable..
27  * @cfg {Array} btns List of additional buttons.
28  * 
29  * 
30  * NEEDS Extra CSS? 
31  * .x-html-editor-tb .x-edit-none .x-btn-text { background: none; }
32  */
33  
34 Roo.form.HtmlEditor.ToolbarStandard = function(config)
35 {
36     
37     Roo.apply(this, config);
38     
39     // default disabled, based on 'good practice'..
40     this.disable = this.disable || {};
41     Roo.applyIf(this.disable, {
42         fontSize : true,
43         colors : true,
44         specialElements : true
45     });
46     
47     
48     //Roo.form.HtmlEditorToolbar1.superclass.constructor.call(this, editor.wrap.dom.firstChild, [], config);
49     // dont call parent... till later.
50 }
51
52 Roo.apply(Roo.form.HtmlEditor.ToolbarStandard.prototype,  {
53     
54     tb: false,
55     
56     rendered: false,
57     
58     editor : false,
59     /**
60      * @cfg {Object} disable  List of toolbar elements to disable
61          
62      */
63     disable : false,
64       /**
65      * @cfg {Array} fontFamilies An array of available font families
66      */
67     fontFamilies : [
68         'Arial',
69         'Courier New',
70         'Tahoma',
71         'Times New Roman',
72         'Verdana'
73     ],
74     
75     specialChars : [
76            "&#169;",
77           "&#174;",     
78           "&#8482;",    
79           "&#163;" ,    
80          // "&#8212;",    
81           "&#8230;",    
82           "&#247;" ,    
83         //  "&#225;" ,     ?? a acute?
84            "&#8364;"    , //Euro
85        //   "&#8220;"    ,
86         //  "&#8221;"    ,
87         //  "&#8226;"    ,
88           "&#176;"  //   , // degrees
89
90          // "&#233;"     , // e ecute
91          // "&#250;"     , // u ecute?
92     ],
93     
94     specialElements : [
95         {
96             text: "Insert Table",
97             xtype: 'MenuItem',
98             xns : Roo.Menu,
99             ihtml :  '<table><tr><td>Cell</td></tr></table>' 
100                 
101         },
102         {    
103             text: "Insert Image",
104             xtype: 'MenuItem',
105             xns : Roo.Menu,
106             ihtml : '<img src="about:blank"/>'
107             
108         }
109         
110          
111     ],
112     
113     
114     inputElements : [ 
115             "form", "input:text", "input:hidden", "input:checkbox", "input:radio", "input:password", 
116             "input:submit", "input:button", "select", "textarea", "label" ],
117     formats : [
118         ["p"] ,  
119         ["h1"],["h2"],["h3"],["h4"],["h5"],["h6"], 
120         ["pre"],[ "code"], 
121         ["abbr"],[ "acronym"],[ "address"],[ "cite"],[ "samp"],[ "var"]
122     ],
123      /**
124      * @cfg {String} defaultFont default font to use.
125      */
126     defaultFont: 'tahoma',
127    
128     fontSelect : false,
129     
130     
131     formatCombo : false,
132     
133     init : function(editor)
134     {
135         this.editor = editor;
136         
137         
138         var fid = editor.frameId;
139         var etb = this;
140         function btn(id, toggle, handler){
141             var xid = fid + '-'+ id ;
142             return {
143                 id : xid,
144                 cmd : id,
145                 cls : 'x-btn-icon x-edit-'+id,
146                 enableToggle:toggle !== false,
147                 scope: editor, // was editor...
148                 handler:handler||editor.relayBtnCmd,
149                 clickEvent:'mousedown',
150                 tooltip: etb.buttonTips[id] || undefined, ///tips ???
151                 tabIndex:-1
152             };
153         }
154         
155         
156         
157         var tb = new Roo.Toolbar(editor.wrap.dom.firstChild);
158         this.tb = tb;
159          // stop form submits
160         tb.el.on('click', function(e){
161             e.preventDefault(); // what does this do?
162         });
163
164         if(!this.disable.font && !Roo.isSafari){
165             /* why no safari for fonts
166             editor.fontSelect = tb.el.createChild({
167                 tag:'select',
168                 tabIndex: -1,
169                 cls:'x-font-select',
170                 html: editor.createFontOptions()
171             });
172             editor.fontSelect.on('change', function(){
173                 var font = editor.fontSelect.dom.value;
174                 editor.relayCmd('fontname', font);
175                 editor.deferFocus();
176             }, editor);
177             tb.add(
178                 editor.fontSelect.dom,
179                 '-'
180             );
181             */
182         };
183         if(!this.disable.formats){
184             this.formatCombo = new Roo.form.ComboBox({
185                 store: new Roo.data.SimpleStore({
186                     id : 'tag',
187                     fields: ['tag'],
188                     data : this.formats // from states.js
189                 }),
190                 blockFocus : true,
191                 //autoCreate : {tag: "div",  size: "20"},
192                 displayField:'tag',
193                 typeAhead: false,
194                 mode: 'local',
195                 editable : false,
196                 triggerAction: 'all',
197                 emptyText:'Add tag',
198                 selectOnFocus:true,
199                 width:135,
200                 listeners : {
201                     'select': function(c, r, i) {
202                         editor.insertTag(r.get('tag'));
203                         editor.focus();
204                     }
205                 }
206
207             });
208             tb.addField(this.formatCombo);
209             
210         }
211         
212         if(!this.disable.format){
213             tb.add(
214                 btn('bold'),
215                 btn('italic'),
216                 btn('underline')
217             );
218         };
219         if(!this.disable.fontSize){
220             tb.add(
221                 '-',
222                 
223                 
224                 btn('increasefontsize', false, editor.adjustFont),
225                 btn('decreasefontsize', false, editor.adjustFont)
226             );
227         };
228         
229         
230         if(!this.disable.colors){
231             tb.add(
232                 '-', {
233                     id:editor.frameId +'-forecolor',
234                     cls:'x-btn-icon x-edit-forecolor',
235                     clickEvent:'mousedown',
236                     tooltip: this.buttonTips['forecolor'] || undefined,
237                     tabIndex:-1,
238                     menu : new Roo.menu.ColorMenu({
239                         allowReselect: true,
240                         focus: Roo.emptyFn,
241                         value:'000000',
242                         plain:true,
243                         selectHandler: function(cp, color){
244                             editor.execCmd('forecolor', Roo.isSafari || Roo.isIE ? '#'+color : color);
245                             editor.deferFocus();
246                         },
247                         scope: editor,
248                         clickEvent:'mousedown'
249                     })
250                 }, {
251                     id:editor.frameId +'backcolor',
252                     cls:'x-btn-icon x-edit-backcolor',
253                     clickEvent:'mousedown',
254                     tooltip: this.buttonTips['backcolor'] || undefined,
255                     tabIndex:-1,
256                     menu : new Roo.menu.ColorMenu({
257                         focus: Roo.emptyFn,
258                         value:'FFFFFF',
259                         plain:true,
260                         allowReselect: true,
261                         selectHandler: function(cp, color){
262                             if(Roo.isGecko){
263                                 editor.execCmd('useCSS', false);
264                                 editor.execCmd('hilitecolor', color);
265                                 editor.execCmd('useCSS', true);
266                                 editor.deferFocus();
267                             }else{
268                                 editor.execCmd(Roo.isOpera ? 'hilitecolor' : 'backcolor', 
269                                     Roo.isSafari || Roo.isIE ? '#'+color : color);
270                                 editor.deferFocus();
271                             }
272                         },
273                         scope:editor,
274                         clickEvent:'mousedown'
275                     })
276                 }
277             );
278         };
279         // now add all the items...
280         
281
282         if(!this.disable.alignments){
283             tb.add(
284                 '-',
285                 btn('justifyleft'),
286                 btn('justifycenter'),
287                 btn('justifyright')
288             );
289         };
290
291         //if(!Roo.isSafari){
292             if(!this.disable.links){
293                 tb.add(
294                     '-',
295                     btn('createlink', false, editor.createLink)    /// MOVE TO HERE?!!?!?!?!
296                 );
297             };
298
299             if(!this.disable.lists){
300                 tb.add(
301                     '-',
302                     btn('insertorderedlist'),
303                     btn('insertunorderedlist')
304                 );
305             }
306             if(!this.disable.sourceEdit){
307                 tb.add(
308                     '-',
309                     btn('sourceedit', true, function(btn){
310                         this.toggleSourceEdit(btn.pressed);
311                     })
312                 );
313             }
314         //}
315         
316         var smenu = { };
317         // special menu.. - needs to be tidied up..
318         if (!this.disable.special) {
319             smenu = {
320                 text: "&#169;",
321                 cls: 'x-edit-none',
322                 
323                 menu : {
324                     items : []
325                 }
326             };
327             for (var i =0; i < this.specialChars.length; i++) {
328                 smenu.menu.items.push({
329                     
330                     html: this.specialChars[i],
331                     handler: function(a,b) {
332                         editor.insertAtCursor(String.fromCharCode(a.html.replace('&#','').replace(';', '')));
333                         
334                     },
335                     tabIndex:-1
336                 });
337             }
338             
339             
340             tb.add(smenu);
341             
342             
343         }
344          
345         if (!this.disable.specialElements) {
346             var semenu = {
347                 text: "Other;",
348                 cls: 'x-edit-none',
349                 menu : {
350                     items : []
351                 }
352             };
353             for (var i =0; i < this.specialElements.length; i++) {
354                 semenu.menu.items.push(
355                     Roo.apply({ 
356                         handler: function(a,b) {
357                             editor.insertAtCursor(this.ihtml);
358                         }
359                     }, this.specialElements[i])
360                 );
361                     
362             }
363             
364             tb.add(semenu);
365             
366             
367         }
368          
369         
370         if (this.btns) {
371             for(var i =0; i< this.btns.length;i++) {
372                 var b = this.btns[i];
373                 b.cls =  'x-edit-none';
374                 b.scope = editor;
375                 tb.add(b);
376             }
377         
378         }
379         
380         
381         
382         // disable everything...
383         
384         this.tb.items.each(function(item){
385            if(item.id != editor.frameId+ '-sourceedit'){
386                 item.disable();
387             }
388         });
389         this.rendered = true;
390         
391         // the all the btns;
392         editor.on('editorevent', this.updateToolbar, this);
393         // other toolbars need to implement this..
394         //editor.on('editmodechange', this.updateToolbar, this);
395     },
396     
397     
398     
399     /**
400      * Protected method that will not generally be called directly. It triggers
401      * a toolbar update by reading the markup state of the current selection in the editor.
402      */
403     updateToolbar: function(){
404
405         if(!this.editor.activated){
406             this.editor.onFirstFocus();
407             return;
408         }
409
410         var btns = this.tb.items.map, 
411             doc = this.editor.doc,
412             frameId = this.editor.frameId;
413
414         if(!this.disable.font && !Roo.isSafari){
415             /*
416             var name = (doc.queryCommandValue('FontName')||this.editor.defaultFont).toLowerCase();
417             if(name != this.fontSelect.dom.value){
418                 this.fontSelect.dom.value = name;
419             }
420             */
421         }
422         if(!this.disable.format){
423             btns[frameId + '-bold'].toggle(doc.queryCommandState('bold'));
424             btns[frameId + '-italic'].toggle(doc.queryCommandState('italic'));
425             btns[frameId + '-underline'].toggle(doc.queryCommandState('underline'));
426         }
427         if(!this.disable.alignments){
428             btns[frameId + '-justifyleft'].toggle(doc.queryCommandState('justifyleft'));
429             btns[frameId + '-justifycenter'].toggle(doc.queryCommandState('justifycenter'));
430             btns[frameId + '-justifyright'].toggle(doc.queryCommandState('justifyright'));
431         }
432         if(!Roo.isSafari && !this.disable.lists){
433             btns[frameId + '-insertorderedlist'].toggle(doc.queryCommandState('insertorderedlist'));
434             btns[frameId + '-insertunorderedlist'].toggle(doc.queryCommandState('insertunorderedlist'));
435         }
436         
437         var ans = this.editor.getAllAncestors();
438         if (this.formatCombo) {
439             
440             
441             var store = this.formatCombo.store;
442             this.formatCombo.setValue("");
443             for (var i =0; i < ans.length;i++) {
444                 if (ans[i] && store.query('tag',ans[i].tagName.toLowerCase(), false).length) {
445                     // select it..
446                     this.formatCombo.setValue(ans[i].tagName.toLowerCase());
447                     break;
448                 }
449             }
450         }
451         
452         
453         
454         // hides menus... - so this cant be on a menu...
455         Roo.menu.MenuMgr.hideAll();
456
457         //this.editorsyncValue();
458     },
459    
460     
461     createFontOptions : function(){
462         var buf = [], fs = this.fontFamilies, ff, lc;
463         for(var i = 0, len = fs.length; i< len; i++){
464             ff = fs[i];
465             lc = ff.toLowerCase();
466             buf.push(
467                 '<option value="',lc,'" style="font-family:',ff,';"',
468                     (this.defaultFont == lc ? ' selected="true">' : '>'),
469                     ff,
470                 '</option>'
471             );
472         }
473         return buf.join('');
474     },
475     
476     toggleSourceEdit : function(sourceEditMode){
477         if(sourceEditMode === undefined){
478             sourceEditMode = !this.sourceEditMode;
479         }
480         this.sourceEditMode = sourceEditMode === true;
481         var btn = this.tb.items.get(this.editor.frameId +'-sourceedit');
482         // just toggle the button?
483         if(btn.pressed !== this.editor.sourceEditMode){
484             btn.toggle(this.editor.sourceEditMode);
485             return;
486         }
487         
488         if(this.sourceEditMode){
489             this.tb.items.each(function(item){
490                 if(item.cmd != 'sourceedit'){
491                     item.disable();
492                 }
493             });
494           
495         }else{
496             if(this.initialized){
497                 this.tb.items.each(function(item){
498                     item.enable();
499                 });
500             }
501             
502         }
503         // tell the editor that it's been pressed..
504         this.editor.toggleSourceEdit(sourceEditMode);
505        
506     },
507      /**
508      * Object collection of toolbar tooltips for the buttons in the editor. The key
509      * is the command id associated with that button and the value is a valid QuickTips object.
510      * For example:
511 <pre><code>
512 {
513     bold : {
514         title: 'Bold (Ctrl+B)',
515         text: 'Make the selected text bold.',
516         cls: 'x-html-editor-tip'
517     },
518     italic : {
519         title: 'Italic (Ctrl+I)',
520         text: 'Make the selected text italic.',
521         cls: 'x-html-editor-tip'
522     },
523     ...
524 </code></pre>
525     * @type Object
526      */
527     buttonTips : {
528         bold : {
529             title: 'Bold (Ctrl+B)',
530             text: 'Make the selected text bold.',
531             cls: 'x-html-editor-tip'
532         },
533         italic : {
534             title: 'Italic (Ctrl+I)',
535             text: 'Make the selected text italic.',
536             cls: 'x-html-editor-tip'
537         },
538         underline : {
539             title: 'Underline (Ctrl+U)',
540             text: 'Underline the selected text.',
541             cls: 'x-html-editor-tip'
542         },
543         increasefontsize : {
544             title: 'Grow Text',
545             text: 'Increase the font size.',
546             cls: 'x-html-editor-tip'
547         },
548         decreasefontsize : {
549             title: 'Shrink Text',
550             text: 'Decrease the font size.',
551             cls: 'x-html-editor-tip'
552         },
553         backcolor : {
554             title: 'Text Highlight Color',
555             text: 'Change the background color of the selected text.',
556             cls: 'x-html-editor-tip'
557         },
558         forecolor : {
559             title: 'Font Color',
560             text: 'Change the color of the selected text.',
561             cls: 'x-html-editor-tip'
562         },
563         justifyleft : {
564             title: 'Align Text Left',
565             text: 'Align text to the left.',
566             cls: 'x-html-editor-tip'
567         },
568         justifycenter : {
569             title: 'Center Text',
570             text: 'Center text in the editor.',
571             cls: 'x-html-editor-tip'
572         },
573         justifyright : {
574             title: 'Align Text Right',
575             text: 'Align text to the right.',
576             cls: 'x-html-editor-tip'
577         },
578         insertunorderedlist : {
579             title: 'Bullet List',
580             text: 'Start a bulleted list.',
581             cls: 'x-html-editor-tip'
582         },
583         insertorderedlist : {
584             title: 'Numbered List',
585             text: 'Start a numbered list.',
586             cls: 'x-html-editor-tip'
587         },
588         createlink : {
589             title: 'Hyperlink',
590             text: 'Make the selected text a hyperlink.',
591             cls: 'x-html-editor-tip'
592         },
593         sourceedit : {
594             title: 'Source Edit',
595             text: 'Switch to source editing mode.',
596             cls: 'x-html-editor-tip'
597         }
598     },
599     // private
600     onDestroy : function(){
601         if(this.rendered){
602             
603             this.tb.items.each(function(item){
604                 if(item.menu){
605                     item.menu.removeAll();
606                     if(item.menu.el){
607                         item.menu.el.destroy();
608                     }
609                 }
610                 item.destroy();
611             });
612              
613         }
614     },
615     onFirstFocus: function() {
616         this.tb.items.each(function(item){
617            item.enable();
618         });
619     }
620 });
621
622
623
624