8fbd5ce72d9eed051f099c2aad0ddc91eb5b55d7
[roojs1] / Roo / bootstrap / HtmlEditor / ToolbarStandard.js
1   
2
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     
52     bar : true,
53     
54     editor : false,
55     editorcore : false,
56     
57     
58     formats : [
59         "p" ,  
60         "h1","h2","h3","h4","h5","h6", 
61         "pre", "code", 
62         "abbr", "acronym", "address", "cite", "samp", "var",
63         'div','span'
64     ],
65     
66     onRender : function(ct, position)
67     {
68        // Roo.log("Call onRender: " + this.xtype);
69         
70        Roo.bootstrap.HtmlEditor.ToolbarStandard.superclass.onRender.call(this, ct, position);
71        Roo.log(this.el);
72        this.el.dom.style.marginBottom = '0';
73        var _this = this;
74        var editorcore = this.editorcore;
75        var editor= this.editor;
76        
77        var children = [];
78        var btn = function(id,cmd , toggle, handler){
79        
80             var  event = toggle ? 'toggle' : 'click';
81        
82             var a = {
83                 size : 'sm',
84                 xtype: 'Button',
85                 xns: Roo.bootstrap,
86                 glyphicon : id,
87                 cmd : id || cmd,
88                 enableToggle:toggle !== false,
89                 //html : 'submit'
90                 pressed : toggle ? false : null,
91                 listeners : {}
92             }
93             a.listeners[toggle ? 'toggle' : 'click'] = function() {
94                 handler ? handler.call(_this,this) :_this.onBtnClick.call(_this, cmd ||  id);
95             }
96             children.push(a);
97             return a;
98        }
99         
100         var style = {
101                 xtype: 'Button',
102                 size : 'sm',
103                 xns: Roo.bootstrap,
104                 glyphicon : 'font',
105                 //html : 'submit'
106                 menu : {
107                     xtype: 'Menu',
108                     xns: Roo.bootstrap,
109                     items:  []
110                 }
111         };
112         Roo.each(this.formats, function(f) {
113             style.menu.items.push({
114                 xtype :'MenuItem',
115                 xns: Roo.bootstrap,
116                 html : '<'+ f+' style="margin:2px">'+f +'</'+ f+'>',
117                 tagname : f,
118                 listeners : {
119                     click : function()
120                     {
121                         editorcore.insertTag(this.tagname);
122                         editor.focus();
123                     }
124                 }
125                 
126             });
127         });
128          children.push(style);   
129             
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                 
147                 this.toggleSourceEdit(btn.pressed);
148         });
149         /*
150         var cog = {
151                 xtype: 'Button',
152                 size : 'sm',
153                 xns: Roo.bootstrap,
154                 glyphicon : 'cog',
155                 //html : 'submit'
156                 menu : {
157                     xtype: 'Menu',
158                     xns: Roo.bootstrap,
159                     items:  []
160                 }
161         };
162         
163         cog.menu.items.push({
164             xtype :'MenuItem',
165             xns: Roo.bootstrap,
166             html : Clean styles,
167             tagname : f,
168             listeners : {
169                 click : function()
170                 {
171                     editorcore.insertTag(this.tagname);
172                     editor.focus();
173                 }
174             }
175             
176         });
177        */
178         
179          
180        this.xtype = 'NavSimplebar';
181         
182         for(var i=0;i< children.length;i++) {
183             
184             this.buttons.add(this.addxtypeChild(children[i]));
185             
186         }
187         
188         editor.on('editorevent', this.updateToolbar, this);
189     },
190     onBtnClick : function(id)
191     {
192        this.editorcore.relayCmd(id);
193        this.editorcore.focus();
194     },
195     
196     /**
197      * Protected method that will not generally be called directly. It triggers
198      * a toolbar update by reading the markup state of the current selection in the editor.
199      */
200     updateToolbar: function(){
201
202         if(!this.editorcore.activated){
203             this.editor.onFirstFocus(); // is this neeed?
204             return;
205         }
206
207         var btns = this.buttons; 
208         var doc = this.editorcore.doc;
209         btns.get('bold').setActive(doc.queryCommandState('bold'));
210         btns.get('italic').setActive(doc.queryCommandState('italic'));
211         //btns.get('underline').setActive(doc.queryCommandState('underline'));
212         
213         btns.get('align-left').setActive(doc.queryCommandState('justifyleft'));
214         btns.get('align-center').setActive(doc.queryCommandState('justifycenter'));
215         btns.get('align-right').setActive(doc.queryCommandState('justifyright'));
216         
217         //btns[frameId + '-insertorderedlist').setActive(doc.queryCommandState('insertorderedlist'));
218         btns.get('list').setActive(doc.queryCommandState('insertunorderedlist'));
219          /*
220         
221         var ans = this.editorcore.getAllAncestors();
222         if (this.formatCombo) {
223             
224             
225             var store = this.formatCombo.store;
226             this.formatCombo.setValue("");
227             for (var i =0; i < ans.length;i++) {
228                 if (ans[i] && store.query('tag',ans[i].tagName.toLowerCase(), false).length) {
229                     // select it..
230                     this.formatCombo.setValue(ans[i].tagName.toLowerCase());
231                     break;
232                 }
233             }
234         }
235         
236         
237         
238         // hides menus... - so this cant be on a menu...
239         Roo.bootstrap.MenuMgr.hideAll();
240         */
241         Roo.bootstrap.MenuMgr.hideAll();
242         //this.editorsyncValue();
243     },
244     onFirstFocus: function() {
245         this.buttons.each(function(item){
246            item.enable();
247         });
248     },
249     toggleSourceEdit : function(sourceEditMode){
250         
251           
252         if(sourceEditMode){
253             Roo.log("disabling buttons");
254            this.buttons.each( function(item){
255                 if(item.cmd != 'pencil'){
256                     item.disable();
257                 }
258             });
259           
260         }else{
261             Roo.log("enabling buttons");
262             if(this.editorcore.initialized){
263                 this.buttons.each( function(item){
264                     item.enable();
265                 });
266             }
267             
268         }
269         Roo.log("calling toggole on editor");
270         // tell the editor that it's been pressed..
271         this.editor.toggleSourceEdit(sourceEditMode);
272        
273     }
274 });
275
276
277
278