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