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