Roo/htmleditor/BlockFigure.js
[roojs1] / Roo / htmleditor / BlockFigure.js
1  
2
3 /**
4  * @class Roo.htmleditor.BlockFigure
5  * Block that has an image and a figcaption
6  * @cfg {String} image_src the url for the image
7  * @cfg {String} align (left|right) alignment for the block default left
8  * @cfg {String} caption the text to appear below  (and in the alt tag)
9  * @cfg {String} caption_display (block|none) display or not the caption
10  * @cfg {String|number} image_width the width of the image number or %?
11  * @cfg {String|number} image_height the height of the image number or %?
12  * 
13  * @constructor
14  * Create a new Filter.
15  * @param {Object} config Configuration options
16  */
17
18 Roo.htmleditor.BlockFigure = function(cfg)
19 {
20     if (cfg.node) {
21         this.readElement(cfg.node);
22         this.updateElement(cfg.node);
23     }
24     Roo.apply(this, cfg);
25 }
26 Roo.extend(Roo.htmleditor.BlockFigure, Roo.htmleditor.Block, {
27  
28     
29     // setable values.
30     image_src: '',
31     align: 'center',
32     caption : '',
33     caption_display : 'block',
34     width : '100%',
35     
36     // margin: '2%', not used
37     
38     text_align: 'left', //   (left|right) alignment for the text caption default left. - not used at present
39
40     
41     // used by context menu
42     friendly_name : 'Image with caption',
43     deleteTitle : "Delete Image and Caption",
44     
45     contextMenu : function(toolbar)
46     {
47         
48         var block = function() {
49             return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode);
50         };
51         
52         
53         var rooui =  typeof(Roo.bootstrap) == 'undefined' ? Roo : Roo.bootstrap;
54         
55         var syncValue = toolbar.editorcore.syncValue;
56         
57         var fields = {};
58         
59         return [
60              {
61                 xtype : 'TextItem',
62                 text : "Source: ",
63                 xns : rooui.Toolbar  //Boostrap?
64             },
65             {
66                 xtype : 'TextField',
67                 allowBlank : false,
68                 width : 150,
69                 name : 'image_src',
70                 listeners : {
71                     keyup : function (combo, e)
72                     { 
73                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
74                         var b = block();
75                         b.image_src = this.getValue();
76                         b.updateElement();
77                         syncValue();
78                         toolbar.editorcore.onEditorEvent();
79                     }
80                 },
81                 xns : rooui.form
82                 
83             },
84             {
85                 xtype : 'TextItem',
86                 text : "Width: ",
87                 xns : rooui.Toolbar  //Boostrap?
88             },
89             {
90                 xtype : 'ComboBox',
91                 allowBlank : false,
92                 displayField : 'val',
93                 editable : true,
94                 listWidth : 100,
95                 triggerAction : 'all',
96                 typeAhead : true,
97                 valueField : 'val',
98                 width : 70,
99                 name : 'width',
100                 listeners : {
101                     select : function (combo, r, index)
102                     {
103                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
104                         var b = block();
105                         b.width = r.get('val');
106                         b.updateElement();
107                         syncValue();
108                         toolbar.editorcore.onEditorEvent();
109                     }
110                 },
111                 xns : rooui.form,
112                 store : {
113                     xtype : 'SimpleStore',
114                     data : [
115                         ['auto'],
116                         ['50%'],
117                         ['100%']
118                     ],
119                     fields : [ 'val'],
120                     xns : Roo.data
121                 }
122             },
123             {
124                 xtype : 'TextItem',
125                 text : "Align: ",
126                 xns : rooui.Toolbar  //Boostrap?
127             },
128             {
129                 xtype : 'ComboBox',
130                 allowBlank : false,
131                 displayField : 'val',
132                 editable : true,
133                 listWidth : 100,
134                 triggerAction : 'all',
135                 typeAhead : true,
136                 valueField : 'val',
137                 width : 70,
138                 name : 'align',
139                 listeners : {
140                     select : function (combo, r, index)
141                     {
142                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
143                         var b = block();
144                         b.align = r.get('val');
145                         b.updateElement();
146                         syncValue();
147                         toolbar.editorcore.onEditorEvent();
148                     }
149                 },
150                 xns : rooui.form,
151                 store : {
152                     xtype : 'SimpleStore',
153                     data : [
154                         ['left'],
155                         ['right'],
156                         ['center']
157                     ],
158                     fields : [ 'val'],
159                     xns : Roo.data
160                 }
161             },
162             
163             
164             {
165                 xtype : 'Button',
166                 text: 'Hide Caption',
167                 name : 'caption_display',
168                 pressed : false,
169                 enableToggle : true,
170                 setValue : function(v) {
171                     this.toggle(v == 'block' ? false : true);
172                 },
173                 listeners : {
174                     toggle: function (btn, state)
175                     {
176                         var b  = block();
177                         b.caption_display = b.caption_display == 'block' ? 'none' : 'block';
178                         this.setText(b.caption_display == 'block' ? "Hide Caption" : "Show Caption");
179                         b.updateElement();
180                         syncValue();
181                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
182                         toolbar.editorcore.onEditorEvent();
183                     }
184                 },
185                 xns : rooui.Toolbar
186             }
187         ];
188         
189     },
190     /**
191      * create a DomHelper friendly object - for use with
192      * Roo.DomHelper.markup / overwrite / etc..
193      */
194     toObject : function()
195     {
196         var d = document.createElement('div');
197         d.innerHTML = this.caption;
198         
199         var m = this.width == '50%' && this.align == 'center' ? '0 auto' : 0; 
200         
201         return  {
202             tag: 'figure',
203             'data-block' : 'Figure',
204             contenteditable : 'false',
205             style : {
206                 display: 'block',
207                 float :  this.align ,
208                 'max-width':  this.width,
209                 width : 'auto',
210                 margin:  m,
211                 padding: '10px'
212                 
213             },
214             align : this.align,
215             cn : [
216                 {
217                     tag : 'img',
218                     src : this.image_src,
219                     alt : d.innerText.replace(/\n/g, " "), // removeHTML..
220                     style: {
221                         width : 'auto',
222                         'max-width': '100%',
223                         margin : '0px' 
224                         
225                         
226                     }
227                 },
228                 {
229                     tag: 'figcaption',
230                     contenteditable : true,
231                     style : {
232                         'text-align': 'left',
233                         'margin-top' : '16px',
234                         'font-size' : '16px',
235                         'line-height' : '24px',
236                         'font-style': 'italic',
237                         display : this.caption_display
238                     },
239                     html : this.caption
240                     
241                 }
242             ]
243         };
244          
245     },
246     
247     readElement : function(node)
248     {
249         this.image_src = this.getVal(node, 'img', 'src');
250         this.align = this.getVal(node, 'figure', 'align');
251         this.caption = this.getVal(node, 'figcaption', 'html');
252         //this.text_align = this.getVal(node, 'figcaption', 'style','text-align');
253         this.width = this.getVal(node, 'figure', 'style', 'max-width');
254         //this.margin = this.getVal(node, 'figure', 'style', 'margin');
255         
256     },
257     removeNode : function()
258     {
259         return this.node;
260     }
261     
262   
263    
264      
265     
266     
267     
268     
269 })
270