Code

Extensions. Compressed+media export improvements (see Bug #386664, Gather Resources...
[inkscape.git] / share / extensions / inkweb.js
1 /*
2 **  InkWeb - Inkscape's Javscript features for the open vector web
3 **
4 **  Copyright (C) 2009 Aurelio A. Heckert, aurium (a) gmail dot com
5 **
6 **  ********* Bugs and New Fetures *************************************
7 **   If you found any bug on this script or if you want to propose a
8 **   new feature, please report it in the inkscape bug traker
9 **   https://bugs.launchpad.net/inkscape/+filebug
10 **   and assign that to Aurium.
11 **  ********************************************************************
12 **
13 **  This program is free software: you can redistribute it and/or modify
14 **  it under the terms of the GNU Lesser General Public License as published
15 **  by the Free Software Foundation, either version 3 of the License, or
16 **  (at your option) any later version.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU Lesser General Public License for more details.
22 **
23 **  You should have received a copy of the GNU Lesser General Public License
24 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 */
27 var InkWeb = {
29   version: 0.04,
31   NS: {
32     svg:      "http://www.w3.org/2000/svg",
33     sodipodi: "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
34     inkscape: "http://www.inkscape.org/namespaces/inkscape",
35     cc:       "http://creativecommons.org/ns#",
36     dc:       "http://purl.org/dc/elements/1.1/",
37     rdf:      "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
38     xlink:    "http://www.w3.org/1999/xlink",
39     xml:      "http://www.w3.org/XML/1998/namespace"
40   }
42 };
44 InkWeb.el = function (tag, attributes) {
45   // A helper to create SVG elements
46   var element = document.createElementNS( this.NS.svg, tag );
47   for ( var att in attributes ) {
48     switch ( att ) {
49       case "parent":
50         attributes.parent.appendChild( element );
51         break;
52       case "text":
53         element.appendChild( document.createTextNode( attributes.text ) );
54         break;
55       default:
56         element.setAttribute( att, attributes[att] );
57     }
58   }
59   return element;
60 }
62 InkWeb.reGetStyleAttVal = function (att) {
63   return new RegExp( "(^|.*;)[ ]*"+ att +":([^;]*)(;.*|$)" )
64 }
66 InkWeb.getStyle = function (el, att) {
67   // This method is needed because el.style is only working
68   // to HTML style in the Firefox 3.0
69   if ( typeof(el) == "string" )
70     el = document.getElementById(el);
71   var style = el.getAttribute("style");
72   var match = this.reGetStyleAttVal(att).exec(style);
73   if ( match ) {
74     return match[2];
75   } else {
76     return false;
77   }
78 }
80 InkWeb.setStyle = function (el, att, val) {
81   if ( typeof(el) == "string" )
82     el = document.getElementById(el);
83   var style = el.getAttribute("style");
84   re = this.reGetStyleAttVal(att);
85   if ( re.test(style) ) {
86     style = style.replace( re, "$1"+ att +":"+ val +"$3" );
87   } else {
88     style += ";"+ att +":"+ val;
89   }
90   el.setAttribute( "style", style );
91   return val
92 }
94 InkWeb.transmitAtt = function (conf) {
95   conf.att = conf.att.split( /\s+/ );
96   if ( typeof(conf.from) == "string" )
97     conf.from = document.getElementById( conf.from );
98   if ( ! conf.to.join )
99     conf.to = [ conf.to ];
100   for ( var toEl,elN=0; toEl=conf.to[elN]; elN++ ) {
101     if ( typeof(toEl) == "string" )
102       toEl = document.getElementById( toEl );
103     for ( var i=0; i<conf.att.length; i++ ) {
104       var val = this.getStyle( conf.from, conf.att[i] );
105       if ( val ) {
106         this.setStyle( toEl, conf.att[i], val );
107       } else {
108         val = conf.from.getAttribute(conf.att[i]);
109         toEl.setAttribute( conf.att[i], val );
110       }
111     }
112   }
115 InkWeb.setAtt = function (conf) {
116   if ( ! conf.el.join )
117     conf.to = [ conf.el ];
118   conf.att = conf.att.split( /\s+/ );
119   conf.val = conf.val.split( /\s+/ );
120   for ( var el,elN=0; el=conf.el[elN]; elN++ ) {
121     if ( typeof(el) == "string" )
122       el = document.getElementById( el );
123     for ( var att,i=0; att=conf.att[i]; i++ ) {
124       if (
125            att == "width"  ||
126            att == "height" ||
127            att == "x"  ||
128            att == "y"  ||
129            att == "cx" ||
130            att == "cy" ||
131            att == "r"  ||
132            att == "rx" ||
133            att == "ry" ||
134            att == "transform"
135          ) {
136         el.setAttribute( att, conf.val[i] );
137       } else {
138         this.setStyle( el, att, conf.val[i] );
139       }
140     }
141   }
144 InkWeb.moveElTo = function (startConf) {
145   if ( typeof(startConf) == "string" ) {
146     // startConf may be only a element Id, to timeout recursive calls.
147     var el = document.getElementById( startConf );
148   } else {
149     if ( typeof(startConf.el) == "string" )
150       startConf.el = document.getElementById( startConf.el );
151     var el = startConf.el;
152   }
153   if ( ! el.inkWebMoving ) {
154     el.inkWebMoving = {
155       step: 0
156     };
157   }
158   var conf = el.inkWebMoving;
159   if ( conf.step == 0 ) {
160     conf.x = startConf.x;
161     conf.y = startConf.y;
162     // dur : duration of the animation in seconds
163     if ( startConf.dur ) { conf.dur = startConf.dur }
164     else { conf.dur = 1 }
165     // steps : animation steps in a second
166     if ( startConf.stepsBySec ) { conf.stepsBySec = startConf.stepsBySec }
167     else { conf.stepsBySec = 16 }
168     conf.sleep = Math.round( 1000 / conf.stepsBySec );
169     conf.steps = conf.dur * conf.stepsBySec;
170     var startPos = el.getBBox();
171     conf.xInc = ( conf.x - startPos.x ) / conf.steps;
172     conf.yInc = ( conf.y - startPos.y ) / conf.steps;
173     conf.transform = el.transform.baseVal.consolidate();
174     if ( ! conf.transform ) {
175       conf.transform = el.ownerSVGElement.createSVGTransform();
176     }
177     el.transform.baseVal.clear();
178     el.transform.baseVal.appendItem(conf.transform);
179   }
180   if ( conf.step < conf.steps ) {
181     conf.step++;
182     conf.transform.matrix.e += conf.xInc;
183     conf.transform.matrix.f += conf.yInc;
184     try{ el.ownerSVGElement.forceRedraw() }
185     catch(e){ this.log(e, "this "+el.ownerSVGElement+" has no forceRedraw().") }
186     conf.timeout = setTimeout( 'InkWeb.moveElTo("'+el.id+'")', conf.sleep );
187   } else {
188     delete el.inkWebMoving;
189   }
192 InkWeb.log = function () { /* if you need that, use the inkweb-debug.js too */ }