fix caption display and pasting
[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                         ['100%']
183                     ],
184                     fields : [ 'val'],
185                     xns : Roo.data
186                 }
187             },
188             {
189                 xtype : 'TextItem',
190                 text : "Align: ",
191                 xns : rooui.Toolbar  //Boostrap?
192             },
193             {
194                 xtype : 'ComboBox',
195                 allowBlank : false,
196                 displayField : 'val',
197                 editable : true,
198                 listWidth : 100,
199                 triggerAction : 'all',
200                 typeAhead : true,
201                 valueField : 'val',
202                 width : 70,
203                 name : 'align',
204                 listeners : {
205                     select : function (combo, r, index)
206                     {
207                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
208                         var b = block();
209                         b.align = r.get('val');
210                         b.updateElement();
211                         syncValue();
212                         toolbar.editorcore.onEditorEvent();
213                     }
214                 },
215                 xns : rooui.form,
216                 store : {
217                     xtype : 'SimpleStore',
218                     data : [
219                         ['left'],
220                         ['right'],
221                         ['center']
222                     ],
223                     fields : [ 'val'],
224                     xns : Roo.data
225                 }
226             },
227             
228             
229             {
230                 xtype : 'Button',
231                 text: 'Hide Caption',
232                 name : 'caption_display',
233                 pressed : false,
234                 enableToggle : true,
235                 setValue : function(v) {
236                     this.toggle(v == 'block' ? false : true);
237                 },
238                 listeners : {
239                     toggle: function (btn, state)
240                     {
241                         var b  = block();
242                         b.caption_display = b.caption_display == 'block' ? 'none' : 'block';
243                         this.setText(b.caption_display == 'block' ? "Hide Caption" : "Show Caption");
244                         b.updateElement();
245                         syncValue();
246                         toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
247                         toolbar.editorcore.onEditorEvent();
248                     }
249                 },
250                 xns : rooui.Toolbar
251             }
252         ];
253         
254     },
255     /**
256      * create a DomHelper friendly object - for use with
257      * Roo.DomHelper.markup / overwrite / etc..
258      */
259     toObject : function()
260     {
261         var d = document.createElement('div');
262         d.innerHTML = this.caption;
263         
264         var m = this.width == '50%' && this.align == 'center' ? '0 auto' : 0; 
265         
266         var img =   {
267             tag : 'img',
268             contenteditable : 'false',
269             src : this.image_src,
270             alt : d.innerText.replace(/\n/g, " ").replace(/\s+/g, ' ').trim(), // removeHTML and reduce spaces..
271             style: {
272                 width : 'auto',
273                 'max-width': '100%',
274                 margin : '0px' 
275                 
276                 
277             }
278         };
279         /*
280         '<div class="{0}" width="420" height="315" src="{1}" frameborder="0" allowfullscreen>' +
281                     '<a href="{2}">' + 
282                         '<img class="{0}-thumbnail" src="{3}/Images/{4}/{5}#image-{4}" />' + 
283                     '</a>' + 
284                 '</div>',
285         */
286                 
287         if (this.href.length > 0) {
288             img = {
289                 tag : 'a',
290                 href: this.href,
291                 contenteditable : 'true',
292                 cn : [
293                     img
294                 ]
295             };
296         }
297         
298         
299         if (this.video_url.length > 0) {
300             img = {
301                 tag : 'div',
302                 cls : this.cls,
303                 frameborder : 0,
304                 allowfullscreen : true,
305                 width : 420,  // these are for video tricks - that we replace the outer
306                 height : 315,
307                 src : this.video_url,
308                 cn : [
309                     img
310                 ]
311             };
312         }
313         // we remove caption totally if its hidden... - will delete data.. but otherwise we end up with fake caption
314         var captionhtml = this.caption_display == 'none' ? '' : (this.caption.length ? this.caption : "Caption");
315         
316         return  {
317             tag: 'figure',
318             'data-block' : 'Figure',
319             contenteditable : 'false',
320             
321             style : {
322                 display: 'block',
323                 float :  this.align ,
324                 'max-width':  this.width,
325                 width : 'auto',
326                 margin:  m,
327                 padding: '10px'
328                 
329             },
330            
331             
332             align : this.align,
333             cn : [
334                 img,
335               
336                 {
337                     tag: 'figcaption',
338                     'data-display' : this.caption_display,
339                     style : {
340                         'text-align': 'left',
341                       
342                         'font-size' : '16px',
343                         'line-height' : '24px',
344                          display : this.caption_display
345                     },
346                     cls : this.cls.length > 0 ? (this.cls  + '-thumbnail' ) : '',
347                     cn : [
348                         {
349                             tag: 'div',
350                             style  : {
351                                 'margin-top' : '16px'
352                             },
353                             align: 'left',
354                             cn : [
355                                 {
356                                     // we can not rely on yahoo syndication to use CSS elements - so have to use  '<i>' to encase stuff.
357                                     tag : 'i',
358                                     contenteditable : true,
359                                     html : captionhtml
360                                 }
361                                 
362                             ]
363                         }
364                         
365                     ]
366                     
367                 }
368             ]
369         };
370          
371     },
372     
373     readElement : function(node)
374     {
375         // this should not really come from the link...
376         this.video_url = this.getVal(node, 'div', 'src');
377         this.cls = this.getVal(node, 'div', 'class');
378         this.href = this.getVal(node, 'a', 'href');
379         
380         
381         this.image_src = this.getVal(node, 'img', 'src');
382          
383         this.align = this.getVal(node, 'figure', 'align');
384         this.caption = this.getVal(node, 'figcaption', 'html');
385         // remove '<i>
386         if (this.caption.trim().match(/^<i[^>]*>/i)) {
387             this.caption = this.caption.trim().replace(/^<i[^>]*>/i, '').replace(/^<\/i>$/i, '');
388         }
389         this.caption_display = this.getVal(node, 'figcaption', 'data-display');
390         //this.text_align = this.getVal(node, 'figcaption', 'style','text-align');
391         this.width = this.getVal(node, 'figure', 'style', 'max-width');
392         //this.margin = this.getVal(node, 'figure', 'style', 'margin');
393         
394     },
395     removeNode : function()
396     {
397         return this.node;
398     }
399     
400   
401    
402      
403     
404     
405     
406     
407 })
408