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