Code

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