Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (hide annotations) (download) (as text)
Fri Aug 6 08:02:11 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 86596 byte(s)
TextEditor class appended 4
1 isao-hara 2 /*
2 isao-hara 31 SvgEditor.js
3 isao-hara 2
4     iSlideMaker
5     http://sourceforge.jp/projects/islidemaker/simple/
6    
7     Copyright(c) 2010, Isao Hara, isao-hara@users.sourceforge.jp
8    
9     All rights reserved. This program is made available under the terms of the
10     Eclipse Public License v1.0 which accompanies this distribution, and is
11     available at http://www.eclipse.org/legal/epl-v10.html
12    
13     Contributors: Isao Hara.
14     */
15     var systemDB;
16     var currentMode;
17     var editarea_w=80;
18     var editarea_h=10;
19     var MainTitle="Simple SVG Editor";
20    
21 isao-hara 68 var Config;
22    
23 isao-hara 2 var preview=null;
24     var targetItem=null;
25     var selectedItems=new Array();
26     var sx=0;
27     var sy=0;
28     var ex=0;
29     var ey=0;
30     var modeSVG=null;
31     var iSlideMgr='iSlideManager.php';
32     var nextId=1;
33    
34     var lineEdit=null;
35 isao-hara 24
36 isao-hara 2 ////// SVG
37     var svg_ns = 'http://www.w3.org/2000/svg';
38     var svg_x=10;
39     var svg_y=10;
40     var svg_width = 100;
41     var svg_height = 50;
42     var svg_rx = 50;
43     var svg_ry = 30;
44     var svg_lw = 1;
45     var svg_wo = null;
46     var svg_ho = null;
47     var svg_ro = null;
48     var svg_rxo = null;
49     var svg_ryo = null;
50     var svg_fsize = null;
51     var svg_scale_dir = null;
52    
53     var svg_top = null;
54     var svg_select = null;
55     var svg_defs = null;
56    
57 isao-hara 25 var svg_font_size = 24;
58     var svg_font_family = 'Helvetica';
59     var svg_color = '#000000';
60     var svg_fill_color = '#ffffff';
61     var svg_line_width = 1;
62    
63 isao-hara 27 var editingTextObj = null;
64 isao-hara 46 var svg_rotate_locked = false;
65     var svg_scale_locked = false;
66 isao-hara 27
67 isao-hara 42 var dupItems;
68     var dupX;
69     var dupY;
70 isao-hara 56 var is_newPath;
71 isao-hara 42
72     var firstTouch = new Date();
73    
74 isao-hara 58 var lineW = new Array(0.25,0.5,0.75,1,1.5,2.25,3,4.5,6);
75     var StrokeDash = new Array("", "2", "6 2", "6 2 1 2", "8 2", "8 2 1 2", "8 2 1 2 1 2" );
76    
77 isao-hara 67 var remote_only = false;
78    
79 isao-hara 14 ///// Color
80     var colors=new Array('none', '#ffffff',
81     '#000000', '#3f3f3f', '#7f7f7f', '#bfbfbf',
82     '#ff0000', '#ff7f00', '#ffff00', '#7fff00',
83     '#00ff00', '#00ff7f', '#00ffff', '#007fff',
84     '#0000ff', '#7f00ff', '#ff00ff', '#ff007f',
85     '#7f0000', '#7f3f00', '#7f7f00', '#3f7f00',
86     '#007f00', '#007f3f', '#007f7f', '#003f7f',
87     '#00007f', '#3f007f', '#7f007f', '#7f003f',
88     '#7f0000', '#7f3f00', '#7f7f00', '#3f7f00',
89     '#ffaaaa', '#ffd4aa', '#ffffaa', '#d4ffaa',
90     '#aaffaa', '#aaffd4', '#aaffff', '#aad4ff',
91     '#aaaaff', '#d4aaff', '#ffaaff', '#ffaad4'
92     );
93    
94 isao-hara 70 var Editor;
95    
96 isao-hara 2 //// Initialize
97     function initEditor(name, dispname, size){
98 isao-hara 70 systemDB = (new TextEditor("Editor")).initDB(name, dispname, size);
99 isao-hara 2 fileSelector();
100     preview=document.getElementById('preview');
101     if(preview){
102     preview.addEventListener("touchstart", onTouchStart,false);
103     preview.addEventListener("touchmove", onTouchMove, false);
104     preview.addEventListener("touchend", onTouchEnd, false);
105     preview.addEventListener("gesturestart", onGestureStart,false);
106     preview.addEventListener("gesturechange", onGestureChange, false);
107     preview.addEventListener("gestureend", onGestureEnd, false);
108     }
109 isao-hara 68 Config = new Configuration();
110     Config.restoreValues();
111 isao-hara 2 }
112    
113 isao-hara 42 //// localStorage
114 isao-hara 68 function Configuration(){
115     this.MgrPath = "";
116     }
117    
118     Configuration.prototype.restoreValues = function(){
119 isao-hara 42 if(typeof(localStorage) == 'undefined'){ alert('local storage not suported'); }
120 isao-hara 68 this.MgrPath = localStorage.getItem('MgrPath') ? localStorage.getItem('MgrPath') : "";
121     window.onbeforeuload=function(){ return this.saveChanges(); }
122 isao-hara 2 }
123 isao-hara 68 Configuration.prototype.clearAll = function(){
124 isao-hara 2 localsStorage.clear();
125 isao-hara 68 this.restoreValudes();
126 isao-hara 2 }
127 isao-hara 68 Configuration.prototype.saveChanges = function(){
128     localStorage.setItem('MgrPath', this.MgrPath);
129 isao-hara 2 }
130    
131     // initialize a database
132 isao-hara 70 function TextEditor(name){
133     this.db=null;
134     this.name=name;
135     eval(name+"= this;");
136     return this;
137     }
138    
139     TextEditor.prototype.initDB=function(name, dispname, size){
140 isao-hara 2 try {
141     if (!window.openDatabase) {
142 isao-hara 67 remote_only = true;
143     alert('not supported');
144     return null;
145 isao-hara 2 } else {
146 isao-hara 67 var version = '1.0';
147 isao-hara 70 this.db = openDatabase(name, version, dispname, size);
148     this.createTables();
149     return this.db;
150 isao-hara 2 }
151 isao-hara 42 }catch(e){
152     if (e == INVALID_STATE_ERR){ alert("Invalid database version."); }else{ alert("Unknown error "+e+"."); }
153 isao-hara 2 return null;
154     }
155     }
156    
157 isao-hara 70 TextEditor.prototype.createTables=function(){
158     this.db.transaction(
159 isao-hara 2 function (transaction) {
160 isao-hara 70 transaction.executeSql('CREATE TABLE IF NOT EXISTS files(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, filedata_id INTEGER NOT NULL, deleted INTEGER NOT NULL DEFAULT 0);', [], this.nullDataHandler, this.killTransaction);
161 isao-hara 73 transaction.executeSql('CREATE TABLE IF NOT EXISTS filedata(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, datablob BLOB NOT NULL DEFAULT "");', [], TextEditor.nullDataHandler, TextEditor.errorHandler);
162 isao-hara 2 }
163     );
164     }
165    
166 isao-hara 70 TextEditor.prototype.dropTable=function(){
167     this.db.transaction(
168 isao-hara 42 function (transaction) {
169     transaction.executeSql('DROP TABLE files;');
170     transaction.executeSql('DROP TABLE filedata;');
171     }
172     );
173 isao-hara 2 }
174     /// Create New File
175 isao-hara 70 TextEditor.prototype.reallyCreateNewFileAction=function(name){
176     this.db.transaction(
177 isao-hara 72 new Function("transaction",
178     "transaction.executeSql('INSERT INTO filedata (datablob) VALUES (\"\");', [],"+
179     "function(transaction,results){"+
180     "transaction.executeSql('INSERT INTO files (name, filedata_id) VALUES (?, ?);',"+
181 isao-hara 73 "['"+name+"',results.insertId],TextEditor.nullDataHandler,TextEditor.killTransaction);},"+
182     "TextEditor.errorHandler);"));
183 isao-hara 2 fileSelector();
184     }
185    
186 isao-hara 70 TextEditor.prototype.createNewFileAction=function(){
187 isao-hara 2 var name = document.getElementById('createFilename').value
188     if(name == "") {
189 isao-hara 72 if (confirm('Filename is required, try agein?')) { this.createNewFile(); }else{ fileSelector(); }
190 isao-hara 2 return;
191     }
192 isao-hara 70 this.db.transaction(
193 isao-hara 2 new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+name+"'],"+
194     "function (transaction, results) {"+
195     "if(results.rows.length == 0){"+
196 isao-hara 70 this.name+".reallyCreateNewFileAction('"+name+"');"+
197 isao-hara 2 "}else{"+
198     "if (confirm(results.rows.item(0)['name']+' already exist, try agein?')) {"+
199 isao-hara 72 this.name+".createNewFile();"+
200 isao-hara 2 "}else{"+
201     "fileSelector();"+
202     "}"+
203     "}"+
204 isao-hara 73 "}, TextEditor.errorHandler);")
205 isao-hara 2 );
206     }
207    
208     //// Delete File
209 isao-hara 70 TextEditor.prototype.deleteUpdateResults=function(transaction, results){
210     if (results.rowsAffected) { fileSelector(); }
211 isao-hara 2 }
212    
213 isao-hara 70 TextEditor.prototype.reallyDelete=function(id){
214     this.db.transaction(
215 isao-hara 2 new Function("transaction",
216 isao-hara 73 "transaction.executeSql('DELETE FROM files where id=?;', ["+id+"], null, TextEditor.errorHandler);"
217 isao-hara 70 +"transaction.executeSql('DELETE FROM filedata where id=?;',["+id+"],"+this.name+".deleteUpdateResults, "+
218 isao-hara 73 "TextEditor.errorHandler);")
219 isao-hara 2 );
220     }
221    
222 isao-hara 70 TextEditor.prototype.deleteFile=function(name){
223     this.db.transaction(
224 isao-hara 2 new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+name+"'],"+
225     "function (transaction, results) {"+
226     "if (confirm('Really delete '+results.rows.item(0)['name']+'?')) {"+
227 isao-hara 70 this.name+".reallyDelete(results.rows.item(0)['id']);"+
228 isao-hara 2 "}"+
229 isao-hara 73 "}, TextEditor.errorHandler);")
230 isao-hara 2 );
231     }
232    
233 isao-hara 70 TextEditor.prototype.reallyDeleteAll=function(){
234     this.transaction(
235 isao-hara 2 new Function("transaction",
236 isao-hara 73 "transaction.executeSql('DELETE FROM files;',[],null,TextEditor.errorHandler);"
237     +"transaction.executeSql('DELETE FROM filedata;',[],"+this.name+".deleteUpdateResults,TextEditor.errorHandler);")
238 isao-hara 2 );
239     }
240    
241 isao-hara 70 TextEditor.prototype.deleteAllFile=function(){
242     if (confirm('Really delete all file?')) { this.reallyDeleteAll(); }
243 isao-hara 2 }
244    
245     ///// Rename
246 isao-hara 70 TextEditor.prototype.reallyRenameFileAction=function(id, newname){
247     this.db.transaction(
248 isao-hara 2 function (transaction) {
249 isao-hara 73 transaction.executeSql('UPDATE files set name=? where id=?;', [newname,id ], null,TextEditor.errorHandler);
250 isao-hara 2 }
251     );
252     fileSelector();
253     }
254    
255 isao-hara 70 TextEditor.prototype.renameFileAction=function(){
256     var new_name = document.getElementById('newFilename').value;
257     var id = document.getElementById('fileId').value;
258 isao-hara 2
259 isao-hara 70 if(new_name == "") { alert('Filename is required.'); fileSelector(); return; }
260     this.db.transaction(
261 isao-hara 51 new Function("transaction",
262     "transaction.executeSql('SELECT id,name from files where name=?;',['"+new_name+"'],"+
263 isao-hara 2 "function (transaction, results) {"+
264     "if(results.rows.length == 0){"+
265 isao-hara 70 this.name+".reallyRenameFileAction('"+id+"','"+new_name+"');"+
266 isao-hara 2 "}else{"+
267     "alert(results.rows.item(0)['name']+' already exist');"+
268     "fileSelector();"+
269     "}"+
270 isao-hara 73 "},TextEditor.errorHandler);")
271 isao-hara 2 );
272     }
273    
274     /// Save File
275 isao-hara 70 TextEditor.prototype.saveFile=function(){
276 isao-hara 2 var editarea = document.getElementById('editarea');
277     var contents = "";
278     contents = editarea.value;
279    
280 isao-hara 70 this.saveContent(contents);
281 isao-hara 2 }
282    
283 isao-hara 70 TextEditor.prototype.saveData=function(){
284 isao-hara 2 var contents = "";
285     contents = getSVGContent();
286 isao-hara 70 this.saveContent(contents);
287 isao-hara 2 }
288    
289 isao-hara 70 TextEditor.prototype.saveContent=function(contents){
290     if(!contents) { alert("Invalid content"); return; }
291 isao-hara 14
292 isao-hara 70 this.db.transaction(
293 isao-hara 14 function (transaction) {
294     var datadiv = document.getElementById('tempdata');
295     var filedata_id = datadiv.getAttribute('lfdataid');
296    
297     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
298 isao-hara 73 [ contents, filedata_id ], TextEditor.nullDataHandler, TextEditor.errorHandler);
299 isao-hara 14
300     alert('Saved.');
301     }
302     );
303     }
304    
305 isao-hara 70 TextEditor.prototype.reallySaveNewFileAction=function(fname){
306 isao-hara 17 var datadiv = document.getElementById('tempdata');
307     content = datadiv.textContent;
308    
309 isao-hara 70 this.db.transaction(
310 isao-hara 17 function (transaction) {
311 isao-hara 73 var myfunc = new Function("transaction", "results", "transaction.executeSql('INSERT INTO files (name, filedata_id) VALUES (?, ?);', [ '"+fname+"', results.insertId], TextEditor.nullDataHandler, TextEditor.killTransaction);");
312 isao-hara 17
313 isao-hara 73 transaction.executeSql('INSERT INTO filedata (datablob) VALUES (?);', [content], myfunc,TextEditor.errorHandler);
314 isao-hara 17 }
315     );
316     datadiv.textContent="";
317     alert("download to "+fname);
318     }
319    
320 isao-hara 70 TextEditor.prototype.saveContentFilename=function(id){
321 isao-hara 17 var datadiv = document.getElementById('tempdata');
322     datadiv.setAttribute('lfdataid', id);
323 isao-hara 70 this.saveContent(datadiv.innerHTML);
324 isao-hara 17 datadiv.textContent="";
325     }
326    
327 isao-hara 70 TextEditor.prototype.saveContentWithFilename=function(fname, contents){
328 isao-hara 42 if(!contents) { alert("Invalid content"); return; }
329 isao-hara 17
330     var datadiv = document.getElementById('tempdata');
331     datadiv.setAttribute('lfname', fname);
332     datadiv.textContent = contents;
333    
334 isao-hara 70 this.db.transaction(
335 isao-hara 17 new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+fname+"'],"+
336     "function (transaction, results) {"+
337     "if(results.rows.length == 0){"+
338 isao-hara 70 this.name+".reallySaveNewFileAction('"+fname+"');"+
339 isao-hara 17 "}else{"+
340     "if (confirm(results.rows.item(0)['name']+' already exist, Overwrite?')) {"+
341 isao-hara 70 this.name+".saveContentFilename(results.rows.item(0)['id']);"+
342 isao-hara 17 "}else{"+
343     "fileSelector();"+
344     "}"+
345     "}"+
346 isao-hara 73 "}, TextEditor.errorHandler);")
347 isao-hara 17 );
348     }
349    
350 isao-hara 70 TextEditor.prototype.getFile=function(name,func){
351 isao-hara 2 var datadiv = document.getElementById('tempdata');
352     if(datadiv) datadiv.setAttribute('lfname', name);
353    
354 isao-hara 70 this.db.transaction(
355 isao-hara 2 function (transaction) {
356 isao-hara 73 transaction.executeSql('SELECT * from files, filedata where files.name=? and files.filedata_id = filedata.id;', [name], eval(func), TextEditor.errorHandler);
357 isao-hara 2 }
358     );
359     }
360 isao-hara 70
361 isao-hara 71 TextEditor.prototype.showFile=function(name){ this.getFile(name, this.name+'.showFileData'); }
362     TextEditor.prototype.showFileData=function(transaction, results){
363     var data = results.rows.item(0);
364     var filename = data['name'];
365     var filedata = data['datablob'];
366    
367     var datadiv = document.getElementById('tempdata');
368     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
369     document.title=filename;
370    
371     previewFile(filedata);
372    
373     updateShowMenu();
374     setMode('Preview');
375     }
376     /// ErrorHandler
377 isao-hara 70 TextEditor.prototype.killTransaction=function(transaction, error){ return true; }
378     TextEditor.prototype.errorHandler=function(transaction, error){
379 isao-hara 2 alert('Oops. Error was '+error.message+' (Code '+error.code+')');
380    
381 isao-hara 70 this.we_think_this_error_is_fatal = true;
382     if (this.we_think_this_error_is_fatal) return true;
383 isao-hara 2 return false;
384     }
385 isao-hara 70 TextEditor.prototype.nullDataHandler=function(transaction, results){ }
386    
387 isao-hara 71 /// FileList
388     TextEditor.prototype.mkfileList=function(){
389 isao-hara 70 this.db.transaction(
390 isao-hara 71 new Function("transaction",
391     "transaction.executeSql('SELECT * from files where deleted=0;',[],"+
392     "function (transaction, results) {"+
393     "var filelist = '';"+
394     "var menuDiv = document.getElementById('menuDiv');"+
395     "if(menuDiv){"+
396     "for(var i=0;i<results.rows.length;i++){"+
397     "var row = results.rows.item(i);"+
398     "filelist = filelist + "+this.name+".fileEntry(row);"+
399     "}"+
400     "if(!filelist){"+
401     "filelist = 'No files.<br />';"+
402     "}else{"+
403     "filelist = '<center><table class=\"filetable\">'+filelist+'</table></center>';"+
404     "}"+
405     "menuDiv.innerHTML='<H1 class=\"title\">'+MainTitle+'</H1>'+"+this.name+".createMenuBar()+filelist;"+
406     "}"+
407 isao-hara 73 "}, TextEditor.errorHandler);")
408 isao-hara 2 );
409 isao-hara 70 }
410    
411 isao-hara 71 TextEditor.prototype.fileEntry=function(row){
412     var name = row['name'];
413     var files_id = row['id'];
414 isao-hara 70
415 isao-hara 71 var res = "<tr class='filerow'>";
416     res += "<td class='filenamecell' onClick=\""+this.name+".showFile('"+name+"');\">"+name+"</td>";
417     res += "<td class='filelinkcell'>";
418 isao-hara 72 res += "<button class='blue' onClick=\""+this.name+".editFile('"+name+"');\">&nbsp;Edit&nbsp;</button> &nbsp;";
419     res += "<button class='green' onClick=\""+this.name+".renameFile('"+name+"',"+files_id+");\">&nbsp;Rename&nbsp;</button> &nbsp;";
420 isao-hara 71 res += "<button onClick=\"uploadFile('"+name+"');\">Upload</button>";
421     res += "<button class='red' onClick=\""+this.name+".deleteFile('"+name+"');\">Delete</button>";
422     res += "</td></tr>\n";
423 isao-hara 70
424 isao-hara 71 return res;
425     }
426 isao-hara 70
427 isao-hara 71 TextEditor.prototype.createMenuBar=function(){
428     var menu = "<ul id='menubar'>";
429 isao-hara 72 menu += "<li><button onClick='"+this.name+".createNewFile()'>Create New File</button></li>";
430 isao-hara 71 // menu += "<li><button onClick='deleteAllFile()'>Delete All</button></li>";
431     menu += "<li><button onClick='getRemoteFileList()'>File List on a Server</button></li>";
432     menu += "<li><button onClick='configServer()'>Server Configuration</button></li>";
433     menu += "</ul><p class='cls'>";
434     return menu;
435 isao-hara 70 }
436    
437 isao-hara 72 TextEditor.prototype.createNewFile=function(){
438     var popupDiv = document.getElementById('popup');
439     var string = "";
440    
441     string += "<H1 class='title'>Create New File</H1>\n";
442     string += "<div class=\"input_form\">\n";
443     string += "Filename:<input id='createFilename' name='name' value=\"\" />\n";
444     string += "<button onClick=\""+this.name+".createNewFileAction();hideItemById('popup'); \">Create</button>\n";
445     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
446     string += "</div>\n";
447 isao-hara 70
448 isao-hara 72 popupDiv.innerHTML=string;
449     popupDiv.style.display='block';
450     }
451    
452     TextEditor.prototype.renameFile=function(name, id){
453     var popupDiv = document.getElementById('popup');
454     var string = "";
455    
456     string += "<H1 class='title'>Rename File</H1>\n";
457     string += "<div class='input_form'>\n";
458     string += "Old Filename: "+name+"<br>";
459     string += "New Filename:<input id='newFilename' name='newname' value=\"\" />\n";
460     string += "<input type='hidden' id='fileId' value=\""+id+"\" /><br>\n";
461     string += "<button onClick=\""+this.name+".renameFileAction();hideItemById('popup');\">Rename</button>\n";
462     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
463     string += "</div>\n";
464    
465     popupDiv.innerHTML=string;
466     popupDiv.style.display='block';
467     }
468    
469     TextEditor.prototype.editFile=function(name){
470     if(currentMode == 'Preview'){
471     var data = getSVGContent();
472     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+data+"</textarea>\n";
473     updateEditMenu();
474     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
475    
476     setMode('Edit');
477     }else{
478     this.getFile(name, this.name+'.editFileData');
479     }
480     }
481    
482     TextEditor.prototype.editFileData=function(transaction, results){
483     var editDiv = document.getElementById('editDiv');
484     var datadiv = document.getElementById('tempdata');
485    
486     if( results.rows.length == 0) return;
487     var data = results.rows.item(0);
488     var filename = data['name'];
489     var filedata = data['datablob'];
490     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
491    
492     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+filedata+"</textarea>\n";
493    
494     document.title="EditFile: "+filename;
495     updateEditMenu();
496     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
497    
498     setMode('Edit');
499     }
500    
501     TextEditor.prototype.editCurrentFile=function(){
502     var datadiv = document.getElementById('tempdata');
503     var name = datadiv.getAttribute('lfname');
504    
505     this.editFile(name);
506     }
507    
508    
509 isao-hara 70 /////////
510     function fileSelector() {
511     if(!systemDB){
512     getRemoteFileList();
513     }else{
514 isao-hara 71 Editor.mkfileList();
515 isao-hara 70 }
516 isao-hara 2 setMode('List');
517     }
518    
519     function fileSelectorOnServer(val) {
520     var filelist = '';
521     var menuDiv = document.getElementById('menuDiv');
522     var results = val.split(',');
523    
524     if(menuDiv){
525     for(var i=0; i<results.length; i++) {
526     var row = results[i];
527     filelist = filelist + fileEntryOnServer(row);
528     }
529     if (filelist == "") {
530     filelist = "No files.<br />\n";
531     } else {
532     filelist = "<center><table class='filetable'>"+filelist+"</table></center>";
533     }
534     var Menu = "<button onClick=\"fileSelector();\">Local Storage</button>";
535 isao-hara 16 Menu += "<button onClick=\"getRemoteFileList();\">List on Server</button>";
536 isao-hara 2 menuDiv.innerHTML="<H1 class='title'>"+MainTitle+"</H1>"+Menu+filelist;
537     }
538     setMode('List');
539     }
540    
541     function fileEntryOnServer(name){
542     name = name.split('.')[0];
543     var res = "<tr class='filerow'>";
544     res += "<td class='filenamecell' onClick=\"getRemoteFile('"+name+"');\">"+name+"</td>";
545     res += "<td class='filelinkcell'>";
546     res += "<button class='green' onClick=\"showRemoteFile('"+name+"');\">View</button>";
547 isao-hara 16 res += "<button onClick=\"downloadFile('"+name+"');\">Download to local storage</button>";
548     res += "<button onClick=\"downloadToFile('"+name+"');\">Download as SVG File</button>";
549 isao-hara 2 res += "</td></tr>\n";
550    
551     return res;
552     }
553    
554    
555 isao-hara 25 //// Config
556     function setConfig(){
557     var ele = document.getElementById('ServerURL');
558 isao-hara 68 Config.MgrPath=ele.value;
559     Config.saveChanges();
560 isao-hara 25 alert("Done");
561     }
562    
563     function configServer(){
564     var popupDiv = document.getElementById('popup');
565     var string = "";
566    
567     string += "<H1 class='title'>Server Configration</H1>\n";
568     string += "<div class=\"input_form\">\n";
569 isao-hara 68 string += "Filename:<input id='ServerURL' name='url' value=\""+Config.MgrPath+"\" size=\"80\"/><br>\n";
570 isao-hara 25 string += "<button onClick=\"setConfig();hideItemById('popup'); \">Done</button>\n";
571     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
572     string += "</div>\n";
573    
574     popupDiv.innerHTML=string;
575     popupDiv.style.display='block';
576     }
577    
578 isao-hara 73 /////// Access Server
579     function newXMLRequest(){
580     if(this.XMLHttpRequest){
581     return new XMLHttpRequest();
582     }else {
583     return new ActiveXObject("Microsoft.XMLHTTP");
584     }
585     }
586    
587     function createRequestData(data){
588     var str="filetype=svg";
589     for (var i in data){
590     str = str +"&"+ i +"="+encodeURIComponent(data[i]);
591     }
592     return str;
593     }
594    
595     function postRequest(url, data, func){
596     var postData=createRequestData(data);
597     var obj=newXMLRequest();
598    
599     try{
600     obj.onreadystatechange = function(){
601     if (obj.readyState == 4){
602     if (obj.status == 200){
603     func(obj.responseText);
604     }else{
605     alert("Fail to postRequest: status="+obj.status);
606     return;
607     }
608     }
609     }
610     obj.open("POST", url, true);
611     obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
612     obj.send(postData);
613     }catch(e){
614     }
615     }
616    
617     function commnadFinishAlert(s){
618     alert(s);
619     }
620    
621     function uploadFileData(transaction, results){
622     var data = results.rows.item(0);
623     var updata = new Array(0);
624     updata['name']= data['name'];
625     updata['datalob'] = data['datablob'];
626     updata['cmd'] = 'upload'
627    
628     postRequest(Config.MgrPath+iSlideMgr, updata, commnadFinishAlert);
629     }
630    
631     function uploadFile(name){
632     Editor.getFile(name, 'uploadFileData');
633     }
634    
635     function getRemoteFileList(){
636     var data=new Array();
637     data['name'] = "SVG";
638     data['cmd'] = "list";
639     postRequest(Config.MgrPath+iSlideMgr, data, fileSelectorOnServer);
640     }
641    
642     function previewRemoteFile(content){
643     previewFile(content);
644     setMode('Preview');
645     }
646    
647     function getRemoteFile(name){
648     var data=new Array();
649     data['name'] = name;
650     data['cmd'] = "get";
651     postRequest(Config.MgrPath+iSlideMgr, data, previewRemoteFile);
652     }
653    
654     function saveRemoteFile(content){
655     var datadiv = document.getElementById('tempdata');
656     var fname = datadiv.getAttribute('lfname');
657     Editor.saveContentWithFilename(fname, content);
658     }
659    
660     function downloadFile(name){
661     var data=new Array();
662     data['name'] = name;
663     data['cmd'] = "get";
664     var datadiv = document.getElementById('tempdata');
665     datadiv.setAttribute('lfname', name);
666     postRequest(Config.MgrPath+iSlideMgr, data, saveRemoteFile);
667     }
668    
669     function showRemoteFile(name){
670     getRemoteFile(name);
671     }
672    
673     function downloadToFile(name){
674     var downloadForm = "";
675     downloadForm += "<form action=\"iSlideManager.php\" method=\"post\">";
676     downloadForm += "<input type=\"hidden\" name=\"cmd\" value=\"download\">";
677     downloadForm += "<input type=\"hidden\" name=\"name\" value=\""+name+"\">";
678     downloadForm += "<input type=\"hidden\" name=\"filetype\" value=\"svg\">";
679     downloadForm += "<input type=\"submit\">";
680     downloadForm += "</form>";
681    
682     var cmdForm = document.getElementById('cmdForm');
683     cmdForm.innerHTML = downloadForm;
684     cmdForm.firstChild.submit();
685     }
686    
687    
688 isao-hara 2 /////
689     /// EditMenu
690     function insertStr(str, len){
691     var editarea = document.getElementById('editarea');
692     if(len < 0) len = str.length;
693    
694     if (editarea){
695     if(str=='DQ') str='"';
696     var strs = editarea.value;
697     var cPos = editarea.selectionStart;
698     var tmp = strs.substr(0,cPos);
699     editarea.value = tmp +str+strs.substr(cPos, strs.length);
700     cPos += len;
701     editarea.setSelectionRange(cPos, cPos);
702     editarea.focus();
703     }
704     }
705    
706     function insertTag(tag){
707     var str;
708     var len = -1;
709     if (tag == "p"){
710 isao-hara 51 str = "<p> </p>";
711     len = 4;
712 isao-hara 2 }else if (tag == "ul"){
713 isao-hara 51 str = "<ul class=\" \"> \n\n</ul>";
714     len = 17;
715 isao-hara 2 }else if (tag == "li"){
716 isao-hara 51 str = "<li> </li>";
717     len = 5;
718 isao-hara 2 }else if (tag == "href"){
719 isao-hara 51 str = "xlink:href=\"\"";
720     len = 12;
721 isao-hara 2 }else if (tag == "EQ"){
722 isao-hara 51 str = "=\"\"";
723     len = 2;
724 isao-hara 2 }else if (tag == "svg:text"){
725 isao-hara 51 str = "<svg:text x=\"100\" y=\"100\" fill=\"black\" font-size=\"12\"> </svg:text>";
726     len = 58;
727 isao-hara 2 }else if (tag == "svg:rect"){
728 isao-hara 51 str = "<svg:rect x=\"10\" y=\"10\" width=\"100\" height=\"100\" fill=\"white\" stroke=\"black\" />";
729     len = 13;
730 isao-hara 2 }else if (tag == "svg:circle"){
731 isao-hara 51 str = "<svg:circle cx=\"10\" cy=\"100\" r=\"100\" fill=\"white\" stroke=\"black\" strokc-width=\"1\"/>";
732     len = 17;
733 isao-hara 2 }
734     insertStr(str, len);
735     }
736    
737     function editMenuBar()
738     {
739     var str = "";
740     str += "<button onClick=\"insertStr('/',1);\">/</button>\n";
741     str += "<button onClick=\"insertStr('DQ',1);\">\"</button>\n";
742     str += "<button onClick=\"insertTag('EQ',1);\">=\"\"</button>\n";
743     str += "<button onClick=\"insertTag('p');\">p</button>\n";
744     str += "<button onClick=\"insertTag('href');\">href</button>\n";
745    
746     str += "<button onClick=\"insertTag('svg:text');\">TEXT</button>\n";
747     str += "<button onClick=\"insertTag('svg:rect');\">rect</button>\n";
748     str += "<button onClick=\"insertTag('svg:circle');\">circle</button>\n";
749    
750     str += "<button onClick=\"chEditareaHeight();\">...</button>\n";
751    
752     return str;
753     }
754    
755     function updateEditMenu(){
756     var menuDiv = document.getElementById('menuDiv');
757     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
758 isao-hara 71 menuDiv.innerHTML+= "<button onClick=\"Editor.saveFile();fileSelector();\"> Save </button>\n";
759 isao-hara 2 menuDiv.innerHTML+= "<button onClick=\"previewData();\"> Preview </button>\n";
760     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
761    
762     menuDiv.style.display='block';
763     }
764    
765 isao-hara 71 //////////////////////////////
766 isao-hara 51 // Show File (GUI Editor)
767 isao-hara 2 function updateShowMenu(){
768     var menuDiv = document.getElementById('menuDiv');
769     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
770 isao-hara 51 var menu_str = "<img src=\"images/menu.png\" usemap=\"#topmenu\" />\n";
771     menu_str+= "<map name=\"topmenu\">";
772     menu_str+= "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"fileSelector();\">";
773 isao-hara 70 menu_str+= "<area shape=\"rect\" coords=\"30,0,60,25\" onClick=\"Editor.saveData();\">";
774 isao-hara 51 menu_str+= "<area shape=\"rect\" coords=\"60,0,90,25\" onClick=\"showSVGSource();\">";
775     menu_str+= "</map>";
776 isao-hara 41
777 isao-hara 51 menu_str+= updateSVGObjMenu();
778     menuDiv.innerHTML+= menu_str;
779 isao-hara 52 updateStrokeWidth();
780 isao-hara 54 updatePathTypeMenu();
781     updateArrowMenu();
782 isao-hara 2 menuDiv.style.display='block';
783     updateToolBar();
784     }
785    
786 isao-hara 25 function showColorPalette(val){
787     var palette = document.getElementById('color-palette');
788     var ele;
789     var current = palette.getAttribute("targetType");
790 isao-hara 2
791 isao-hara 25 if(palette.style.display=='block' && current == val){
792     palette.style.display='none';
793     return;
794     }
795    
796     if(val == 'fill'){
797     ele = document.getElementById('toolFill');
798     }else{
799     ele = document.getElementById('toolStroke');
800     }
801    
802     var pos= ele.offsetTop + 110;
803     palette.style.top = pos +"px";
804     palette.style.display='block';
805     palette.setAttribute("targetType", val);
806    
807     }
808    
809 isao-hara 51 function selectToolBar(idx){
810 isao-hara 64 clearSelectedItems();
811 isao-hara 51 var ele=document.getElementById('tool_select');
812     var pos = idx *25;
813     ele.style.top= pos+'px';
814     }
815    
816    
817 isao-hara 2 function updateToolBar(){
818     var toolbar = document.getElementById('toolBar');
819 isao-hara 25 var str = "";
820 isao-hara 14 if(!toolbar.innerHTML){
821 isao-hara 51 str += "<li><img src=\"images/tools.png\" usemap=\"#toolbar\" />\n";
822     str += "<img id=\"tool_select\" src=\"images/select.png\" style=\"position:absolute;top:0;left:10px;\" />\n";
823     str += "<map name=\"toolbar\">";
824     str += "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"setSVGMode('selector');\">";
825     str += "<area shape=\"rect\" coords=\"0,25,30,50\" onClick=\"setSVGMode('newPath');\">";
826     str += "<area shape=\"rect\" coords=\"0,50,30,75\" onClick=\"setSVGMode('newLine');\">";
827     str += "<area shape=\"rect\" coords=\"0,75,30,100\" onClick=\"setSVGMode('text');\">";
828     str += "<area shape=\"rect\" coords=\"0,100,30,125\" onClick=\"setSVGMode('rect');\">";
829     str += "<area shape=\"rect\" coords=\"0,125,30,150\" onClick=\"setSVGMode('circle');\">";
830     str += "<area shape=\"rect\" coords=\"0,150,30,175\" onClick=\"setSVGMode('ellipse');\">";
831     str += "<area shape=\"rect\" coords=\"0,175,30,200\" onClick=\"setSVGMode('image');\">";
832     str += "<area shape=\"rect\" coords=\"0,210,30,235\" onClick=\"showColorPalette('fill');\">";
833     str += "<area shape=\"rect\" coords=\"0,240,30,265\" onClick=\"showColorPalette('stroke');\">";
834     str += "<area shape=\"rect\" coords=\"0,280,30,305\" onClick=\"topItem();\">";
835     str += "<area shape=\"rect\" coords=\"0,305,30,330\" onClick=\"upItem();\">";
836     str += "<area shape=\"rect\" coords=\"0,330,30,355\" onClick=\"downItem();\">";
837     str += "<area shape=\"rect\" coords=\"0,355,30,380\" onClick=\"bottomItem();\">";
838     str += "<area shape=\"rect\" coords=\"0,390,30,415\" onClick=\"dupObject();\">";
839     str += "<area shape=\"rect\" coords=\"0,415,30,440\" onClick=\"delSVGObj();\">";
840 isao-hara 62 str += "<area shape=\"rect\" coords=\"0,450,30,470\" onClick=\"setRotLock();\">";
841     str += "<area shape=\"rect\" coords=\"0,470,30,490\" onClick=\"setScaleLock();\">";
842 isao-hara 51 str += "</map>";
843 isao-hara 62 str += "<img src=\"images/lock.png\" onClick=\"toggleRotateScaleLock();\" id=\"lock\" style=\"display:none;z-index:100;position:absolute;\"/>\n";
844 isao-hara 51 str += "<div id=\"toolFill\"></div>\n";
845     str += "<div id=\"toolStroke\"></div>\n";
846     str += "</li>\n";
847 isao-hara 25 toolbar.innerHTML= "<ul>"+str+"</ul>";
848 isao-hara 14 }
849 isao-hara 2 toolbar.style.display='block';
850     }
851    
852     function setMode(m){
853     currentMode=m;
854     switch(m){
855     case 'List':
856     hideItemById('editDiv');
857     hideItemById('preview');
858     hideItemById('popup');
859     hideItemById('toolBar');
860 isao-hara 14 hideItemById('color-palette');
861 isao-hara 2 showItemById('menuDiv');
862     break;
863     case 'Edit':
864     hideItemById('preview');
865     hideItemById('popup');
866     hideItemById('toolBar');
867 isao-hara 14 hideItemById('color-palette');
868 isao-hara 2 showItemById('editDiv');
869     showItemById('menuDiv');
870     break;
871     case 'Preview':
872     showItemById('preview');
873     hideItemById('editDiv');
874     hideItemById('popup');
875 isao-hara 25 hideItemById('color-palette');
876 isao-hara 2 showItemById('menuDiv');
877     showItemById('toolBar');
878     break;
879     default:
880     break;
881     }
882     }
883    
884     function hideItemById(id){
885     var itm = document.getElementById(id);
886     if(itm) itm.style.display='none';
887     }
888    
889     function showItemById(id){
890     var itm = document.getElementById(id);
891     if(itm) itm.style.display='block';
892     }
893    
894     function removeChildNodes(id){
895     var itm = document.getElementById(id);
896     if(!itm) return;
897    
898     var child = itm.firstChild;
899    
900     while(child){
901     itm.removeChild(child);
902     child = itm.firstChild;
903     }
904     }
905    
906     function chEditareaHeight(){
907     var itm = document.getElementById('editarea');
908     if(!itm) return;
909     var cv = itm.getAttribute('rows');
910     if(parseInt(cv) > editarea_h){
911     itm.setAttribute('rows', editarea_h);
912     }else{
913     itm.setAttribute('rows', editarea_h * 2);
914     }
915     }
916    
917     function format_file(str){
918     return "<pre>"+str+"</pre>";
919     }
920    
921     function previewData(data){
922     if(!data) data = document.getElementById('editarea').value;
923    
924     previewFile(data);
925    
926     updateShowMenu();
927     setMode('Preview');
928     }
929    
930     function previewFile(data){
931     removeChildNodes('preview');
932    
933     preview.style.display='block';
934     preview.style.position='absolute';
935     preview.style.top='180px';
936 isao-hara 14 preview.style.bottom='50px';
937     preview.style.left='0px';
938     preview.style.right='10px';
939 isao-hara 21 preview.style.width='800px';
940     preview.style.height='525px';
941 isao-hara 14 mkColorPalette();
942 isao-hara 2
943     var ele = toSVGElement(data, '100%','100%');
944    
945     preview.appendChild(ele);
946    
947     svg_top = document.getElementById('svg_top');
948     initSVGElementId(svg_top);
949     svg_select = createSVGObj('rect', 'x=1,y=1,width=1,height=1,visibility=hidden,stroke-dasharray=9 5', 'none', 'blue', 2);
950     svg_select.setAttribute("id","svg_select");
951     appendSVGObj(svg_select);
952 isao-hara 14 }
953 isao-hara 2
954 isao-hara 14 function mkColorPalette(){
955     var palette = document.getElementById('color-palette');
956     if(!palette) return;
957     if(palette.innerHTML) return;
958    
959     palette.innerHTML="";
960 isao-hara 32 palette.addEventListener("touchstart", onTouchStartColor, false);
961 isao-hara 30
962 isao-hara 14 for(var i=0; i<colors.length ;i++){
963     if(colors[i] == 'none')
964     palette.innerHTML +="<div class=\"item\" style=\"background-color:"+colors[i]+";\" color-val=\""+colors[i]+"\">X</div>";
965     else
966     palette.innerHTML +="<div class=\"item\" style=\"background-color:"+colors[i]+";\" color-val=\""+colors[i]+"\"> </div>";
967     }
968 isao-hara 25 palette.style.width='120px';
969 isao-hara 2 }
970    
971     ////// for SVG object
972     ////////////////
973 isao-hara 42 function downItem(){
974     if(selectedItems.length != 1) return;
975     var itm = selectedItems[0];
976     var nodes = svg_top.childNodes;
977     for(var i=0; i< nodes.length; i++){ if(nodes[i] == itm) break; }
978     if (i > 0) svg_top.insertBefore(itm, nodes[i-1]);
979     }
980    
981     function upItem(){
982     if(selectedItems.length != 1) return;
983     var itm = selectedItems[0];
984     var nodes = svg_top.childNodes;
985     for(var i=0; i< nodes.length; i++){ if(nodes[i] == itm) break; }
986    
987     if (i == nodes.length-2){
988     svg_top.appendChild(itm);
989     }else if (i < nodes.length-1){
990     svg_top.insertBefore(itm, nodes[i+2]);
991     }
992     }
993    
994     function bottomItem(){
995     if(selectedItems.length != 1) return;
996     var itm = selectedItems[0];
997     svg_top.insertBefore(itm, svg_top.firstChild);
998     }
999    
1000     function topItem(){
1001     if(selectedItems.length != 1) return;
1002     var itm = selectedItems[0];
1003     svg_top.removeChild(itm);
1004     svg_top.appendChild(itm);
1005     }
1006    
1007 isao-hara 2 function updateSVGObjMenu(){
1008     var res = "";
1009     var tag = modeSVG;
1010 isao-hara 65 if(selectedItems.length > 1){
1011     res += '<button onClick="groupSVGObjects(selectedItems);">Group</button>';
1012     return res;
1013     }
1014 isao-hara 64 if(selectedItems.length == 1){
1015     tag = getElementTag(selectedItems[0]);
1016     res += setSVGObjectProp(selectedItems[0]);
1017     }else{
1018     switch(tag){
1019     case 'text':
1020     res += propSVGText("", svg_font_size, svg_color, 0);
1021     break;
1022     case 'rect':
1023     case 'circle':
1024     case 'ellipse':
1025     case 'newPath':
1026     case 'newLine':
1027     case 'path':
1028     case 'line':
1029     res += propSVGObj(tag+":", svg_line_width, svg_color, svg_fill_color, "", 0);
1030     break;
1031     case 'image':
1032     res += propSVGImage("", 100, 100, 0);
1033     break;
1034     default:
1035     break;
1036     }
1037 isao-hara 2 }
1038     return res;
1039     }
1040    
1041 isao-hara 36 function propSVGText(str, size, color, rot){
1042 isao-hara 2 if(!size) size = 24;
1043     if(!color) color = '#000000';
1044 isao-hara 36 if(!rot) rot = 0;
1045 isao-hara 2
1046     var res = "Text:";
1047 isao-hara 28 res += "<input type=\"hidden\" id=\"svg_text\" value=\""+str+"\"/>";
1048     res += "<input type=\"hidden\" id=\"svg_color\" value=\""+color+"\" size=\"8\"/>";
1049 isao-hara 37 res += "Rot:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_rotate\" value=\""+rot+"\" size=\"4\"/>";
1050 isao-hara 36
1051 isao-hara 37 res += "Size:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_size\" value=\""+size+"\" size=\"4\"/>";
1052 isao-hara 25
1053     document.getElementById('toolFill').style.backgroundColor=color;
1054 isao-hara 2 return res;
1055     }
1056    
1057 isao-hara 39 function propSVGObj(type, stroke, color, fill, style, rot){
1058 isao-hara 2 var res = type;
1059     if(!stroke) stroke = 1;
1060     if(!color) color = '#000000';
1061     if(!fill) fill = '#ffffff';
1062 isao-hara 36 if(!rot) rot = 0;
1063 isao-hara 39
1064 isao-hara 28 res += "<input type=\"hidden\" id=\"svg_fill\" value=\""+fill+"\" size=\"8\"/>";
1065     res += "<input type=\"hidden\" id=\"svg_color\" value=\""+color+"\" size=\"8\"/>";
1066 isao-hara 58 res += "<input type=\"hidden\" id=\"svg_stroke\" onChange=\"updateSVGObj();\" value=\""+stroke+"\" size=\"2\"/>";
1067 isao-hara 37 res += "Rot:<input type=\"text\" id=\"svg_rotate\" onChange=\"updateSVGObj();\" value=\""+rot+"\" size=\"4\"/>";
1068 isao-hara 25
1069 isao-hara 39 if(style==null) style="";
1070 isao-hara 58 res += "<input type=\"hidden\" id=\"svg_stroke_type\" onChange=\"updateSVGObj();\" value=\""+style+"\" size=\"8\"/>";
1071 isao-hara 39
1072 isao-hara 58 res += "<img id=\"strokeW\" src=\"images/strokeW.png\" usemap=\"#strokemenu\" />\n";
1073     res += "<map name=\"strokemenu\">";
1074     res += "<area shape=\"rect\" coords=\"0,0,60,25\" onClick=\"selectStrokeW(0);\">";
1075     res += "<area shape=\"rect\" coords=\"0,25,60,50\" onClick=\"selectStrokeW(1);\">";
1076     res += "<area shape=\"rect\" coords=\"0,50,60,75\" onClick=\"selectStrokeW(2);\">";
1077     res += "<area shape=\"rect\" coords=\"0,75,60,100\" onClick=\"selectStrokeW(3);\">";
1078     res += "<area shape=\"rect\" coords=\"0,100,60,125\" onClick=\"selectStrokeW(4);\">";
1079     res += "<area shape=\"rect\" coords=\"0,125,60,150\" onClick=\"selectStrokeW(5);\">";
1080     res += "<area shape=\"rect\" coords=\"0,150,60,175\" onClick=\"selectStrokeW(6);\">";
1081     res += "<area shape=\"rect\" coords=\"0,175,60,200\" onClick=\"selectStrokeW(7);\">";
1082     res += "<area shape=\"rect\" coords=\"0,200,60,225\" onClick=\"selectStrokeW(8);\">";
1083     res += "</map>";
1084    
1085     res += "<img id=\"strokeDash\" src=\"images/dash_type.png\" usemap=\"#dashtype\" />\n";
1086     res += "<map name=\"dashtype\">";
1087     res += "<area shape=\"rect\" coords=\"0,0,50,25\" onClick=\"selectStrokeDash(0);\">";
1088     res += "<area shape=\"rect\" coords=\"0,25,50,50\" onClick=\"selectStrokeDash(1);\">";
1089     res += "<area shape=\"rect\" coords=\"0,50,50,75\" onClick=\"selectStrokeDash(2);\">";
1090     res += "<area shape=\"rect\" coords=\"0,75,50,100\" onClick=\"selectStrokeDash(3);\">";
1091     res += "<area shape=\"rect\" coords=\"0,100,50,125\" onClick=\"selectStrokeDash(4);\">";
1092     res += "<area shape=\"rect\" coords=\"0,125,50,150\" onClick=\"selectStrokeDash(5);\">";
1093     res += "<area shape=\"rect\" coords=\"0,150,50,175\" onClick=\"selectStrokeDash(6);\">";
1094     res += "</map>";
1095    
1096 isao-hara 25 document.getElementById('toolFill').style.backgroundColor=fill;
1097     document.getElementById('toolStroke').style.backgroundColor=color;
1098 isao-hara 2 return res;
1099     }
1100    
1101 isao-hara 36 function propSVGLine(type, stroke, color, fill, style, rot){
1102 isao-hara 39 var res = propSVGObj(type, stroke, color, fill, style, rot);
1103 isao-hara 36 if(!rot) rot = 0;
1104 isao-hara 39
1105 isao-hara 54 res += "<img id=\"arrow_l\" src=\"images/arrow_l.png\" usemap=\"#arrow_l\" />\n";
1106     res += "<map name=\"arrow_l\">";
1107     res += "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"setLeftArrow();\">";
1108     res += "<area shape=\"rect\" coords=\"0,25,30,50\" onClick=\"removeLeftArrow();\">";
1109     res += "</map>";
1110 isao-hara 25
1111 isao-hara 54 res += "<img id=\"arrow_r\" src=\"images/arrow_r.png\" usemap=\"#arrow_r\" />\n";
1112     res += "<map name=\"arrow_r\">";
1113     res += "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"setRightArrow();\">";
1114     res += "<area shape=\"rect\" coords=\"0,25,30,50\" onClick=\"removeRightArrow();\">";
1115     res += "</map>";
1116 isao-hara 52
1117 isao-hara 55
1118 isao-hara 54 res += "<img id=\"path_type\" src=\"images/path_type.png\" usemap=\"#path_type\" />\n";
1119     res += "<map name=\"path_type\">";
1120 isao-hara 64 res += "<area shape=\"rect\" coords=\"0,0,30,100\" onClick=\"togglePathType();\">";
1121 isao-hara 54 res += "</map>";
1122 isao-hara 25 document.getElementById('toolFill').style.backgroundColor=fill;
1123     document.getElementById('toolStroke').style.backgroundColor=color;
1124    
1125 isao-hara 2 return res;
1126     }
1127    
1128 isao-hara 58 function getStrokeWIdx(lw){
1129     var res=0;
1130     for(var i=0;i<lineW.length;i++){
1131     if(lw < lineW[i]){ return res; }
1132     res = i;
1133     }
1134     return lineW.length-1;
1135     }
1136    
1137     function getStrokeW(idx){ return lineW[idx]; }
1138    
1139 isao-hara 52 function updateStrokeWidth(){
1140     if(selectedItems.length == 1){
1141     var obj = selectedItems[0];
1142 isao-hara 54 if(!obj){ return; }
1143 isao-hara 52 var val = obj.getAttribute('stroke-width');
1144     if(!val) return;
1145 isao-hara 58 var lw = parseFloat(val);
1146     updateStrokeWImg(getStrokeWIdx(lw));
1147     updateStrokeDash();
1148 isao-hara 52 }
1149     }
1150    
1151 isao-hara 58 function updateStrokeWImg(idx){
1152 isao-hara 52 var ele = document.getElementById('strokeW');
1153     if(!ele) return;
1154 isao-hara 58 var v1 = idx*25;
1155 isao-hara 52 var v2 = v1+25;
1156 isao-hara 58 var v3 = 81 - v1;
1157 isao-hara 52 ele.style.clip = "rect("+v1+"px,70px,"+v2+"px,0px)";
1158     ele.style.top = v3+"px";
1159     }
1160    
1161 isao-hara 58 function selectStrokeW(idx){
1162     var ele = document.getElementById('strokeW');
1163     if(ele.style.clip != "rect(auto auto auto auto)"){
1164     ele.style.clip = "rect(auto auto auto auto)";
1165     ele.style.top = "81px";
1166     return;
1167     }
1168 isao-hara 52 if(selectedItems.length == 1){
1169     var obj = selectedItems[0];
1170 isao-hara 58 var lw = getStrokeW(idx);
1171     var plw = parseFloat(obj.getAttribute('stroke-width'));
1172 isao-hara 52 obj.setAttribute('stroke-width', lw);
1173 isao-hara 58 updateDashArray(obj, lw, plw, obj.getAttribute('stroke-dasharray'));
1174     updateStrokeWImg(idx);
1175     }else{
1176     updateStrokeWImg(1);
1177     }
1178 isao-hara 52 }
1179    
1180 isao-hara 58 function updateStrokeDash(){
1181 isao-hara 52 if(selectedItems.length == 1){
1182     var obj = selectedItems[0];
1183 isao-hara 58 if(!obj){ return; }
1184 isao-hara 52 var val = obj.getAttribute('stroke-width');
1185     if(!val) return;
1186 isao-hara 58 var lw = parseFloat(val);
1187     var darr = obj.getAttribute('stroke-dasharray');
1188     if(!darr) return;
1189     updateStrokeDashImg( getDashArrayIndex(lw, darr) );
1190     }
1191 isao-hara 52 }
1192 isao-hara 58 function updateStrokeDashImg(idx){
1193     var ele = document.getElementById('strokeDash');
1194     if(!ele) return;
1195     var v1 = idx*25;
1196     var v2 = v1+25;
1197     var v3 = 81 - v1;
1198     ele.style.clip = "rect("+v1+"px,70px,"+v2+"px,0px)";
1199     ele.style.top = v3+"px";
1200     }
1201 isao-hara 52
1202 isao-hara 58 function selectStrokeDash(idx){
1203     var ele = document.getElementById('strokeDash');
1204     if(ele.style.clip != "rect(auto auto auto auto)"){
1205     ele.style.clip = "rect(auto auto auto auto)";
1206     ele.style.top = "81px";
1207     return;
1208     }
1209    
1210     if(selectedItems.length == 1){
1211     var obj = selectedItems[0];
1212     var lw = obj.getAttribute('stroke-width');
1213     if(!lw ) lw=1;
1214     else lw=parseFloat(lw);
1215    
1216     var darr = StrokeDash[idx];
1217     if(darr=="") try{ obj.removeAttribute('stroke-dasharray'); }catch(e){}
1218     else updateDashArray(obj, lw, 1, darr);
1219    
1220     updateStrokeDashImg(idx);
1221     }else{
1222     updateStrokeDashImg(1);
1223     }
1224     }
1225    
1226     function getDashArrayIndex(plw, darr){
1227     if(!darr || darr=="") return 0;
1228     var d_arr = darr.split(' ');
1229     var top = Math.round(parseFloat(d_arr[0])/plw);
1230     if(top == 6){
1231     if(d_arr.length == 2) return 2;
1232     if(d_arr.length == 4) return 3;
1233     }else if(top == 8){
1234     if(d_arr.length == 2) return 4;
1235     if(d_arr.length == 4) return 5;
1236     if(d_arr.length == 6) return 6;
1237     }else if(top == 2){
1238     return 1;
1239     }
1240     return 0;
1241     }
1242    
1243     function updateDashArray(obj, lw, plw, darr){
1244     if(darr && darr != ""){
1245     var d_arr = darr.split(' ');
1246     darr = "";
1247     for(var i=0; i<d_arr.length; i++){
1248     var val = Math.round(parseFloat(d_arr[i])/plw);
1249     darr += val * lw + ' ';
1250     }
1251     obj.setAttribute('stroke-dasharray', trim(darr));
1252     return true;
1253     }
1254     return false;
1255     }
1256    
1257     function togglePathType(){
1258     if(selectedItems.length != 1) return;
1259     var itm = selectedItems[0];
1260     var path = itm.getAttribute("d").split(' ');
1261     var newpath = "";
1262    
1263     for(var i=0;i<path.length ;i++){
1264     if(path[i] == "L"){ path[i] = "Q"; }
1265 isao-hara 64 else if(path[i] == "Q"){ path[i] = "S"; }
1266     else if(path[i] == "S"){ path[i] = "T"; }
1267     else if(path[i] == "T"){ path[i] = "L"; }
1268 isao-hara 58 newpath += path[i] + ' ';
1269     }
1270     var mm = path.length % 4;
1271     if(mm != 0) {
1272     newpath += path[i-2] + ' ';
1273     newpath += path[i-1] + ' ';
1274     }
1275    
1276     itm.setAttribute("d",trim(newpath));
1277     updatePathTypeMenu();
1278     }
1279    
1280     function updatePathTypeMenu(){
1281     if(selectedItems.length != 1) return;
1282     var itm = selectedItems[0];
1283     if(!itm) return;
1284     var path = itm.getAttribute("d");
1285     var img = document.getElementById('path_type');
1286     if(!path){
1287 isao-hara 65 if(img) img.style.display='none';
1288 isao-hara 58 return;
1289     }
1290    
1291     if(path.indexOf("Q") > 0){
1292     selectMenuImage(img, 1);
1293     }else if(path.indexOf("T") > 0){
1294     selectMenuImage(img, 2);
1295     }else if(path.indexOf("S") > 0){
1296     selectMenuImage(img, 3);
1297     }else{
1298     selectMenuImage(img, 0);
1299     }
1300     img.style.display='block';
1301     }
1302    
1303     function updateArrowMenu(){
1304     try{
1305     var imgl = document.getElementById('arrow_l');
1306 isao-hara 60 if(hasArrow('start')){ selectMenuImage(imgl, 1); }else{ selectMenuImage(imgl, 0); }
1307 isao-hara 58 }catch(e){}
1308    
1309     try{
1310     var imgr = document.getElementById('arrow_r');
1311 isao-hara 60 if(hasArrow('end')){ selectMenuImage(imgr, 1); }else{ selectMenuImage(imgr, 0); }
1312 isao-hara 58 }catch(e){}
1313     }
1314    
1315     function selectMenuImage(img, pos){
1316     if(!img) return;
1317 isao-hara 60 var clip = new Array("rect(0px,30px,25px,0px)","rect(25px,30px,50px,0px)",
1318     "rect(50px,30px,75px,0px)", "rect(75px,30px,100px,0px)");
1319     var top = new Array( "81px", "56px", "31px", "6px");
1320 isao-hara 58
1321     img.style.clip = clip[pos];
1322     img.style.top = top[pos];
1323     }
1324    
1325 isao-hara 36 function propSVGImage(str, w, h, rot){
1326 isao-hara 5 var res = "image:";
1327 isao-hara 36 if(!rot) rot = 0;
1328 isao-hara 5 res += "<input type=\"text\" id=\"svg_text\" value=\""+str+"\" />";
1329 isao-hara 51 res += "Width:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_w\" value=\""+w+"\" size=\"4\"/>";
1330     res += "Height:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_h\" value=\""+h+"\" size=\"4\"/>";
1331 isao-hara 37 res += "Rot:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_rotate\" value=\""+rot+"\" size=\"4\"/>";
1332 isao-hara 5 return res;
1333     }
1334    
1335 isao-hara 64
1336 isao-hara 2 function setSVGMode(m){
1337     modeSVG = m;
1338 isao-hara 51 switch(m){
1339     case 'selector':
1340     selectToolBar(0);
1341     break;
1342     case 'newPath':
1343     selectToolBar(1);
1344     break;
1345     case 'newLine':
1346     selectToolBar(2);
1347     break;
1348     case 'text':
1349     selectToolBar(3);
1350     break;
1351     case 'rect':
1352     selectToolBar(4);
1353     break;
1354     case 'circle':
1355     selectToolBar(5);
1356     break;
1357     case 'ellipse':
1358     selectToolBar(6);
1359     break;
1360     case 'image':
1361     selectToolBar(7);
1362     break;
1363     default:
1364     break;
1365 isao-hara 2 }
1366 isao-hara 64 updateShowMenu();
1367 isao-hara 2 }
1368    
1369    
1370     function toSVGElement(str, w, h){
1371     var xmlsvg = "xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
1372     var parser = new DOMParser();
1373     var header = "<svg:svg width=\""+w+"\" height=\""+h+"\" "+xmlsvg+" id=\"svg_top\">";
1374     var footer = "</svg:svg>";
1375     var xmlDoc = parser.parseFromString(header+str+footer, "text/xml");
1376     var xmlRoot = xmlDoc.documentElement;
1377     var ele = document.importNode(xmlRoot,true);
1378    
1379     return ele;
1380     }
1381    
1382 isao-hara 47 function newID(){
1383     var id = 'svg_'+nextId;
1384     nextId++;
1385     return id;
1386     }
1387    
1388 isao-hara 2 function createSVGElement(tag, id){
1389 isao-hara 51 if(!id || id=='new') id = newID();
1390 isao-hara 2 var ele= document.createElementNS(svg_ns, tag);
1391     ele.setAttribute("id", id);
1392     return ele;
1393     }
1394    
1395     function defSVGElement(node){
1396     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1397     if(!svg_defs){
1398     svg_defs = createSVGElement('defs', 'svg_defs');
1399     svg_top.insertBefore(svg_defs, svg_top.firstChild);
1400     }
1401     if(node) svg_defs.appendChild(node);
1402     }
1403    
1404     function initSVGElementId(svg_top){
1405     nextId = 1;
1406     checkSVGElementId(svg_top);
1407     appendSVGElementId(svg_top);
1408     }
1409    
1410     function checkSVGElementId(top){
1411     var objs = top.childNodes;
1412    
1413     for(var i=0; i<objs.length ;i++){
1414     if(objs[i].tagName){
1415     var id = objs[i].getAttribute("id");
1416    
1417     if(id && id.match(/svg_[0-9]+/i)){
1418     var n = parseInt(RegExp.lastMatch.substr(4));
1419     if(n >= nextId){
1420     nextId = n+1;
1421     }
1422    
1423     }
1424     checkSVGElementId(objs[i]);
1425     }
1426     }
1427     }
1428    
1429     function appendSVGElementId(top){
1430     var objs = top.childNodes;
1431    
1432     for(var i=0; i<objs.length ;i++){
1433     if(objs[i].tagName){
1434     var id = objs[i].getAttribute("id");
1435     if(!id){
1436     objs[i].setAttribute("id", "svg_"+nextId);
1437     nextId++;
1438     }
1439     }
1440     appendSVGElementId(objs[i]);
1441     }
1442     }
1443    
1444     function setAttributes(obj, attrs){
1445     var attr_array = attrs.split(',');
1446    
1447     for (var i=0; i<attr_array.length;i++){
1448     var x = attr_array[i].split('=');
1449     if(x.length == 2){
1450     obj.setAttribute(x[0], x[1]);
1451     }
1452     }
1453     }
1454    
1455     function createSVGObj(tag, attrs, fill, color, lw){
1456     var ele = createSVGElement(tag, 'new');
1457     setAttributes(ele, attrs);
1458     if (fill) ele.setAttribute('fill', fill);
1459     if (color) ele.setAttribute('stroke', color);
1460     if(lw) ele.setAttribute('stroke-width', lw);
1461    
1462     return ele;
1463     }
1464    
1465     function createSVGText(txt, x, y, size, color){
1466     var ele = createSVGElement('text', 'new');
1467    
1468     ele.setAttribute('x', x);
1469     ele.setAttribute('y', y);
1470     ele.setAttribute('font-size', size);
1471     ele.setAttribute('fill', color);
1472     ele.textContent=txt;
1473    
1474     return ele;
1475     }
1476    
1477     function createSVGImage(fname, width, height, attrs){
1478     var ele = createSVGElement('image', 'new');
1479 isao-hara 7 ele.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', fname);
1480 isao-hara 2 ele.setAttribute('width', width);
1481     ele.setAttribute('height', height);
1482     setAttributes(ele, attrs);
1483    
1484     return ele;
1485     }
1486    
1487 isao-hara 65 function groupSVGObjects(){
1488     try{
1489     var objs = selectedItems;
1490     if(objs.length == 0) return null;
1491     var ele = createSVGElement('g', 'new');
1492     for(var i=0;i<objs.length;i++){
1493     var obj = objs[i];
1494     obj.parentNode.removeChild(obj);
1495     ele.appendChild(obj);
1496     }
1497     appendSVGObj(ele);
1498     selectedItems = new Array(ele);
1499     updateShowMenu();
1500     return ele;
1501     }catch(e){ return null;}
1502     }
1503    
1504     function ungroupSVGObjects(){
1505     try{
1506     var obj = selectedItems[0];
1507     if(getElementTag(obj) != 'g') return;
1508     var parent = obj.parentNode;
1509     selectedItems = new Array();
1510     while(obj.firstChild != null){
1511     selectedItems.push(obj.firstChild);
1512     parent.appendChild(obj.firstChild);
1513     }
1514     parent.removeChild(obj);
1515     updateShowMenu();
1516     }catch(e){ return;}
1517     }
1518    
1519 isao-hara 2 function createSVGMarker(pid, id, child){
1520     var parent = document.getElementById(pid);
1521     if(!parent) return;
1522     var ele = createSVGElement('marker', pid+'_'+id);
1523     ele.appendChild(child);
1524     return ele;
1525     }
1526    
1527     function setLeftArrow(){
1528     if( selectedItems.length == 1 ){
1529     setArrow(selectedItems[0], 'start', '');
1530 isao-hara 36 updateShowMenu();
1531 isao-hara 2 }
1532     }
1533    
1534     function setRightArrow(){
1535     if( selectedItems.length == 1 ){
1536     setArrow(selectedItems[0], 'end', '');
1537 isao-hara 36 updateShowMenu();
1538 isao-hara 2 }
1539     }
1540    
1541     function removeLeftArrow(){
1542     if( selectedItems.length == 1 ){
1543     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1544 isao-hara 64 try{
1545     var marker = getArrowMarker(selectedItems[0],'start');
1546     svg_defs.removeChild(marker);
1547     selectedItems[0].removeAttribute('marker-start');
1548     }catch(e){ }
1549 isao-hara 36 updateShowMenu();
1550 isao-hara 2 }
1551     }
1552    
1553     function removeRightArrow(){
1554     if( selectedItems.length == 1 ){
1555     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1556 isao-hara 64 try{
1557     var marker = getArrowMarker(selectedItems[0],'end');
1558     svg_defs.removeChild(marker);
1559     selectedItems[0].removeAttribute('marker-end');
1560     }catch(e){ }
1561 isao-hara 54 updateShowMenu();
1562 isao-hara 2 }
1563     }
1564    
1565 isao-hara 64 function removeAllMarker(obj){
1566     var smarker = getArrowMarker(obj,'start');
1567     if(smarker) svg_defs.removeChild(smarker);
1568     var emarker = getArrowMarker(obj,'end');
1569     if(emarker) svg_defs.removeChild(emarker);
1570     return true;
1571     }
1572    
1573 isao-hara 2 function hasArrow(pos){
1574     var pobj = selectedItems[0];
1575     if(!pobj) return false;
1576 isao-hara 64 if(getArrowMarker(pobj,pos)) return true;
1577 isao-hara 2 return false;
1578     }
1579    
1580     function getArrowMarker(obj,pos){
1581 isao-hara 64 try{
1582     return document.getElementById(obj.getAttribute('id')+'_'+pos+'arrow');
1583     }catch(e){ return null; }
1584    
1585 isao-hara 2 }
1586    
1587     function setArrow(pobj, pos, type){
1588     if(!pobj) return;
1589 isao-hara 64 var marker = getArrowMarker(pobj,pos);
1590     if(marker){ return; }
1591 isao-hara 2
1592     var obj = createSVGElement('path', 'new');
1593    
1594 isao-hara 64 var refX = 10;
1595     var refY = 10;
1596 isao-hara 2
1597     switch(pos){
1598     case 'start':
1599     setAttributes(obj,'d=M 20 0 L 0 10 20 20 Z');
1600     break;
1601     case 'end':
1602     setAttributes(obj,'d=M 0 0 L 20 10 0 20 Z');
1603     break;
1604     default:
1605     return;
1606     }
1607    
1608 isao-hara 64 var pid = pobj.getAttribute("id");
1609 isao-hara 2 marker = createSVGMarker(pid, pos+'arrow' , obj);
1610 isao-hara 64
1611 isao-hara 2 setAttributes(marker,'markerWidth=10,markerHeight=10,orient=auto,viewBox=0 0 20 20,markerUnits=strokeWidth,refX='+refX+',refY='+refY);
1612    
1613     marker.setAttribute("fill",pobj.getAttribute("stroke"));
1614    
1615     defSVGElement(marker);
1616 isao-hara 56 if(!document.getElementById(pid+'_'+pos+'arrow')){
1617     alert("Error in setArrow");
1618     return;
1619     }
1620 isao-hara 2 var mid = "url(#"+marker.getAttribute("id")+")";
1621     var mattr = "marker-"+pos;
1622     pobj.setAttribute(mattr, mid);
1623 isao-hara 64 updateShowMenu();
1624 isao-hara 2 }
1625    
1626 isao-hara 44 function scalePath(itm, scale){
1627 isao-hara 52 if(!scale) return;
1628 isao-hara 44 var path = itm.getAttribute("d").split(' ');
1629 isao-hara 46 var bbox = itm.getBBox();
1630 isao-hara 44 var newpath = "";
1631 isao-hara 59 var sX=scale[0];
1632     var sY=scale[1];
1633 isao-hara 46 var isX=true;
1634     var dx = bbox.x - sX*bbox.x;
1635     var dy = bbox.y - sY*bbox.y;
1636 isao-hara 44
1637     for(var i=0;i<path.length ;i++){
1638     if(path[i].match(/[0-9]+/)){
1639     var val = parseInt(path[i]);
1640 isao-hara 46 if(isX){
1641     path[i] = Math.round(val*sX) + dx ;
1642     isX = false;
1643     }else{
1644     path[i] = Math.round(val*sY) + dy ;
1645     isX = true;
1646     }
1647 isao-hara 44 }
1648     newpath += path[i] + ' ';
1649     }
1650    
1651     itm.setAttribute("d",trim(newpath));
1652     }
1653    
1654     function scaleLine(itm, scale){
1655 isao-hara 52 if(!scale) return;
1656 isao-hara 46 var sX=scale[0];
1657     var sY=scale[1];
1658 isao-hara 44 var x1 = parseInt(itm.getAttribute("x1"));
1659     var y1 = parseInt(itm.getAttribute("y1"));
1660     var x2 = parseInt(itm.getAttribute("x2"));
1661     var y2 = parseInt(itm.getAttribute("y2"));
1662 isao-hara 46 var bbox = itm.getBBox();
1663     var dx = bbox.x - sX*bbox.x;
1664     var dy = bbox.y - sY*bbox.y;
1665 isao-hara 44
1666 isao-hara 46 itm.setAttribute("x1", Math.round(x1*sX)+dx);
1667     itm.setAttribute("y1", Math.round(y1*sY)+dy);
1668     itm.setAttribute("x2", Math.round(x2*sX)+dx);
1669     itm.setAttribute("y2", Math.round(y2*sY)+dy);
1670 isao-hara 44 }
1671    
1672 isao-hara 2 function appendSVGObj(obj){
1673     var svg_top = document.getElementById('svg_top');
1674     if(!svg_top) return;
1675    
1676     svg_top.appendChild(obj);
1677     }
1678    
1679 isao-hara 65 function removeSVGObj(obj){
1680     var svg_top = document.getElementById('svg_top');
1681     if(!svg_top) return;
1682    
1683     svg_top.removeChild(obj);
1684     }
1685    
1686    
1687 isao-hara 2 function isChildById(element, id) {
1688     if (element == null || element.parentNode == null || element.parentNode.nodeName=='BODY') return false;
1689     else if (element.parentNode.id == id) return true;
1690     else return isChildById(element.parentNode, id);
1691     }
1692    
1693     /////////////////////// Formatting SVG DOM
1694     function escapeHTML(text) {
1695     return text.replace( /[<>"&]/g,
1696     function (m) { return { '<': '&lt;', '>': '&gt;', '"': '&quot;', '&': '&amp;' }[m]; }
1697     );
1698     };
1699    
1700     function formatTag(ele){
1701     var str="";
1702     if(ele.nodeType == 1){
1703     var tag_a = ele.tagName.split(':');
1704     var tag;
1705     if(tag_a.length == 1){ tag = "svg:"+tag_a[0]; }else{ tag = ele.tagName; }
1706    
1707     str += "<"+tag;
1708     var attrs = ele.attributes;
1709     for(var i=0; i<attrs.length; i++){
1710     str += " "+attrs[i].nodeName+"=\""+attrs[i].nodeValue+"\"";
1711     }
1712     var cn = ele.childNodes;
1713     if(cn.length > 0){
1714     str +=">\n";
1715     for(var i=0; i<cn.length; i++){
1716 isao-hara 13 var tmp = trim(formatTag(cn[i]));
1717     if(tmp) str += " "+tmp+"\n";
1718 isao-hara 2 }
1719     str += "</"+tag+">";
1720     }else{
1721     str +=" />";
1722     }
1723     return str;
1724     }else if(ele.nodeType==3){
1725     return ele.textContent;
1726     }
1727     }
1728    
1729     function getSVGContent(){
1730     if(!svg_top) return "";
1731    
1732     var str = "";
1733     var elements = svg_top.childNodes;
1734     for(var i=0; i<elements.length; i++){
1735 isao-hara 12 if(elements[i] != svg_select){
1736     var tmp = trim(formatTag(elements[i]));
1737     if(tmp) str += tmp + '\n';
1738     }
1739 isao-hara 2 }
1740     return str;
1741     }
1742    
1743     function trim(str){
1744     return str.replace(/(^\s+)|(\s+$)/g, "");
1745     }
1746    
1747     //////// Event Handler
1748 isao-hara 51 function selectedRectangle(x, y, ex, ey){
1749     if(!svg_select) return null;
1750    
1751     svg_select.setAttribute("x",x);
1752     svg_select.setAttribute("y",y);
1753     svg_select.setAttribute("width", ex-x);
1754     svg_select.setAttribute("height", ey-y);
1755     svg_select.setAttribute("visibility", "visible");
1756     return svg_select;
1757     }
1758    
1759     function hideSelectedRectangle(){
1760     svg_select.setAttribute("x",0);
1761     svg_select.setAttribute("y",0);
1762     svg_select.setAttribute("width",0);
1763     svg_select.setAttribute("height",0);
1764     svg_select.setAttribute("visibility","hidden");
1765     }
1766    
1767 isao-hara 2 function setInnerHTML(id, val){
1768     var itm=document.getElementById(id);
1769     if(itm) itm.innerHTML=val;
1770     }
1771    
1772 isao-hara 62 function toggleRotateScaleLock(){
1773     var lock = document.getElementById('lock');
1774 isao-hara 51 if(svg_rotate_locked){
1775 isao-hara 62 svg_rotate_locked=false;
1776     if(lock.style.top == '450px'){
1777     lock.style.display='none';
1778     }else{
1779     svg_scale_locked=true;
1780     lock.style.top = '470px';
1781     }
1782     return;
1783     }
1784     if(svg_scale_locked){
1785     svg_scale_locked=false;
1786     if(lock.style.top == '470px'){
1787     lock.style.display='none';
1788     }else{
1789     svg_rotate_locked=true;
1790     lock.style.top = '450px';
1791     }
1792     return;
1793     }
1794    
1795     }
1796    
1797     function setRotLock(){
1798     var lock = document.getElementById('lock');
1799     if(!svg_rotate_locked){
1800 isao-hara 51 svg_rotate_locked = true;
1801 isao-hara 62 svg_scale_locked = false;
1802     lock.style.top = '450px';
1803 isao-hara 51 lock.style.display='block';
1804 isao-hara 62 }else alert("setRotLock");
1805 isao-hara 42 }
1806    
1807 isao-hara 62 function setScaleLock(){
1808     var lock = document.getElementById('lock');
1809     if(!svg_scale_locked){
1810 isao-hara 51 svg_scale_locked = true;
1811 isao-hara 62 svg_rotate_locked = false;
1812     lock.style.top = '470px';
1813 isao-hara 51 lock.style.display='block';
1814 isao-hara 62 }else alert("setScaleLock");
1815 isao-hara 41 }
1816    
1817 isao-hara 2 function popupInfo(val){
1818     var str="<button onClick=\"hideItemById('popup');\">Close</button><hr> ";
1819     str += val;
1820     setInnerHTML('popup', str);
1821     showItemById('popup');
1822     }
1823    
1824     function addAttributeVal(obj, itm, dv){
1825 isao-hara 65 if(obj.hasAttribute(itm)){
1826     var x = parseInt(obj.getAttribute(itm));
1827     x = x + dv;
1828     obj.setAttribute(itm, x)
1829     }else alert("No such attribute:"+itm);
1830 isao-hara 2 }
1831    
1832     function showSVGSource(){
1833     var str = getSVGContent();
1834 isao-hara 14 var escstr="<button onClick=\"hideItemById('popup');\">Close</button>";
1835 isao-hara 70 escstr+="<button onClick=\"Editor.saveContent(document.getElementById('ContentView').value); previewData(document.getElementById('ContentView').value);\">Save</button><hr> ";
1836 isao-hara 51 escstr += "<textarea cols=\"100\" rows=\"30\" id=\"ContentView\">"+str+"</textarea>";
1837 isao-hara 2 setInnerHTML('popup', escstr);
1838     showItemById('popup');
1839     }
1840    
1841    
1842     function getElementTag(obj){
1843 isao-hara 6 if (!obj) return null;
1844 isao-hara 65 if(!obj.tagName) return "NoName";
1845 isao-hara 2 var tag = obj.tagName.split(':');
1846     if(tag.length == 2 && tag[0]=='svg') return tag[1];
1847     return tag[0];
1848     }
1849    
1850     function setSVGObjectProp(obj){
1851     var tag = getElementTag(obj);
1852     var res ="";
1853     switch(tag){
1854     case 'text':
1855 isao-hara 36 res = propSVGText(obj.textContent, obj.getAttribute("font-size"), obj.getAttribute("fill"),getRotateAngle(obj));
1856 isao-hara 2 break;
1857     case 'rect':
1858     case 'circle':
1859     case 'ellipse':
1860 isao-hara 39 res = propSVGObj(tag+":", obj.getAttribute("stroke-width"), obj.getAttribute("stroke"),
1861     obj.getAttribute("fill"),obj.getAttribute("stroke-dasharray"),getRotateAngle(obj));
1862 isao-hara 2 break;
1863     case 'path':
1864     case 'line':
1865     res = propSVGLine(tag+":",obj.getAttribute("stroke-width"),
1866 isao-hara 36 obj.getAttribute("stroke"),obj.getAttribute("fill"), obj.getAttribute("stroke-dasharray"),getRotateAngle(obj));
1867 isao-hara 2 break;
1868 isao-hara 5 case 'image':
1869 isao-hara 36 res = propSVGImage(obj.getAttribute("xlink:href"),obj.getAttribute("width"),obj.getAttribute("height"),getRotateAngle(obj));
1870 isao-hara 5 break;
1871 isao-hara 65 case 'g':
1872     res += '<button onClick="ungroupSVGObjects();">Ungroup</button>';
1873     break;
1874 isao-hara 2 default:
1875     break;
1876     }
1877    
1878     return res
1879     }
1880    
1881     function updateSVGObj(){
1882     if(selectedItems.length != 1) return;
1883     var obj = selectedItems[0];
1884     var tag = getElementTag(obj);
1885 isao-hara 36 var deg=document.getElementById('svg_rotate');
1886 isao-hara 2 var res ="";
1887     switch(tag){
1888     case 'text':
1889     var txt=document.getElementById('svg_text');
1890     var color=document.getElementById('svg_color');
1891     var size=document.getElementById('svg_size');
1892     obj.textContent = txt.value;
1893     obj.setAttribute("fill", color.value);
1894     obj.setAttribute("font-size", size.value);
1895 isao-hara 36 setRotate(obj,deg.value);
1896 isao-hara 2 break;
1897     case 'rect':
1898     case 'circle':
1899     case 'ellipse':
1900     var fill=document.getElementById('svg_fill');
1901     var color=document.getElementById('svg_color');
1902     var L=document.getElementById('svg_stroke');
1903 isao-hara 39 var dash=document.getElementById('svg_stroke_type');
1904 isao-hara 2 obj.setAttribute("fill", fill.value);
1905     obj.setAttribute("stroke", color.value);
1906     obj.setAttribute("stroke-width", L.value);
1907 isao-hara 39 if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1908 isao-hara 36 setRotate(obj,deg.value);
1909 isao-hara 2 break;
1910     case 'path':
1911     case 'line':
1912     var fill=document.getElementById('svg_fill');
1913     var color=document.getElementById('svg_color');
1914     var L=document.getElementById('svg_stroke');
1915     var dash=document.getElementById('svg_stroke_type');
1916     obj.setAttribute("fill", fill.value);
1917     obj.setAttribute("stroke", color.value);
1918     obj.setAttribute("stroke-width", L.value);
1919 isao-hara 39 if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1920 isao-hara 36 setRotate(obj,deg.value);
1921 isao-hara 39
1922 isao-hara 2 var id = obj.getAttribute("id");
1923     var marker = document.getElementById(id+'_startarrow');
1924     if(marker) marker.setAttribute("fill", color.value);
1925     var marker = document.getElementById(id+'_endarrow');
1926     if(marker) marker.setAttribute("fill", color.value);
1927     break;
1928 isao-hara 5 case 'image':
1929     var fname=document.getElementById('svg_text');
1930     var w=document.getElementById('svg_w');
1931     var h=document.getElementById('svg_h');
1932 isao-hara 6 obj.setAttribute("xlink:href", fname.value);
1933 isao-hara 5 obj.setAttribute("width", w.value);
1934     obj.setAttribute("height", h.value);
1935 isao-hara 36 setRotate(obj,deg.value);
1936 isao-hara 5 break;
1937 isao-hara 2 default:
1938     break;
1939     }
1940    
1941     }
1942    
1943     function delSVGObj(){
1944     if(!svg_top) return;
1945 isao-hara 43 for(var i=0; i< selectedItems.length; i++){
1946     var obj = selectedItems[i];
1947 isao-hara 64 removeAllMarker(obj);
1948 isao-hara 43 svg_top.removeChild(obj);
1949     }
1950 isao-hara 51 hideSelectedRectangle();
1951 isao-hara 2 }
1952    
1953     ///// For MobileSafari
1954     function getPreviewX(x){ return x - preview.offsetLeft; }
1955     function getPreviewY(y){ return y - preview.offsetTop; }
1956    
1957 isao-hara 51 function calcDict(x1, y1, x2, y2){
1958 isao-hara 2 return (x1-x2)*(x1-x2) +(y1-y2)*(y1-y2);
1959     }
1960    
1961     function isIncludeEllipse( x1, y1, cx, cy, rx, ry){
1962     return ((x1-cx)*(x1-cx)/rx/rx +(y1-cy)*(y1-cy)/ry/ry) < 1;
1963     }
1964    
1965     function getAttributeVal(obj, name){
1966     try{
1967     return parseInt(obj.getAttribute(name));
1968     }catch(e){ return 0; }
1969     }
1970    
1971     function checkIntersection(obj, x, y, ex, ey){
1972 isao-hara 18 if(!obj) return false;
1973 isao-hara 2 var res = true;
1974     var bbox = obj.getBBox();
1975     var ox = bbox.x;
1976     var oy = bbox.y;
1977     var oex = bbox.x+bbox.width;
1978     var oey = bbox.y+bbox.height;
1979     if( ex < ox || oex < x || ey < oy || oey < y) res = false;
1980    
1981     var tag = getElementTag(obj);
1982     switch(tag){
1983     case 'circle':
1984     case 'ellipse':
1985     var cx = getAttributeVal(obj,"cx");
1986     var cy = getAttributeVal(obj,"cy");
1987     var rx = getAttributeVal(obj,"r");
1988     var ry = rx;
1989     if(!rx){
1990     var rx = getAttributeVal(obj,"rx");
1991     var ry = getAttributeVal(obj,"ry");
1992     }
1993    
1994     if(res){
1995     if(cx <x && cy < y && !isIncludeEllipse(x,y,cx,cy,rx,ry)) res=false;
1996     else if(cx < x && cy > ey && !isIncludeEllipse(x,ey,cx,cy,rx,ry)) res=false;
1997     else if(cx > ex && cy > ey && !isIncludeEllipse(ex,ey,cx,cy,rx,ry)) res=false;
1998     else if(cx > ex && cy < y && !isIncludeEllipse( ex,y,cx,cy,rx,ry)) res=false;
1999     }
2000     break;
2001     case 'path':
2002     var d = obj.getAttribute("d");
2003     var p = getPoints(d);
2004     for(var i=0;i<p.length;i++){
2005     var ox=p[i][0];
2006     var oy=p[i][1];
2007     if(x < ox && ox < ex && y < oy && oy < ey) {
2008     return true;
2009     }
2010     }
2011     return false;
2012     break;
2013     case 'line':
2014     var x1 = getAttributeVal(obj,"x1");
2015     var y1 = getAttributeVal(obj,"y1");
2016     var x2 = getAttributeVal(obj,"x2");
2017     var y2 = getAttributeVal(obj,"y2");
2018 isao-hara 3 var d = (y2-y1)/(x2-x1);
2019    
2020     if(res){
2021     var xx = Math.abs(x2-x1);
2022     var sign = 1;
2023     if(x2-x1 < 0){ sign = -1; }
2024     for(var i=0; i < xx; i++){
2025     var nx = i*sign + x1;
2026     var ny = d * i*sign + y1;
2027     if(x < nx && nx < ex && y < ny && ny < ey) return true;
2028     }
2029     }
2030 isao-hara 2 return false;
2031    
2032 isao-hara 3 return res;
2033 isao-hara 2 break;
2034     case 'text':
2035     case 'rect':
2036     case 'polygon':
2037     case 'polyline':
2038     case 'image':
2039 isao-hara 65 case 'g':
2040 isao-hara 2 break;
2041     default:
2042     res=false;
2043     break;
2044     }
2045     return res;
2046     }
2047    
2048     function getBoundingBox(obj){
2049     var res = new Array(4);
2050     var bbox = obj.getBBox();
2051     res[0] = bbox.x-1;
2052     res[1] = bbox.y-1;
2053     res[2] = bbox.x+bbox.width+2;
2054     res[3] = bbox.y+bbox.height+2;
2055     return res;
2056     }
2057    
2058     function setSelectBox(){
2059     if(!svg_select) return;
2060    
2061     if(selectedItems.length == 0){
2062 isao-hara 51 hideSelectedRectangle()
2063 isao-hara 2 return;
2064     }
2065    
2066 isao-hara 51 var bbox = new Array(1000,1000,0,0);
2067 isao-hara 2
2068     for(var i=0;i<selectedItems.length;i++){
2069     var bp = getBoundingBox(selectedItems[i]);
2070     if(bp[0] < bbox[0]) bbox[0]=bp[0];
2071     if(bp[1] < bbox[1]) bbox[1]=bp[1];
2072     if(bp[2] > bbox[2]) bbox[2]=bp[2];
2073     if(bp[3] > bbox[3]) bbox[3]=bp[3];
2074     }
2075 isao-hara 51 selectedRectangle(bbox[0], bbox[1], bbox[2], bbox[3]);
2076 isao-hara 2 }
2077    
2078     function getSelectedObjects(x1, y1, x2, y2){
2079     if(x1 > x2) { var tmp = x1; x1=x2; x2=tmp; }
2080     if(y1 > y2) { var tmp = y1; y1=y2; y2=tmp; }
2081    
2082 isao-hara 3 var val="";
2083 isao-hara 2 if(svg_top){
2084     var val ="";
2085     var objs = svg_top.childNodes;
2086     selectedItems = new Array();
2087     for(var i=0; i<objs.length;i++){
2088     if(objs[i].tagName){
2089 isao-hara 3
2090 isao-hara 2 if(objs[i] != svg_select && checkIntersection(objs[i], x1, y1, x2, y2)){
2091     selectedItems.push(objs[i]);
2092     }
2093 isao-hara 3 val += objs[i].tagName+" ";
2094 isao-hara 2 }
2095     }
2096     }
2097     setSelectBox();
2098     }
2099    
2100 isao-hara 42 function dupObject(){
2101     if(selectedItems.length == 0){ return; }
2102     dupItems = selectedItems;
2103     dupX = parseInt(svg_select.getAttribute("x"));
2104     dupY = parseInt(svg_select.getAttribute("y"));
2105     setSVGMode('Duplicate');
2106     }
2107    
2108     function pasteObject(x,y){
2109     if(selectedItems.length == 0){ return; }
2110     for(var i=0;i<dupItems.length;i++){
2111     var itm = dupItems[i].cloneNode(true);
2112 isao-hara 47 itm.setAttribute("id", newID());
2113 isao-hara 42 replaceTranslate(itm,x-dupX,y-dupY);
2114     updateTransform(itm);
2115     appendSVGObj(itm);
2116     }
2117     }
2118    
2119 isao-hara 19 function onTouchStartCore(){
2120 isao-hara 35 if((!modeSVG || modeSVG == 'selector') && selectedItems.length == 0){ // Selector Mode
2121     var x1=getPreviewX(sx-1);
2122     var y1=getPreviewY(sy-1);
2123     var x2=getPreviewX(sx+2);
2124     var y2=getPreviewY(sy+2);
2125     getSelectedObjects(x1, y1, x2, y2);
2126    
2127     if(selectedItems.length == 0){
2128     setSVGMode('selector');
2129     }else if(selectedItems.length == 1){
2130     targetItem=selectedItems[0];
2131     }else{
2132     setSVGMode('Group');
2133     }
2134     }else { // CreateMode
2135     if(selectedItems.length == 0){
2136 isao-hara 2
2137 isao-hara 35 var fill=document.getElementById('svg_fill');
2138     var color=document.getElementById('svg_color');
2139     var L=document.getElementById('svg_stroke');
2140 isao-hara 2
2141 isao-hara 35 var x = getPreviewX(sx);
2142     var y = getPreviewY(sy);
2143 isao-hara 2
2144 isao-hara 35 switch(modeSVG){
2145     case 'text':
2146     var txt=document.getElementById('svg_text');
2147     var size=document.getElementById('svg_size');
2148     if(txt.value){
2149 isao-hara 64 y = y + parseInt(size.value)*0.8;
2150     targetItem=createSVGText(txt.value, x, y, size.value, color.value);
2151 isao-hara 35 }else{
2152 isao-hara 64 putInputForm(x, y, txt.value, size.value);
2153 isao-hara 35 }
2154     break;
2155     case 'rect':
2156     var attr = 'x='+x+',y='+y+',width='+svg_width+',height='+svg_height;
2157     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2158     break;
2159     case 'circle':
2160     var attr = 'cx='+x+',cy='+y+',r='+svg_rx;
2161     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2162     break;
2163     case 'ellipse':
2164     var attr = 'cx='+x+',cy='+y+',rx='+svg_rx+',ry='+svg_ry;
2165     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2166     break;
2167     case 'newPath':
2168 isao-hara 56 //var attr = 'd=M '+x+' '+y+' L '+x+' '+y;
2169     var attr = 'd=M '+x+' '+y+' L';
2170 isao-hara 35 targetItem=createSVGObj('path' ,attr, 'none', color.value, L.value);
2171 isao-hara 56 is_newPath=true;
2172 isao-hara 35 break;
2173     case 'newLine':
2174     var x2=x+1;
2175     var attr = 'x1='+x+',y1='+y+',x2='+x2+',y2='+y;
2176     targetItem=createSVGObj('line' ,attr, 'none', color.value, L.value);
2177     break;
2178     case 'image':
2179     var attr = 'x='+x+',y='+y;
2180     var txt=document.getElementById('svg_text');
2181     var w=document.getElementById('svg_w');
2182     var h=document.getElementById('svg_h');
2183     if(txt.value) targetItem=createSVGImage(txt.value ,w.value, h.value, attr);
2184     default:
2185     break;
2186 isao-hara 2 }
2187 isao-hara 35 if (targetItem){
2188     appendSVGObj(targetItem);
2189     selectedItems[0]=targetItem;
2190     }
2191     }else{
2192     var x1=getPreviewX(sx-1);
2193     var y1=getPreviewY(sy-1);
2194     var x2=getPreviewX(sx+2);
2195     var y2=getPreviewY(sy+2);
2196 isao-hara 42
2197     if(modeSVG == 'Duplicate'){ pasteObject(x1,y1); }
2198     if(modeSVG == 'newPath'){
2199     if(targetItem.tagName == 'path' ){
2200     var path = targetItem.getAttribute("d");
2201     path = path + ' '+ getPreviewX(sx) + ' '+ getPreviewY(sy) ;
2202     targetItem.setAttribute("d",path);
2203     }
2204     return;
2205     }
2206 isao-hara 35 if(!checkIntersection(svg_select, x1, y1, x2, y2)){
2207     setSVGMode('selector');
2208     }
2209 isao-hara 2 }
2210 isao-hara 35 }
2211 isao-hara 2 }
2212    
2213 isao-hara 25
2214     function onDoubleTap(e){
2215     if(selectedItems.length == 1 ){
2216 isao-hara 51 hideSelectedRectangle();
2217 isao-hara 29
2218 isao-hara 25 var obj = selectedItems[0];
2219 isao-hara 51 if(!obj) return;
2220 isao-hara 25 switch(obj.tagName){
2221     case 'svg:text':
2222 isao-hara 29 case 'text':
2223 isao-hara 25 var txt = trim(obj.textContent);
2224     var size = parseInt(obj.getAttribute("font-size"));
2225     var x = parseInt(obj.getAttribute("x"));
2226     var y = parseInt(obj.getAttribute("y"));
2227     x = x-20;
2228     y = y-size*0.8 -10;
2229     putInputForm(x, y, txt, size, obj.id);
2230     obj.style.display = 'none';
2231     editingTextObj = obj;
2232     break;
2233 isao-hara 42 case 'path':
2234     if(modeSVG == 'newPath'){ setSVGMode('selector'); }
2235     break;
2236 isao-hara 25 default:
2237     break;
2238     }
2239 isao-hara 29 return false;
2240 isao-hara 25 }
2241     }
2242    
2243 isao-hara 2 function getPoints(d){
2244     var p = d.split(' ');
2245     var res = new Array();
2246     var isx=true;
2247     var x, y;
2248     for(var i=0; i<p.length;i++){
2249     if(p[i].match('[MLHVCSQTA]','i')){
2250     }else{
2251     if(isx){
2252     x = parseInt(p[i]);
2253     }else{
2254     y = parseInt(p[i]);
2255     res.push(new Array(x, y));
2256     }
2257     isx = !isx;
2258     }
2259     }
2260     return res;
2261     }
2262    
2263     function updatePath(d, x, y){
2264     var p = d.split(' ');
2265     var res = "";
2266     var isx=true;
2267     var val;
2268     for(var i=0; i<p.length;i++){
2269 isao-hara 42 if(trim(p[i]) == "") continue;
2270 isao-hara 2 if(p[i].match('[MLHVCSQTA]','i')){
2271     res += ' '+p[i];
2272     }else{
2273     if(isx){
2274     val = parseInt(p[i])+x;
2275     }else{
2276     val = parseInt(p[i])+y;
2277     }
2278    
2279     res += ' '+ val;
2280     isx = !isx;
2281     }
2282     }
2283     return trim(res);
2284     }