roojs-ui.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     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: 'Change Image URL',
71                  
72                 listeners : {
73                     click: function (btn, state)
74                     {
75                         var b = block();
76                         
77                         Roo.MessageBox.show({
78                             title : "Image Source URL",
79                             msg : "Enter the url for the image",
80                             buttons: Roo.MessageBox.OKCANCEL,
81                             fn: function(btn, val){
82                                 if (btn != 'ok') {
83                                     return;
84                                 }
85                                 b.image_src = val;
86                                 b.updateElement();
87                                 syncValue();
88                                 toolbar.editorcore.onEditorEvent();
89                             },
90                             minWidth:250,
91                             prompt:true,
92                             //multiline: multiline,
93                             modal : true,
94                             value : b.image_src
95                         });
96                     }
97                 },
98                 xns : rooui.Toolbar
99             },
100          
101             {
102                 xtype : 'Button',
103                 text: 'Change Link URL',
104                  
105                 listeners : {
106                     click: function (btn, state)
107                     {
108                         var b = block();
109                         
110                         Roo.MessageBox.show({
111                             title : "Link URL",
112                             msg : "Enter the url for the link - leave blank to have no link",
113                             buttons: Roo.MessageBox.OKCANCEL,
114                             fn: function(btn, val){
115                                 if (btn != 'ok') {
116                                     return;
117                                 }
118                                 b.href = val;
119                                 b.updateElement();
120                                 syncValue();
121                                 toolbar.editorcore.onEditorEvent();
122                             },
123                             minWidth:250,
124                             prompt:true,
125                             //multiline: multiline,
126                             modal : true,
127                             value : b.href
128                         });
129                     }
130                 },
131                 xns : rooui.Toolbar
132             },
133             {
134                 xtype : 'Button',
135                 text: 'Show Video URL',
136                  
137                 listeners : {
138                     click: function (btn, state)
139                     {
140                         Roo.MessageBox.alert("Video URL",
141                             block().video_url == '' ? 'This image is not linked ot a video' :
142                                 'The image is linked to: <a target="_new" href="' + block().video_url + '">' + block().video_url + '</a>');
143                     }
144                 },
145                 xns : rooui.Toolbar
146             },
147             
148             
149             {
150                 xtype : 'TextItem',
151                 text : "Width: ",
152                 xns : rooui.Toolbar  //Boostrap?
153             },
154             {
155                 xtype : 'ComboBox',
156                 allowBlank : false,
157                 displayField : 'val',
158                 editable : true,
159                 listWidth : 100,
160                 triggerAction : 'all',
161                 typeAhead : true,
162                 valueField : 'val',
163                 width : 70,
164                 name : 'width',
165                 listeners : {
166                     select : function (combo, r, index)
167                     {
168                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
169                         var b = block();
170                         b.width = r.get('val');
171                         b.updateElement();
172                         syncValue();
173                         toolbar.editorcore.onEditorEvent();
174                     }
175                 },
176                 xns : rooui.form,
177                 store : {
178                     xtype : 'SimpleStore',
179                     data : [
180                         ['auto'],
181                         ['50%'],
182                         ['80%'],
183                         ['100%']
184                     ],
185                     fields : [ 'val'],
186                     xns : Roo.data
187                 }
188             },
189             {
190                 xtype : 'TextItem',
191                 text : "Align: ",
192                 xns : rooui.Toolbar  //Boostrap?
193             },
194             {
195                 xtype : 'ComboBox',
196                 allowBlank : false,
197                 displayField : 'val',
198                 editable : true,
199                 listWidth : 100,
200                 triggerAction : 'all',
201                 typeAhead : true,
202                 valueField : 'val',
203                 width : 70,
204                 name : 'align',
205                 listeners : {
206                     select : function (combo, r, index)
207                     {
208                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
209                         var b = block();
210                         b.align = r.get('val');
211                         b.updateElement();
212                         syncValue();
213                         toolbar.editorcore.onEditorEvent();
214                     }
215                 },
216                 xns : rooui.form,
217                 store : {
218                     xtype : 'SimpleStore',
219                     data : [
220                         ['left'],
221                         ['right'],
222                         ['center']
223                     ],
224                     fields : [ 'val'],
225                     xns : Roo.data
226                 }
227             },
228             
229             
230             {
231                 xtype : 'Button',
232                 text: 'Hide Caption',
233                 name : 'caption_display',
234                 pressed : false,
235                 enableToggle : true,
236                 setValue : function(v) {
237                     // this trigger toggle.
238                      
239                     this.setText(v ? "Hide Caption" : "Show Caption");
240                     this.setPressed(v);
241                 },
242                 listeners : {
243                     toggle: function (btn, state)
244                     {
245                         var b  = block();
246                         b.caption_display = b.caption_display == 'block' ? 'none' : 'block';
247                         this.setText(b.caption_display == 'block' ? "Hide Caption" : "Show Caption");
248                         b.updateElement();
249                         syncValue();
250                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
251                         toolbar.editorcore.onEditorEvent();
252                     }
253                 },
254                 xns : rooui.Toolbar
255             }
256         ];
257         
258     },
259     /**
260      * create a DomHelper friendly object - for use with
261      * Roo.DomHelper.markup / overwrite / etc..
262      */
263     toObject : function()
264     {
265         var d = document.createElement('div');
266         d.innerHTML = this.caption;
267         
268         var m = this.width != '100%' && this.align == 'center' ? '0 auto' : 0; 
269         
270         var iw = this.align == 'center' ? this.width : '100%';
271         var img =   {
272             tag : 'img',
273             contenteditable : 'false',
274             src : this.image_src,
275             alt : d.innerText.replace(/\n/g, " ").replace(/\s+/g, ' ').trim(), // removeHTML and reduce spaces..
276             style: {
277                 width : iw,
278                 maxWidth : iw + ' !important', // this is not getting rendered?
279                 margin : m  
280                 
281             }
282         };
283         /*
284         '<div class="{0}" width="420" height="315" src="{1}" frameborder="0" allowfullscreen>' +
285                     '<a href="{2}">' + 
286                         '<img class="{0}-thumbnail" src="{3}/Images/{4}/{5}#image-{4}" />' + 
287                     '</a>' + 
288                 '</div>',
289         */
290                 
291         if (this.href.length > 0) {
292             img = {
293                 tag : 'a',
294                 href: this.href,
295                 contenteditable : 'true',
296                 cn : [
297                     img
298                 ]
299             };
300         }
301         
302         
303         if (this.video_url.length > 0) {
304             img = {
305                 tag : 'div',
306                 cls : this.cls,
307                 frameborder : 0,
308                 allowfullscreen : true,
309                 width : 420,  // these are for video tricks - that we replace the outer
310                 height : 315,
311                 src : this.video_url,
312                 cn : [
313                     img
314                 ]
315             };
316         }
317         // we remove caption totally if its hidden... - will delete data.. but otherwise we end up with fake caption
318         var captionhtml = this.caption_display == 'none' ? '' : (this.caption.length ? this.caption : "Caption");
319         
320   
321         var ret =   {
322             tag: 'figure',
323             'data-block' : 'Figure',
324             
325             contenteditable : 'false',
326             
327             style : {
328                 display: 'block',
329                 float :  this.align ,
330                 maxWidth :  this.align == 'center' ? '100% !important' : (this.width + ' !important'),
331                 width : this.align == 'center' ? '100%' : this.width,
332                 margin:  '0px',
333                 padding: this.align == 'center' ? '0' : '0 10px' ,
334                 textAlign : this.align   // seems to work for email..
335                 
336             },
337            
338             
339             align : this.align,
340             cn : [
341                 img,
342               
343                 {
344                     tag: 'figcaption',
345                     'data-display' : this.caption_display,
346                     style : {
347                         textAlign : 'left',
348                         fontSize : '16px',
349                         lineHeight : '24px',
350                         display : this.caption_display,
351                         maxWidth : this.width + ' !important',
352                         margin: m,
353                         width: this.width
354                     
355                          
356                     },
357                     cls : this.cls.length > 0 ? (this.cls  + '-thumbnail' ) : '',
358                     cn : [
359                         {
360                             tag: 'div',
361                             style  : {
362                                 marginTop : '16px',
363                                 textAlign : 'left'
364                             },
365                             align: 'left',
366                             cn : [
367                                 {
368                                     // we can not rely on yahoo syndication to use CSS elements - so have to use  '<i>' to encase stuff.
369                                     tag : 'i',
370                                     contenteditable : true,
371                                     html : captionhtml
372                                 }
373                                 
374                             ]
375                         }
376                         
377                     ]
378                     
379                 }
380             ]
381         };
382         return ret;
383          
384     },
385     
386     readElement : function(node)
387     {
388         // this should not really come from the link...
389         this.video_url = this.getVal(node, 'div', 'src');
390         this.cls = this.getVal(node, 'div', 'class');
391         this.href = this.getVal(node, 'a', 'href');
392         
393         
394         this.image_src = this.getVal(node, 'img', 'src');
395          
396         this.align = this.getVal(node, 'figure', 'align');
397         var figcaption = this.getVal(node, 'figcaption', false);
398         this.caption = this.getVal(figcaption, 'i', 'html');
399
400         this.caption_display = this.getVal(node, 'figcaption', 'data-display');
401         //this.text_align = this.getVal(node, 'figcaption', 'style','text-align');
402         this.width = this.getVal(node, 'figcaption', 'style', 'width');
403         //this.margin = this.getVal(node, 'figure', 'style', 'margin');
404         
405     },
406     removeNode : function()
407     {
408         return this.node;
409     }
410     
411   
412    
413      
414     
415     
416     
417     
418 })
419