commit
[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, html){
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 : html || '',
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 cb_box = function...
100         
101         var style = {
102                 xtype: 'Button',
103                 size : 'sm',
104                 xns: Roo.bootstrap,
105                 glyphicon : 'font',
106                 //html : 'submit'
107                 menu : {
108                     xtype: 'Menu',
109                     xns: Roo.bootstrap,
110                     items:  []
111                 }
112         };
113         Roo.each(this.formats, function(f) {
114             style.menu.items.push({
115                 xtype :'MenuItem',
116                 xns: Roo.bootstrap,
117                 html : '<'+ f+' style="margin:2px">'+f +'</'+ f+'>',
118                 tagname : f,
119                 listeners : {
120                     click : function()
121                     {
122                         editorcore.insertTag(this.tagname);
123                         editor.focus();
124                     }
125                 }
126                 
127             });
128         });
129         children.push(style);   
130         
131         btn('bold',false,true);
132         btn('italic',false,true);
133         btn('align-left', 'justifyleft',true);
134         btn('align-center', 'justifycenter',true);
135         btn('align-right' , 'justifyright',true);
136         btn('link', false, false, function(btn) {
137             //Roo.log("create link?");
138             var url = prompt(this.createLinkText, this.defaultLinkValue);
139             if(url && url != 'http:/'+'/'){
140                 this.editorcore.relayCmd('createlink', url);
141             }
142         }),
143         btn('list','insertunorderedlist',true);
144         btn('pencil', false,true, function(btn){
145                 Roo.log(this);
146                 this.toggleSourceEdit(btn.pressed);
147         });
148         
149         if (this.editor.btns.length > 0) {
150             for (var i = 0; i<this.editor.btns.length; i++) {
151                 children.push(this.editor.btns[i]);
152             }
153         }
154         
155         /*
156         var cog = {
157                 xtype: 'Button',
158                 size : 'sm',
159                 xns: Roo.bootstrap,
160                 glyphicon : 'cog',
161                 //html : 'submit'
162                 menu : {
163                     xtype: 'Menu',
164                     xns: Roo.bootstrap,
165                     items:  []
166                 }
167         };
168         
169         cog.menu.items.push({
170             xtype :'MenuItem',
171             xns: Roo.bootstrap,
172             html : Clean styles,
173             tagname : f,
174             listeners : {
175                 click : function()
176                 {
177                     editorcore.insertTag(this.tagname);
178                     editor.focus();
179                 }
180             }
181             
182         });
183        */
184         
185          
186        this.xtype = 'NavSimplebar';
187         
188         for(var i=0;i< children.length;i++) {
189             
190             this.buttons.add(this.addxtypeChild(children[i]));
191             
192         }
193         
194         editor.on('editorevent', this.updateToolbar, this);
195     },
196     onBtnClick : function(id)
197     {
198        this.editorcore.relayCmd(id);
199        this.editorcore.focus();
200     },
201     
202     /**
203      * Protected method that will not generally be called directly. It triggers
204      * a toolbar update by reading the markup state of the current selection in the editor.
205      */
206     updateToolbar: function(){
207
208         if(!this.editorcore.activated){
209             this.editor.onFirstFocus(); // is this neeed?
210             return;
211         }
212
213         var btns = this.buttons; 
214         var doc = this.editorcore.doc;
215         btns.get('bold').setActive(doc.queryCommandState('bold'));
216         btns.get('italic').setActive(doc.queryCommandState('italic'));
217         //btns.get('underline').setActive(doc.queryCommandState('underline'));
218         
219         btns.get('align-left').setActive(doc.queryCommandState('justifyleft'));
220         btns.get('align-center').setActive(doc.queryCommandState('justifycenter'));
221         btns.get('align-right').setActive(doc.queryCommandState('justifyright'));
222         
223         //btns[frameId + '-insertorderedlist').setActive(doc.queryCommandState('insertorderedlist'));
224         btns.get('list').setActive(doc.queryCommandState('insertunorderedlist'));
225          /*
226         
227         var ans = this.editorcore.getAllAncestors();
228         if (this.formatCombo) {
229             
230             
231             var store = this.formatCombo.store;
232             this.formatCombo.setValue("");
233             for (var i =0; i < ans.length;i++) {
234                 if (ans[i] && store.query('tag',ans[i].tagName.toLowerCase(), false).length) {
235                     // select it..
236                     this.formatCombo.setValue(ans[i].tagName.toLowerCase());
237                     break;
238                 }
239             }
240         }
241         
242         
243         
244         // hides menus... - so this cant be on a menu...
245         Roo.bootstrap.MenuMgr.hideAll();
246         */
247         Roo.bootstrap.MenuMgr.hideAll();
248         //this.editorsyncValue();
249     },
250     onFirstFocus: function() {
251         this.buttons.each(function(item){
252            item.enable();
253         });
254     },
255     toggleSourceEdit : function(sourceEditMode){
256         
257           
258         if(sourceEditMode){
259             Roo.log("disabling buttons");
260            this.buttons.each( function(item){
261                 if(item.cmd != 'pencil'){
262                     item.disable();
263                 }
264             });
265           
266         }else{
267             Roo.log("enabling buttons");
268             if(this.editorcore.initialized){
269                 this.buttons.each( function(item){
270                     item.enable();
271                 });
272             }
273             
274         }
275         Roo.log("calling toggole on editor");
276         // tell the editor that it's been pressed..
277         this.editor.toggleSourceEdit(sourceEditMode);
278        
279     }
280 });
281
282
283
284