Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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