Roo/bootstrap/htmleditor/ToolbarStandard.js
[roojs1] / Roo / bootstrap / htmleditor / ToolbarStandard.js
1   
2 Roo.namespace('Roo.bootstrap.htmleditor');
3 /**
4  * @class Roo.bootstrap.HtmlEditorToolbar1
5  * Basic Toolbar
6  * 
7  * Usage:
8  *
9  new Roo.bootstrap.HtmlEditor({
10     ....
11     toolbars : [
12         new Roo.bootstrap.HtmlEditorToolbar1({
13             disable : { fonts: 1 , format: 1, ..., ... , ...],
14             btns : [ .... ]
15         })
16     }
17      
18  * 
19  * @cfg {Object} disable List of elements to disable..
20  * @cfg {Array} btns List of additional buttons.
21  * 
22  * 
23  * NEEDS Extra CSS? 
24  * .x-html-editor-tb .x-edit-none .x-btn-text { background: none; }
25  */
26  
27 Roo.bootstrap.htmleditor.ToolbarStandard = function(config)
28 {
29     
30     Roo.apply(this, config);
31     
32     // default disabled, based on 'good practice'..
33     this.disable = this.disable || {};
34     Roo.applyIf(this.disable, {
35         fontSize : true,
36         colors : true,
37         specialElements : true
38     });
39     Roo.bootstrap.htmleditor.ToolbarStandard.superclass.constructor.call(this, config);
40     
41     this.editor = config.editor;
42     this.editorcore = config.editor.editorcore;
43     
44     this.buttons   = new Roo.util.MixedCollection(false, function(o) { return o.cmd; });
45     
46     //Roo.form.HtmlEditorToolbar1.superclass.constructor.call(this, editor.wrap.dom.firstChild, [], config);
47     // dont call parent... till later.
48 }
49 Roo.extend(Roo.bootstrap.htmleditor.ToolbarStandard, Roo.bootstrap.NavSimplebar,  {
50      
51     bar : true,
52     
53     editor : false,
54     editorcore : false,
55     
56     
57     formats : [
58         "p" ,  
59         "h1","h2","h3","h4","h5","h6", 
60         "pre", "code", 
61         "abbr", "acronym", "address", "cite", "samp", "var",
62         'div','span'
63     ],
64     
65     onRender : function(ct, position)
66     {
67        // Roo.log("Call onRender: " + this.xtype);
68         
69        Roo.bootstrap.htmleditor.ToolbarStandard.superclass.onRender.call(this, ct, position);
70        Roo.log(this.el);
71        this.el.dom.style.marginBottom = '0';
72        var _this = this;
73        var editorcore = this.editorcore;
74        var editor= this.editor;
75        
76        var children = [];
77        var btn = function(id,cmd , toggle, handler){
78        
79             var  event = toggle ? 'toggle' : 'click';
80        
81             var a = {
82                 size : 'sm',
83                 xtype: 'Button',
84                 xns: Roo.bootstrap,
85                 glyphicon : id,
86                 cmd : id || cmd,
87                 enableToggle:toggle !== false,
88                 //html : 'submit'
89                 pressed : toggle ? false : null,
90                 listeners : {}
91             };
92             a.listeners[toggle ? 'toggle' : 'click'] = function() {
93                 handler ? handler.call(_this,this) :_this.onBtnClick.call(_this, cmd ||  id);
94             };
95             children.push(a);
96             return a;
97        }
98         
99         var style = {
100                 xtype: 'Button',
101                 size : 'sm',
102                 xns: Roo.bootstrap,
103                 glyphicon : 'font',
104                 //html : 'submit'
105                 menu : {
106                     xtype: 'Menu',
107                     xns: Roo.bootstrap,
108                     items:  []
109                 }
110         };
111         Roo.each(this.formats, function(f) {
112             style.menu.items.push({
113                 xtype :'MenuItem',
114                 xns: Roo.bootstrap,
115                 html : '<'+ f+' style="margin:2px">'+f +'</'+ f+'>',
116                 tagname : f,
117                 listeners : {
118                     click : function()
119                     {
120                         editorcore.insertTag(this.tagname);
121                         editor.focus();
122                     }
123                 }
124                 
125             });
126         });
127          children.push(style);   
128         
129         Roo.log(this.editor.btns.length);
130         
131         if (this.editor.btns.length > 0) {
132             Roo.log('btn length here');
133         }
134             
135         btn('bold',false,true);
136         btn('italic',false,true);
137         btn('align-left', 'justifyleft',true);
138         btn('align-center', 'justifycenter',true);
139         btn('align-right' , 'justifyright',true);
140         btn('link', false, false, function(btn) {
141             //Roo.log("create link?");
142             var url = prompt(this.createLinkText, this.defaultLinkValue);
143             if(url && url != 'http:/'+'/'){
144                 this.editorcore.relayCmd('createlink', url);
145             }
146         }),
147         btn('list','insertunorderedlist',true);
148         btn('pencil', false,true, function(btn){
149                 Roo.log(this);
150                 
151                 this.toggleSourceEdit(btn.pressed);
152         });
153         /*
154         var cog = {
155                 xtype: 'Button',
156                 size : 'sm',
157                 xns: Roo.bootstrap,
158                 glyphicon : 'cog',
159                 //html : 'submit'
160                 menu : {
161                     xtype: 'Menu',
162                     xns: Roo.bootstrap,
163                     items:  []
164                 }
165         };
166         
167         cog.menu.items.push({
168             xtype :'MenuItem',
169             xns: Roo.bootstrap,
170             html : Clean styles,
171             tagname : f,
172             listeners : {
173                 click : function()
174                 {
175                     editorcore.insertTag(this.tagname);
176                     editor.focus();
177                 }
178             }
179             
180         });
181        */
182         
183          
184        this.xtype = 'NavSimplebar';
185         
186         for(var i=0;i< children.length;i++) {
187             
188             this.buttons.add(this.addxtypeChild(children[i]));
189             
190         }
191         
192         editor.on('editorevent', this.updateToolbar, this);
193     },
194     onBtnClick : function(id)
195     {
196        this.editorcore.relayCmd(id);
197        this.editorcore.focus();
198     },
199     
200     /**
201      * Protected method that will not generally be called directly. It triggers
202      * a toolbar update by reading the markup state of the current selection in the editor.
203      */
204     updateToolbar: function(){
205
206         if(!this.editorcore.activated){
207             this.editor.onFirstFocus(); // is this neeed?
208             return;
209         }
210
211         var btns = this.buttons; 
212         var doc = this.editorcore.doc;
213         btns.get('bold').setActive(doc.queryCommandState('bold'));
214         btns.get('italic').setActive(doc.queryCommandState('italic'));
215         //btns.get('underline').setActive(doc.queryCommandState('underline'));
216         
217         btns.get('align-left').setActive(doc.queryCommandState('justifyleft'));
218         btns.get('align-center').setActive(doc.queryCommandState('justifycenter'));
219         btns.get('align-right').setActive(doc.queryCommandState('justifyright'));
220         
221         //btns[frameId + '-insertorderedlist').setActive(doc.queryCommandState('insertorderedlist'));
222         btns.get('list').setActive(doc.queryCommandState('insertunorderedlist'));
223          /*
224         
225         var ans = this.editorcore.getAllAncestors();
226         if (this.formatCombo) {
227             
228             
229             var store = this.formatCombo.store;
230             this.formatCombo.setValue("");
231             for (var i =0; i < ans.length;i++) {
232                 if (ans[i] && store.query('tag',ans[i].tagName.toLowerCase(), false).length) {
233                     // select it..
234                     this.formatCombo.setValue(ans[i].tagName.toLowerCase());
235                     break;
236                 }
237             }
238         }
239         
240         
241         
242         // hides menus... - so this cant be on a menu...
243         Roo.bootstrap.MenuMgr.hideAll();
244         */
245         Roo.bootstrap.MenuMgr.hideAll();
246         //this.editorsyncValue();
247     },
248     onFirstFocus: function() {
249         this.buttons.each(function(item){
250            item.enable();
251         });
252     },
253     toggleSourceEdit : function(sourceEditMode){
254         
255           
256         if(sourceEditMode){
257             Roo.log("disabling buttons");
258            this.buttons.each( function(item){
259                 if(item.cmd != 'pencil'){
260                     item.disable();
261                 }
262             });
263           
264         }else{
265             Roo.log("enabling buttons");
266             if(this.editorcore.initialized){
267                 this.buttons.each( function(item){
268                     item.enable();
269                 });
270             }
271             
272         }
273         Roo.log("calling toggole on editor");
274         // tell the editor that it's been pressed..
275         this.editor.toggleSourceEdit(sourceEditMode);
276        
277     }
278 });
279
280
281
282