sync
[roojs1] / Roo / htmleditor / Block.js
1  
2 /**
3  * @class Roo.htmleditor.Block
4  * Base class for html editor blocks - do not use it directly .. extend it..
5  * @cfg {DomElement} node The node to apply stuff to.
6  * @cfg {String} friendly_name the name that appears in the context bar about this block
7  * @cfg {Object} Context menu - see Roo.form.HtmlEditor.ToolbarContext
8  
9  * @constructor
10  * Create a new Filter.
11  * @param {Object} config Configuration options
12  */
13
14 Roo.htmleditor.Block  = function(cfg)
15 {
16     // do nothing .. should not be called really.
17 }
18
19 Roo.htmleditor.Block.factory = function(node)
20 {
21     
22     var id = Roo.get(node).id;
23     if (typeof(Roo.htmleditor.Block.cache[id]) != 'undefined') {
24         Roo.htmleditor.Block.cache[id].readElement();
25         return Roo.htmleditor.Block.cache[id];
26     }
27     
28     var cls = Roo.htmleditor['Block' + node.getAttribute('data-block')];
29     if (typeof(cls) == 'undefined') {
30         Roo.log("OOps missing block : " + 'Block' + node.getAttribute('data-block'));
31         return false;
32     }
33     Roo.htmleditor.Block.cache[id] = new cls({ node: node });
34     return Roo.htmleditor.Block.cache[id];  /// should trigger update element
35 };
36 // question goes here... do we need to clear out this cache sometimes?
37 // or show we make it relivant to the htmleditor.
38 Roo.htmleditor.Block.cache = {};
39
40 Roo.htmleditor.Block.prototype = {
41     
42     node : false,
43     
44      // used by context menu
45     friendly_name : 'Image with caption',
46     
47     context : false,
48     /**
49      * Update a node with values from this object
50      * @param {DomElement} node
51      */
52     updateElement : function(node)
53     {
54         Roo.DomHelper.update(node === undefined ? this.node : node, this.toObject());
55     },
56      /**
57      * convert to plain HTML for calling insertAtCursor..
58      */
59     toHTML : function()
60     {
61         return Roo.DomHelper.markup(this.toObject());
62     },
63     /**
64      * used by readEleemnt to extract data from a node
65      * may need improving as it's pretty basic
66      
67      * @param {DomElement} node
68      * @param {String} tag - tag to find, eg. IMG ?? might be better to use DomQuery ?
69      * @param {String} attribute (use html - for contents, or style for using next param as style)
70      * @param {String} style the style property - eg. text-align
71      */
72     getVal : function(node, tag, attr, style)
73     {
74         var n = node;
75         if (tag !== true && n.tagName != tag.toUpperCase()) {
76             // in theory we could do figure[3] << 3rd figure? or some more complex search..?
77             // but kiss for now.
78             n = node.getElementsByTagName(tag).item(0);
79         }
80         if (attr == 'html') {
81             return n.innerHTML;
82         }
83         if (attr == 'style') {
84             return n.style[style]
85         }
86         
87         return Roo.get(n).attr(attr);
88             
89     },
90     /**
91      * create a DomHelper friendly object - for use with 
92      * Roo.DomHelper.markup / overwrite / etc..
93      * (override this)
94      */
95     toObject : function()
96     {
97         return {};
98     },
99       /**
100      * Read a node that has a 'data-block' property - and extract the values from it.
101      * @param {DomElement} node - the node
102      */
103     readElement : function(node)
104     {
105         
106     } 
107     
108     
109 };
110