cab4323696dcf2d698b70bf7677e37734c5b889b
[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     cls : '',
36     href: '',
37     video_url : '',
38     
39     // margin: '2%', not used
40     
41     text_align: 'left', //   (left|right) alignment for the text caption default left. - not used at present
42
43     
44     // used by context menu
45     friendly_name : 'Image with caption',
46     deleteTitle : "Delete Image and Caption",
47     
48     contextMenu : function(toolbar)
49     {
50         
51         var block = function() {
52             return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode);
53         };
54         
55         
56         var rooui =  typeof(Roo.bootstrap) == 'undefined' ? Roo : Roo.bootstrap;
57         
58         var syncValue = toolbar.editorcore.syncValue;
59         
60         var fields = {};
61         
62         return [
63              {
64                 xtype : 'TextItem',
65                 text : "Source: ",
66                 xns : rooui.Toolbar  //Boostrap?
67             },
68             {
69                 xtype : 'Button',
70                 text: 'Edit Src',
71                  
72                 listeners : {
73                     click: function (btn, state)
74                     {
75                         
76                         
77                         Roo.MessageBox.show({
78                             title : "Image Source URL",
79                             msg : "Enter the url for the image",
80                             buttons: this.OKCANCEL,
81                             fn: function(val){
82                                 block().image_src = val;
83                                 block().updateElement();
84                                 syncValue();
85                                 toolbar.editorcore.onEditorEvent();
86                             },
87                             minWidth:250,
88                             prompt:true,
89                             //multiline: multiline,
90                             modal : true,
91                             value : block().image_src
92                         });
93                     }
94                 },
95                 xns : rooui.Toolbar
96             },
97          
98             {
99                 xtype : 'TextField',
100                 allowBlank : false,
101                 width : 150,
102                 name : 'image_src',
103                 listeners : {
104                     keyup : function (combo, e)
105                     { 
106                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
107                         var b = block();
108                         b.image_src = this.getValue();
109                         b.updateElement();
110                         syncValue();
111                         toolbar.editorcore.onEditorEvent();
112                     }
113                 },
114                 xns : rooui.form
115                 
116             },
117             {
118                 xtype : 'TextItem',
119                 text : "Width: ",
120                 xns : rooui.Toolbar  //Boostrap?
121             },
122             {
123                 xtype : 'ComboBox',
124                 allowBlank : false,
125                 displayField : 'val',
126                 editable : true,
127                 listWidth : 100,
128                 triggerAction : 'all',
129                 typeAhead : true,
130                 valueField : 'val',
131                 width : 70,
132                 name : 'width',
133                 listeners : {
134                     select : function (combo, r, index)
135                     {
136                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
137                         var b = block();
138                         b.width = r.get('val');
139                         b.updateElement();
140                         syncValue();
141                         toolbar.editorcore.onEditorEvent();
142                     }
143                 },
144                 xns : rooui.form,
145                 store : {
146                     xtype : 'SimpleStore',
147                     data : [
148                         ['auto'],
149                         ['50%'],
150                         ['100%']
151                     ],
152                     fields : [ 'val'],
153                     xns : Roo.data
154                 }
155             },
156             {
157                 xtype : 'TextItem',
158                 text : "Align: ",
159                 xns : rooui.Toolbar  //Boostrap?
160             },
161             {
162                 xtype : 'ComboBox',
163                 allowBlank : false,
164                 displayField : 'val',
165                 editable : true,
166                 listWidth : 100,
167                 triggerAction : 'all',
168                 typeAhead : true,
169                 valueField : 'val',
170                 width : 70,
171                 name : 'align',
172                 listeners : {
173                     select : function (combo, r, index)
174                     {
175                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
176                         var b = block();
177                         b.align = r.get('val');
178                         b.updateElement();
179                         syncValue();
180                         toolbar.editorcore.onEditorEvent();
181                     }
182                 },
183                 xns : rooui.form,
184                 store : {
185                     xtype : 'SimpleStore',
186                     data : [
187                         ['left'],
188                         ['right'],
189                         ['center']
190                     ],
191                     fields : [ 'val'],
192                     xns : Roo.data
193                 }
194             },
195             
196             
197             {
198                 xtype : 'Button',
199                 text: 'Hide Caption',
200                 name : 'caption_display',
201                 pressed : false,
202                 enableToggle : true,
203                 setValue : function(v) {
204                     this.toggle(v == 'block' ? false : true);
205                 },
206                 listeners : {
207                     toggle: function (btn, state)
208                     {
209                         var b  = block();
210                         b.caption_display = b.caption_display == 'block' ? 'none' : 'block';
211                         this.setText(b.caption_display == 'block' ? "Hide Caption" : "Show Caption");
212                         b.updateElement();
213                         syncValue();
214                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
215                         toolbar.editorcore.onEditorEvent();
216                     }
217                 },
218                 xns : rooui.Toolbar
219             }
220         ];
221         
222     },
223     /**
224      * create a DomHelper friendly object - for use with
225      * Roo.DomHelper.markup / overwrite / etc..
226      */
227     toObject : function()
228     {
229         var d = document.createElement('div');
230         d.innerHTML = this.caption;
231         
232         var m = this.width == '50%' && this.align == 'center' ? '0 auto' : 0; 
233         
234         var img =   {
235             tag : 'img',
236             contenteditable : 'false',
237             src : this.image_src,
238             alt : d.innerText.replace(/\n/g, " ").replace(/\s+/g, ' ').trim(), // removeHTML and reduce spaces..
239             style: {
240                 width : 'auto',
241                 'max-width': '100%',
242                 margin : '0px' 
243                 
244                 
245             }
246         };
247         /*
248         '<div class="{0}" width="420" height="315" src="{1}" frameborder="0" allowfullscreen>' +
249                     '<a href="{2}">' + 
250                         '<img class="{0}-thumbnail" src="{3}/Images/{4}/{5}#image-{4}" />' + 
251                     '</a>' + 
252                 '</div>',
253         */
254                 
255         if (this.href.length > 0) {
256             img = {
257                 tag : 'a',
258                 href: this.href,
259                 contenteditable : 'true',
260                 cn : [
261                     img
262                 ]
263             };
264         }
265         
266         
267         if (this.video_url.length > 0) {
268             img = {
269                 tag : 'div',
270                 cls : this.cls,
271                 frameborder : 0,
272                 allowfullscreen : true,
273                 width : 420,  // these are for video tricks - that we replace the outer
274                 height : 315,
275                 src : this.video_url,
276                 cn : [
277                     img
278                 ]
279             };
280         }
281         
282         return  {
283             tag: 'figure',
284             'data-block' : 'Figure',
285             contenteditable : 'false',
286             style : {
287                 display: 'block',
288                 float :  this.align ,
289                 'max-width':  this.width,
290                 width : 'auto',
291                 margin:  m,
292                 padding: '10px'
293                 
294             },
295            
296             
297             align : this.align,
298             cn : [
299                 img,
300               
301                 {
302                     tag: 'figcaption',
303                     contenteditable : true,
304                     style : {
305                         'text-align': 'left',
306                         'margin-top' : '16px',
307                         'font-size' : '16px',
308                         'line-height' : '24px',
309                         'font-style': 'italic',
310                         display : this.caption_display
311                     },
312                     cls : this.cls.length > 0 ? (this.cls  + '-thumbnail' ) : '',
313                     html : this.caption
314                     
315                 }
316             ]
317         };
318          
319     },
320     
321     readElement : function(node)
322     {
323         // this should not really come from the link...
324         this.video_url = this.getVal(node, 'div', 'src');
325         this.cls = this.getVal(node, 'div', 'class');
326         this.href = this.getVal(node, 'a', 'href');
327         
328         this.image_src = this.getVal(node, 'img', 'src');
329          
330         this.align = this.getVal(node, 'figure', 'align');
331         this.caption = this.getVal(node, 'figcaption', 'html');
332         //this.text_align = this.getVal(node, 'figcaption', 'style','text-align');
333         this.width = this.getVal(node, 'figure', 'style', 'max-width');
334         //this.margin = this.getVal(node, 'figure', 'style', 'margin');
335         
336     },
337     removeNode : function()
338     {
339         return this.node;
340     }
341     
342   
343    
344      
345     
346     
347     
348     
349 })
350