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