Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (hide annotations) (download) (as text)
Mon Jul 26 00:46:07 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 67292 byte(s)
delete manifast settings
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     // Show File
725     function updateShowMenu(){
726     var menuDiv = document.getElementById('menuDiv');
727     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
728     menuDiv.innerHTML+= "<button onClick=\"editCurrentFile();\"> Edit </button>\n";
729     menuDiv.innerHTML+= "<button onClick=\"saveData();\"> Save </button>\n";
730     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
731     menuDiv.innerHTML+= "<button onClick=\"showSVGSource();\"> &lt;SVG &gt; </button>\n";
732     menuDiv.innerHTML+= updateSVGObjMenu();
733     menuDiv.style.display='block';
734     updateToolBar();
735     }
736    
737 isao-hara 25 function showColorPalette(val){
738     var palette = document.getElementById('color-palette');
739     var ele;
740     var current = palette.getAttribute("targetType");
741 isao-hara 2
742 isao-hara 25 if(palette.style.display=='block' && current == val){
743     palette.style.display='none';
744     return;
745     }
746    
747     if(val == 'fill'){
748     ele = document.getElementById('toolFill');
749     }else{
750     ele = document.getElementById('toolStroke');
751     }
752    
753     var pos= ele.offsetTop + 110;
754     palette.style.top = pos +"px";
755     palette.style.display='block';
756     palette.setAttribute("targetType", val);
757    
758     }
759    
760 isao-hara 2 function updateToolBar(){
761     var toolbar = document.getElementById('toolBar');
762 isao-hara 25 var str = "";
763 isao-hara 14 if(!toolbar.innerHTML){
764 isao-hara 25 str += "<li><button id=\"b_selector\" onClick=\"setSVGMode('selector');\"><img src=\"images/arrow.png\" /></button> </li>\n";
765     str += "<li><button id=\"b_newPath\" onClick=\"setSVGMode('newPath'); \"><img src=\"images/path.png\" /></button></li> \n";
766     str += "<li><button id=\"b_newLine\" onClick=\"setSVGMode('newLine'); \"><img src=\"images/line.png\" /> </button></li> \n";
767     str += "<li><button id=\"b_text\" onClick=\"setSVGMode('text'); \"><img src=\"images/text.png\" /></button> </li>\n";
768     str += "<li><button id=\"b_rect\" onClick=\"setSVGMode('rect'); \"> <img src=\"images/rect.png\" /> </button></li> \n";
769     str += "<li><button id=\"b_circle\" onClick=\"setSVGMode('circle'); \"> <img src=\"images/circle.png\" /> </button></li> \n";
770     str += "<li><button id=\"b_ellipse\" onClick=\"setSVGMode('ellipse'); \"> <img src=\"images/ellipse.png\" /> </button></li> \n";
771     str += "<li><button id=\"b_image\" onClick=\"setSVGMode('image'); \"> <img src=\"images/img.png\" /> </button></li> \n";
772     str += "<li>&nbsp;</li>";
773     str += "<li><button id=\"b_fillColor\" onClick=\"showColorPalette('fill');\"><img src=\"images/Fill.png\" /><div id=\"toolFill\"></div></button></li> \n";
774     str += "<li><button id=\"b_strokeColor\" onClick=\"showColorPalette('stroke');\"><img src=\"images/Stroke.png\" /><div id=\"toolStroke\"></div></button></li> \n";
775     toolbar.innerHTML= "<ul>"+str+"</ul>";
776 isao-hara 14 }
777 isao-hara 25
778 isao-hara 2 toolbar.style.display='block';
779     }
780    
781     function setMode(m){
782     currentMode=m;
783     switch(m){
784     case 'List':
785     hideItemById('editDiv');
786     hideItemById('preview');
787     hideItemById('popup');
788     hideItemById('toolBar');
789 isao-hara 14 hideItemById('color-palette');
790 isao-hara 2 showItemById('menuDiv');
791     break;
792     case 'Edit':
793     hideItemById('preview');
794     hideItemById('popup');
795     hideItemById('toolBar');
796 isao-hara 14 hideItemById('color-palette');
797 isao-hara 2 showItemById('editDiv');
798     showItemById('menuDiv');
799     break;
800     case 'Preview':
801     showItemById('preview');
802     hideItemById('editDiv');
803     hideItemById('popup');
804 isao-hara 25 hideItemById('color-palette');
805 isao-hara 2 showItemById('menuDiv');
806     showItemById('toolBar');
807     break;
808     default:
809     break;
810     }
811     }
812    
813     function hideItemById(id){
814     var itm = document.getElementById(id);
815     if(itm) itm.style.display='none';
816     }
817    
818     function showItemById(id){
819     var itm = document.getElementById(id);
820     if(itm) itm.style.display='block';
821     }
822    
823     function removeChildNodes(id){
824     var itm = document.getElementById(id);
825     if(!itm) return;
826    
827     var child = itm.firstChild;
828    
829     while(child){
830     itm.removeChild(child);
831     child = itm.firstChild;
832     }
833     }
834    
835     function chEditareaHeight(){
836     var itm = document.getElementById('editarea');
837     if(!itm) return;
838     var cv = itm.getAttribute('rows');
839     if(parseInt(cv) > editarea_h){
840     itm.setAttribute('rows', editarea_h);
841     }else{
842     itm.setAttribute('rows', editarea_h * 2);
843     }
844     }
845    
846     function format_file(str){
847     return "<pre>"+str+"</pre>";
848     }
849    
850     function previewData(data){
851     if(!data) data = document.getElementById('editarea').value;
852    
853     previewFile(data);
854    
855     updateShowMenu();
856     setMode('Preview');
857     }
858    
859     function previewFile(data){
860     removeChildNodes('preview');
861    
862     preview.style.display='block';
863     preview.style.position='absolute';
864     preview.style.top='180px';
865 isao-hara 14 preview.style.bottom='50px';
866     preview.style.left='0px';
867     preview.style.right='10px';
868 isao-hara 21 preview.style.width='800px';
869     // preview.style.height='600px';
870     preview.style.height='525px';
871 isao-hara 14 mkColorPalette();
872 isao-hara 2
873     var ele = toSVGElement(data, '100%','100%');
874    
875     preview.appendChild(ele);
876    
877     svg_top = document.getElementById('svg_top');
878     initSVGElementId(svg_top);
879     svg_select = createSVGObj('rect', 'x=1,y=1,width=1,height=1,visibility=hidden,stroke-dasharray=9 5', 'none', 'blue', 2);
880     svg_select.setAttribute("id","svg_select");
881     appendSVGObj(svg_select);
882 isao-hara 14 }
883 isao-hara 2
884 isao-hara 14 function mkColorPalette(){
885     var palette = document.getElementById('color-palette');
886     if(!palette) return;
887     if(palette.innerHTML) return;
888    
889     palette.innerHTML="";
890 isao-hara 32 palette.addEventListener("touchstart", onTouchStartColor, false);
891 isao-hara 30
892 isao-hara 14 for(var i=0; i<colors.length ;i++){
893     if(colors[i] == 'none')
894     palette.innerHTML +="<div class=\"item\" style=\"background-color:"+colors[i]+";\" color-val=\""+colors[i]+"\">X</div>";
895     else
896     palette.innerHTML +="<div class=\"item\" style=\"background-color:"+colors[i]+";\" color-val=\""+colors[i]+"\"> </div>";
897     }
898 isao-hara 25 palette.style.width='120px';
899 isao-hara 2 }
900    
901     ////// for SVG object
902     ////////////////
903     function updateSVGObjMenu(){
904     var res = "";
905     var tag = modeSVG;
906     if(selectedItems.length > 1) return res;
907     if(selectedItems.length == 1) tag = getElementTag(selectedItems[0]);
908    
909     switch(tag){
910     case 'text':
911     if(selectedItems[0]){
912     res += setSVGObjectProp(selectedItems[0]);
913     }else{
914 isao-hara 25 res += propSVGText("", svg_font_size, svg_color);
915 isao-hara 2 }
916     break;
917     case 'rect':
918     case 'circle':
919     case 'ellipse':
920     case 'newPath':
921     case 'newLine':
922     case 'path':
923     case 'line':
924     case 'polyline':
925     case 'polygon':
926     if(selectedItems[0]){
927     res += setSVGObjectProp(selectedItems[0]);
928     }else{
929 isao-hara 25 res += propSVGObj(tag+":", svg_line_width, svg_color, svg_fill_color);
930 isao-hara 2 }
931     break;
932 isao-hara 5 case 'image':
933     if(selectedItems[0]){
934     res += setSVGObjectProp(selectedItems[0]);
935     }else{
936 isao-hara 6 res += propSVGImage("", 100, 100);
937 isao-hara 5 }
938     break;
939 isao-hara 2 default:
940     break;
941     }
942     return res;
943     }
944    
945     function propSVGText(str, size, color){
946     if(!size) size = 24;
947     if(!color) color = '#000000';
948    
949     var res = "Text:";
950 isao-hara 28 res += "<input type=\"hidden\" id=\"svg_text\" value=\""+str+"\"/>";
951     res += "<input type=\"hidden\" id=\"svg_color\" value=\""+color+"\" size=\"8\"/>";
952 isao-hara 2 res += "Size:<input type=\"text\" id=\"svg_size\" value=\""+size+"\" size=\"4\"/>";
953 isao-hara 25
954     document.getElementById('toolFill').style.backgroundColor=color;
955 isao-hara 2 return res;
956     }
957    
958     function propSVGObj(type, stroke, color, fill){
959     var res = type;
960     if(!stroke) stroke = 1;
961     if(!color) color = '#000000';
962     if(!fill) fill = '#ffffff';
963 isao-hara 28 res += "<input type=\"hidden\" id=\"svg_fill\" value=\""+fill+"\" size=\"8\"/>";
964     res += "<input type=\"hidden\" id=\"svg_color\" value=\""+color+"\" size=\"8\"/>";
965     res += "Width:<input type=\"text\" id=\"svg_stroke\" value=\""+stroke+"\" size=\"2\"/>";
966 isao-hara 25
967     document.getElementById('toolFill').style.backgroundColor=fill;
968     document.getElementById('toolStroke').style.backgroundColor=color;
969 isao-hara 2 return res;
970     }
971    
972     function propSVGLine(type, stroke, color, fill, style){
973     var res = propSVGObj(type, stroke, color, fill);
974 isao-hara 14 if(style==null) style="";
975 isao-hara 2 res += "Type:<input type=\"text\" id=\"svg_stroke_type\" value=\""+style+"\" size=\"8\"/>";
976     res += "Arrow:";
977     if(hasArrow('start')){
978 isao-hara 14 res += "<button class=\"tool\" onClick=\"removeLeftArrow();\"><img src=\"images/normal.png\" /></button>";
979 isao-hara 2 }else{
980 isao-hara 14 res += "<button class=\"tool\" onClick=\"setLeftArrow();\"><img src=\"images/larrow.png\" /></button>";
981 isao-hara 2 }
982     if(hasArrow('end')){
983 isao-hara 14 res += "<button class=\"tool\" onClick=\"removeRightArrow();\"><img src=\"images/normal.png\" /></button>";
984 isao-hara 2 }else{
985 isao-hara 14 res += "<button class=\"tool\" onClick=\"setRightArrow();\"><img src=\"images/rarrow.png\" /></button>";
986 isao-hara 2 }
987 isao-hara 25
988     document.getElementById('toolFill').style.backgroundColor=fill;
989     document.getElementById('toolStroke').style.backgroundColor=color;
990    
991 isao-hara 2 return res;
992     }
993    
994 isao-hara 5 function propSVGImage(str, w, h){
995     var res = "image:";
996     res += "<input type=\"text\" id=\"svg_text\" value=\""+str+"\" />";
997     res += "Width:<input type=\"text\" id=\"svg_w\" value=\""+w+"\" size=\"4\"/>";
998     res += "Height:<input type=\"text\" id=\"svg_h\" value=\""+h+"\" size=\"4\"/>";
999     return res;
1000     }
1001    
1002 isao-hara 2 function setSVGMode(m){
1003 isao-hara 14 var btn = document.getElementById('b_'+modeSVG);
1004     if(btn){ btn.style.border="1px solid black"; }
1005    
1006 isao-hara 2 modeSVG = m;
1007     updateShowMenu();
1008 isao-hara 4
1009 isao-hara 2 if(m == 'selector'){
1010     targetItem=null;
1011     clearSelectedItems();
1012     }
1013 isao-hara 4 var btn = document.getElementById('b_'+modeSVG);
1014 isao-hara 14 if(btn){ btn.style.border="3px solid red"; }
1015 isao-hara 2 }
1016    
1017    
1018     function toSVGElement(str, w, h){
1019     var xmlsvg = "xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
1020     var parser = new DOMParser();
1021     var header = "<svg:svg width=\""+w+"\" height=\""+h+"\" "+xmlsvg+" id=\"svg_top\">";
1022     var footer = "</svg:svg>";
1023     var xmlDoc = parser.parseFromString(header+str+footer, "text/xml");
1024     var xmlRoot = xmlDoc.documentElement;
1025     var ele = document.importNode(xmlRoot,true);
1026    
1027     return ele;
1028     }
1029    
1030     function createSVGElement(tag, id){
1031     if(id=='new'){
1032     id = 'svg_'+nextId;
1033     nextId++;
1034     }
1035     var ele= document.createElementNS(svg_ns, tag);
1036     ele.setAttribute("id", id);
1037     return ele;
1038     }
1039    
1040     function defSVGElement(node){
1041     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1042     if(!svg_defs){
1043     svg_defs = createSVGElement('defs', 'svg_defs');
1044     svg_top.insertBefore(svg_defs, svg_top.firstChild);
1045     }
1046     if(node) svg_defs.appendChild(node);
1047     }
1048    
1049     function initSVGElementId(svg_top){
1050     nextId = 1;
1051     checkSVGElementId(svg_top);
1052     appendSVGElementId(svg_top);
1053     }
1054    
1055     function checkSVGElementId(top){
1056     var objs = top.childNodes;
1057    
1058     for(var i=0; i<objs.length ;i++){
1059     if(objs[i].tagName){
1060     var id = objs[i].getAttribute("id");
1061    
1062     if(id && id.match(/svg_[0-9]+/i)){
1063     var n = parseInt(RegExp.lastMatch.substr(4));
1064     if(n >= nextId){
1065     nextId = n+1;
1066     }
1067    
1068     }
1069     checkSVGElementId(objs[i]);
1070     }
1071     }
1072     }
1073    
1074     function appendSVGElementId(top){
1075     var objs = top.childNodes;
1076    
1077     for(var i=0; i<objs.length ;i++){
1078     if(objs[i].tagName){
1079     var id = objs[i].getAttribute("id");
1080     if(!id){
1081     objs[i].setAttribute("id", "svg_"+nextId);
1082     nextId++;
1083     }
1084     }
1085     appendSVGElementId(objs[i]);
1086     }
1087     }
1088    
1089     function selectedRectangle(x, y, ex, ey){
1090     if(!svg_select) return null;
1091    
1092     svg_select.setAttribute("x",x);
1093     svg_select.setAttribute("y",y);
1094     svg_select.setAttribute("width", ex-x);
1095     svg_select.setAttribute("height", ey-y);
1096     svg_select.setAttribute("visibility", "visible");
1097     return svg_select;
1098     }
1099    
1100    
1101     function setAttributes(obj, attrs){
1102     var attr_array = attrs.split(',');
1103    
1104     for (var i=0; i<attr_array.length;i++){
1105     var x = attr_array[i].split('=');
1106     if(x.length == 2){
1107     obj.setAttribute(x[0], x[1]);
1108     }
1109     }
1110     }
1111    
1112     function createSVGObj(tag, attrs, fill, color, lw){
1113     var ele = createSVGElement(tag, 'new');
1114     setAttributes(ele, attrs);
1115     if (fill) ele.setAttribute('fill', fill);
1116     if (color) ele.setAttribute('stroke', color);
1117     if(lw) ele.setAttribute('stroke-width', lw);
1118    
1119     return ele;
1120     }
1121    
1122     function createSVGText(txt, x, y, size, color){
1123     var ele = createSVGElement('text', 'new');
1124    
1125     ele.setAttribute('x', x);
1126     ele.setAttribute('y', y);
1127     ele.setAttribute('font-size', size);
1128     ele.setAttribute('fill', color);
1129     ele.textContent=txt;
1130    
1131     return ele;
1132     }
1133    
1134     function createSVGImage(fname, width, height, attrs){
1135     var ele = createSVGElement('image', 'new');
1136 isao-hara 7 ele.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', fname);
1137 isao-hara 2 ele.setAttribute('width', width);
1138     ele.setAttribute('height', height);
1139     setAttributes(ele, attrs);
1140    
1141     return ele;
1142     }
1143    
1144     function createSVGMarker(pid, id, child){
1145     var parent = document.getElementById(pid);
1146     if(!parent) return;
1147     var ele = createSVGElement('marker', pid+'_'+id);
1148     ele.appendChild(child);
1149     return ele;
1150     }
1151    
1152     function setLeftArrow(){
1153     if( selectedItems.length == 1 ){
1154     setArrow(selectedItems[0], 'start', '');
1155     }
1156     }
1157    
1158     function setRightArrow(){
1159     if( selectedItems.length == 1 ){
1160     setArrow(selectedItems[0], 'end', '');
1161     }
1162     }
1163    
1164     function removeLeftArrow(){
1165     if( selectedItems.length == 1 ){
1166     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1167     var pid = selectedItems[0].getAttribute('id');
1168     var marker = document.getElementById(pid+'_startarrow');
1169     svg_defs.removeChild(marker);
1170     selectedItems[0].removeAttribute('marker-start');
1171     }
1172     }
1173    
1174     function removeRightArrow(){
1175     if( selectedItems.length == 1 ){
1176     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1177     var pid = selectedItems[0].getAttribute('id');
1178     var marker = document.getElementById(pid+'_endarrow');
1179     svg_defs.removeChild(marker);
1180     selectedItems[0].removeAttribute('marker-end');
1181     }
1182     }
1183    
1184     function hasArrow(pos){
1185     var pobj = selectedItems[0];
1186     if(!pobj) return false;
1187     var pid = pobj.getAttribute('id');
1188     var marker = document.getElementById(pid+'_'+pos+'arrow');
1189     if(marker) return true;
1190     return false;
1191     }
1192    
1193     function getArrowMarker(obj,pos){
1194     if(!obj) return false;
1195     var pid = obj.getAttribute('id');
1196     var marker = document.getElementById(pid+'_'+pos+'arrow');
1197     return false;
1198     }
1199    
1200     function setArrow(pobj, pos, type){
1201     if(!pobj) return;
1202     var pid = pobj.getAttribute('id');
1203     var marker = document.getElementById(pid+'_'+pos+'arrow');
1204     if(marker) return;
1205    
1206     var obj = createSVGElement('path', 'new');
1207    
1208     var refX = 0;
1209     var refY = 0;
1210    
1211     switch(pos){
1212     case 'start':
1213     refX=10;
1214     refY=10;
1215     setAttributes(obj,'d=M 20 0 L 0 10 20 20 Z');
1216     break;
1217     case 'end':
1218     setAttributes(obj,'d=M 0 0 L 20 10 0 20 Z');
1219     refX= 10;
1220     refY=10;
1221     break;
1222     default:
1223     return;
1224     }
1225    
1226     marker = createSVGMarker(pid, pos+'arrow' , obj);
1227     setAttributes(marker,'markerWidth=10,markerHeight=10,orient=auto,viewBox=0 0 20 20,markerUnits=strokeWidth,refX='+refX+',refY='+refY);
1228    
1229     marker.setAttribute("fill",pobj.getAttribute("stroke"));
1230    
1231     defSVGElement(marker);
1232     var mid = "url(#"+marker.getAttribute("id")+")";
1233     var mattr = "marker-"+pos;
1234     pobj.setAttribute(mattr, mid);
1235     }
1236    
1237     function appendSVGObj(obj){
1238     var svg_top = document.getElementById('svg_top');
1239     if(!svg_top) return;
1240    
1241     svg_top.appendChild(obj);
1242     }
1243    
1244     function isChildById(element, id) {
1245     if (element == null || element.parentNode == null || element.parentNode.nodeName=='BODY') return false;
1246     else if (element.parentNode.id == id) return true;
1247     else return isChildById(element.parentNode, id);
1248     }
1249    
1250     /////////////////////// Formatting SVG DOM
1251     function escapeHTML(text) {
1252     return text.replace( /[<>"&]/g,
1253     function (m) { return { '<': '&lt;', '>': '&gt;', '"': '&quot;', '&': '&amp;' }[m]; }
1254     );
1255     };
1256    
1257     function formatTag(ele){
1258     var str="";
1259     if(ele.nodeType == 1){
1260     var tag_a = ele.tagName.split(':');
1261     var tag;
1262     if(tag_a.length == 1){ tag = "svg:"+tag_a[0]; }else{ tag = ele.tagName; }
1263    
1264     str += "<"+tag;
1265     var attrs = ele.attributes;
1266     for(var i=0; i<attrs.length; i++){
1267     str += " "+attrs[i].nodeName+"=\""+attrs[i].nodeValue+"\"";
1268     }
1269     var cn = ele.childNodes;
1270     if(cn.length > 0){
1271     str +=">\n";
1272     for(var i=0; i<cn.length; i++){
1273 isao-hara 13 var tmp = trim(formatTag(cn[i]));
1274     if(tmp) str += " "+tmp+"\n";
1275 isao-hara 2 }
1276     str += "</"+tag+">";
1277     }else{
1278     str +=" />";
1279     }
1280     return str;
1281     }else if(ele.nodeType==3){
1282     return ele.textContent;
1283     }
1284     }
1285    
1286     function getSVGContent(){
1287     if(!svg_top) return "";
1288    
1289     var str = "";
1290     var elements = svg_top.childNodes;
1291     for(var i=0; i<elements.length; i++){
1292 isao-hara 12 if(elements[i] != svg_select){
1293     var tmp = trim(formatTag(elements[i]));
1294     if(tmp) str += tmp + '\n';
1295     }
1296 isao-hara 2 }
1297     return str;
1298     }
1299    
1300     function trim(str){
1301     return str.replace(/(^\s+)|(\s+$)/g, "");
1302     }
1303    
1304     /////// Access Server
1305     function newXMLRequest(){
1306     if(this.XMLHttpRequest){
1307     return new XMLHttpRequest();
1308     }else {
1309     return new ActiveXObject("Microsoft.XMLHTTP");
1310     }
1311     }
1312    
1313     function createRequestData(data){
1314     var str="filetype=svg";
1315     for (var i in data){
1316     str = str +"&"+ i +"="+encodeURIComponent(data[i]);
1317     }
1318     return str;
1319     }
1320    
1321     function postRequest(url, data, func){
1322     var postData=createRequestData(data);
1323     var obj=newXMLRequest();
1324    
1325     obj.onreadystatechange = function(){
1326     if (obj.readyState == 4 && obj.status == 200){
1327     func(obj.responseText);
1328     }
1329     }
1330     obj.open("POST", url, true);
1331     obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
1332     obj.send(postData);
1333     }
1334    
1335     function commnadFinishAlert(s){
1336     alert(s);
1337     }
1338    
1339     function uploadFileData(transaction, results){
1340     var data = results.rows.item(0);
1341     var updata = new Array(0);
1342     updata['name']= data['name'];
1343     updata['datalob'] = data['datablob'];
1344     updata['cmd'] = 'upload'
1345    
1346     postRequest(MgrPath+iSlideMgr, updata, commnadFinishAlert);
1347     }
1348    
1349     function uploadFile(name){
1350     getFile(name, 'uploadFileData');
1351     }
1352    
1353     function getRemoteFileList(){
1354     var data=new Array();
1355     data['name'] = "SVG";
1356     data['cmd'] = "list";
1357     postRequest(MgrPath+iSlideMgr, data, fileSelectorOnServer);
1358     }
1359    
1360     function previewRemoteFile(content){
1361     previewFile(content);
1362     setMode('Preview');
1363     }
1364    
1365     function getRemoteFile(name){
1366     var data=new Array();
1367     data['name'] = name;
1368     data['cmd'] = "get";
1369     postRequest(MgrPath+iSlideMgr, data, previewRemoteFile);
1370     }
1371    
1372 isao-hara 16 function saveRemoteFile(content){
1373 isao-hara 17 var datadiv = document.getElementById('tempdata');
1374     var fname = datadiv.getAttribute('lfname');
1375     saveContentWithFilename(fname, content);
1376 isao-hara 16 }
1377    
1378     function downloadFile(name){
1379     var data=new Array();
1380     data['name'] = name;
1381     data['cmd'] = "get";
1382     var datadiv = document.getElementById('tempdata');
1383     datadiv.setAttribute('lfname', name);
1384     postRequest(MgrPath+iSlideMgr, data, saveRemoteFile);
1385     }
1386    
1387 isao-hara 2 function showRemoteFile(name){
1388     getRemoteFile(name);
1389     }
1390    
1391 isao-hara 16 function downloadToFile(name){
1392     var downloadForm = "";
1393     downloadForm += "<form action=\"iSlideManager.php\" method=\"post\">";
1394     downloadForm += "<input type=\"hidden\" name=\"cmd\" value=\"download\">";
1395     downloadForm += "<input type=\"hidden\" name=\"name\" value=\""+name+"\">";
1396     downloadForm += "<input type=\"hidden\" name=\"filetype\" value=\"svg\">";
1397     downloadForm += "<input type=\"submit\">";
1398     downloadForm += "</form>";
1399    
1400     var cmdForm = document.getElementById('cmdForm');
1401     cmdForm.innerHTML = downloadForm;
1402     cmdForm.firstChild.submit();
1403     }
1404    
1405 isao-hara 2 //////// Event Handler
1406     function setInnerHTML(id, val){
1407     var itm=document.getElementById(id);
1408     if(itm) itm.innerHTML=val;
1409     }
1410    
1411     function popupInfo(val){
1412     var str="<button onClick=\"hideItemById('popup');\">Close</button><hr> ";
1413     str += val;
1414     setInnerHTML('popup', str);
1415     showItemById('popup');
1416     }
1417    
1418     function addAttributeVal(obj, itm, dv){
1419     if(obj.hasAttribute(itm)){
1420     var x = parseInt(obj.getAttribute(itm));
1421     x = x + dv;
1422     obj.setAttribute(itm, x)
1423     }
1424     }
1425    
1426     function showSVGSource(){
1427     var str = getSVGContent();
1428 isao-hara 14 var escstr="<button onClick=\"hideItemById('popup');\">Close</button>";
1429     escstr+="<button onClick=\"saveContent(document.getElementById('ContentView').value); previewData(document.getElementById('ContentView').value);\">Save</button><hr> ";
1430     // escstr += "<pre>"+escapeHTML(str)+"</pre>";
1431     escstr += "<textarea cols=\"130\" rows=\"30\" id=\"ContentView\">"+str+"</textarea>";
1432 isao-hara 2 setInnerHTML('popup', escstr);
1433     showItemById('popup');
1434     }
1435    
1436    
1437     function getElementTag(obj){
1438 isao-hara 6 if (!obj) return null;
1439 isao-hara 2 var tag = obj.tagName.split(':');
1440     if(tag.length == 2 && tag[0]=='svg') return tag[1];
1441     return tag[0];
1442     }
1443    
1444     function setSVGObjectProp(obj){
1445     var tag = getElementTag(obj);
1446     var res ="";
1447     switch(tag){
1448     case 'text':
1449     res = propSVGText(obj.textContent, obj.getAttribute("font-size"), obj.getAttribute("fill"));
1450     break;
1451     case 'rect':
1452     case 'circle':
1453     case 'ellipse':
1454     res = propSVGObj(tag+":", obj.getAttribute("stroke-width"), obj.getAttribute("stroke"), obj.getAttribute("fill"));
1455     break;
1456     case 'path':
1457     case 'line':
1458     res = propSVGLine(tag+":",obj.getAttribute("stroke-width"),
1459     obj.getAttribute("stroke"),obj.getAttribute("fill"), obj.getAttribute("stroke-dasharray"));
1460     break;
1461 isao-hara 5 case 'image':
1462     res = propSVGImage(obj.getAttribute("xlink:href"),obj.getAttribute("width"),obj.getAttribute("height"));
1463     break;
1464 isao-hara 2 default:
1465     break;
1466     }
1467     if(res != ""){
1468 isao-hara 28 res += "<button onClick=\"updateSVGObj();\">Apply</button>";
1469 isao-hara 2 res += "<button onClick=\"delSVGObj();\">Del</button>";
1470     }
1471    
1472     return res
1473     }
1474    
1475     function updateSVGObj(){
1476     if(selectedItems.length != 1) return;
1477     var obj = selectedItems[0];
1478     var tag = getElementTag(obj);
1479     var res ="";
1480     switch(tag){
1481     case 'text':
1482     var txt=document.getElementById('svg_text');
1483     var color=document.getElementById('svg_color');
1484     var size=document.getElementById('svg_size');
1485     obj.textContent = txt.value;
1486     obj.setAttribute("fill", color.value);
1487     obj.setAttribute("font-size", size.value);
1488     break;
1489     case 'rect':
1490     case 'circle':
1491     case 'ellipse':
1492     var fill=document.getElementById('svg_fill');
1493     var color=document.getElementById('svg_color');
1494     var L=document.getElementById('svg_stroke');
1495     obj.setAttribute("fill", fill.value);
1496     obj.setAttribute("stroke", color.value);
1497     obj.setAttribute("stroke-width", L.value);
1498     break;
1499     case 'path':
1500     case 'line':
1501     var fill=document.getElementById('svg_fill');
1502     var color=document.getElementById('svg_color');
1503     var L=document.getElementById('svg_stroke');
1504     var dash=document.getElementById('svg_stroke_type');
1505     obj.setAttribute("fill", fill.value);
1506     obj.setAttribute("stroke", color.value);
1507     obj.setAttribute("stroke-width", L.value);
1508     if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1509     var id = obj.getAttribute("id");
1510     var marker = document.getElementById(id+'_startarrow');
1511     if(marker) marker.setAttribute("fill", color.value);
1512     var marker = document.getElementById(id+'_endarrow');
1513     if(marker) marker.setAttribute("fill", color.value);
1514     break;
1515 isao-hara 5 case 'image':
1516     var fname=document.getElementById('svg_text');
1517     var w=document.getElementById('svg_w');
1518     var h=document.getElementById('svg_h');
1519 isao-hara 6 obj.setAttribute("xlink:href", fname.value);
1520 isao-hara 5 obj.setAttribute("width", w.value);
1521     obj.setAttribute("height", h.value);
1522     break;
1523 isao-hara 2 default:
1524     break;
1525     }
1526    
1527     }
1528    
1529     function delSVGObj(){
1530     if(selectedItems.length != 1) return;
1531     var obj = selectedItems[0];
1532    
1533     if(!svg_top) return;
1534     svg_top.removeChild(obj);
1535     svg_select.setAttribute("visibility","hidden");
1536     }
1537    
1538     ///// For MobileSafari
1539     function getPreviewX(x){ return x - preview.offsetLeft; }
1540     function getPreviewY(y){ return y - preview.offsetTop; }
1541    
1542     function calcDit(x1, y1, x2, y2){
1543     return (x1-x2)*(x1-x2) +(y1-y2)*(y1-y2);
1544     }
1545    
1546     function isIncludeEllipse( x1, y1, cx, cy, rx, ry){
1547     return ((x1-cx)*(x1-cx)/rx/rx +(y1-cy)*(y1-cy)/ry/ry) < 1;
1548     }
1549    
1550     function getAttributeVal(obj, name){
1551     try{
1552     return parseInt(obj.getAttribute(name));
1553     }catch(e){ return 0; }
1554     }
1555    
1556     function checkIntersection(obj, x, y, ex, ey){
1557 isao-hara 18 if(!obj) return false;
1558 isao-hara 2 var res = true;
1559     var bbox = obj.getBBox();
1560     var ox = bbox.x;
1561     var oy = bbox.y;
1562     var oex = bbox.x+bbox.width;
1563     var oey = bbox.y+bbox.height;
1564     if( ex < ox || oex < x || ey < oy || oey < y) res = false;
1565    
1566     var tag = getElementTag(obj);
1567     switch(tag){
1568     case 'circle':
1569     case 'ellipse':
1570     var cx = getAttributeVal(obj,"cx");
1571     var cy = getAttributeVal(obj,"cy");
1572     var rx = getAttributeVal(obj,"r");
1573     var ry = rx;
1574     if(!rx){
1575     var rx = getAttributeVal(obj,"rx");
1576     var ry = getAttributeVal(obj,"ry");
1577     }
1578    
1579     if(res){
1580     if(cx <x && cy < y && !isIncludeEllipse(x,y,cx,cy,rx,ry)) res=false;
1581     else if(cx < x && cy > ey && !isIncludeEllipse(x,ey,cx,cy,rx,ry)) res=false;
1582     else if(cx > ex && cy > ey && !isIncludeEllipse(ex,ey,cx,cy,rx,ry)) res=false;
1583     else if(cx > ex && cy < y && !isIncludeEllipse( ex,y,cx,cy,rx,ry)) res=false;
1584     }
1585     break;
1586     case 'path':
1587     var d = obj.getAttribute("d");
1588     var p = getPoints(d);
1589     for(var i=0;i<p.length;i++){
1590     var ox=p[i][0];
1591     var oy=p[i][1];
1592     if(x < ox && ox < ex && y < oy && oy < ey) {
1593     return true;
1594     }
1595     }
1596     return false;
1597     break;
1598     case 'line':
1599     var x1 = getAttributeVal(obj,"x1");
1600     var y1 = getAttributeVal(obj,"y1");
1601     var x2 = getAttributeVal(obj,"x2");
1602     var y2 = getAttributeVal(obj,"y2");
1603 isao-hara 3 var d = (y2-y1)/(x2-x1);
1604    
1605     if(res){
1606     var xx = Math.abs(x2-x1);
1607     var sign = 1;
1608     if(x2-x1 < 0){ sign = -1; }
1609     for(var i=0; i < xx; i++){
1610     var nx = i*sign + x1;
1611     var ny = d * i*sign + y1;
1612     if(x < nx && nx < ex && y < ny && ny < ey) return true;
1613     }
1614     }
1615 isao-hara 2 return false;
1616    
1617 isao-hara 3 return res;
1618 isao-hara 2 break;
1619     case 'text':
1620     case 'rect':
1621     case 'polygon':
1622     case 'polyline':
1623     case 'image':
1624     break;
1625     default:
1626     res=false;
1627     break;
1628     }
1629     return res;
1630     }
1631    
1632     function getBoundingBox(obj){
1633     var res = new Array(4);
1634     var bbox = obj.getBBox();
1635     res[0] = bbox.x-1;
1636     res[1] = bbox.y-1;
1637     res[2] = bbox.x+bbox.width+2;
1638     res[3] = bbox.y+bbox.height+2;
1639     return res;
1640     }
1641    
1642     function setSelectBox(){
1643     if(!svg_select) return;
1644    
1645     if(selectedItems.length == 0){
1646     svg_select.setAttribute("visibility","hidden");
1647     return;
1648     }
1649    
1650     var bbox = new Array(4);
1651     bbox[0] = 1000;
1652     bbox[1] = 1000;
1653     bbox[2] = 0;
1654     bbox[3] = 0;
1655    
1656     for(var i=0;i<selectedItems.length;i++){
1657     var bp = getBoundingBox(selectedItems[i]);
1658     if(bp[0] < bbox[0]) bbox[0]=bp[0];
1659     if(bp[1] < bbox[1]) bbox[1]=bp[1];
1660     if(bp[2] > bbox[2]) bbox[2]=bp[2];
1661     if(bp[3] > bbox[3]) bbox[3]=bp[3];
1662     }
1663     svg_select.setAttribute("x", bbox[0]);
1664     svg_select.setAttribute("y", bbox[1]);
1665     svg_select.setAttribute("width", bbox[2] - bbox[0]);
1666     svg_select.setAttribute("height", bbox[3] - bbox[1]);
1667     svg_select.setAttribute("visibility","visible;");
1668     }
1669    
1670     function getSelectedObjects(x1, y1, x2, y2){
1671     if(x1 > x2) { var tmp = x1; x1=x2; x2=tmp; }
1672     if(y1 > y2) { var tmp = y1; y1=y2; y2=tmp; }
1673    
1674 isao-hara 3 var val="";
1675 isao-hara 2 if(svg_top){
1676     var val ="";
1677     var objs = svg_top.childNodes;
1678     selectedItems = new Array();
1679     for(var i=0; i<objs.length;i++){
1680     if(objs[i].tagName){
1681 isao-hara 3
1682 isao-hara 2 if(objs[i] != svg_select && checkIntersection(objs[i], x1, y1, x2, y2)){
1683     selectedItems.push(objs[i]);
1684     }
1685 isao-hara 3 val += objs[i].tagName+" ";
1686 isao-hara 2 }
1687     }
1688     }
1689     setSelectBox();
1690     }
1691    
1692 isao-hara 19 function onTouchStartCore(){
1693 isao-hara 35 if((!modeSVG || modeSVG == 'selector') && selectedItems.length == 0){ // Selector Mode
1694     var x1=getPreviewX(sx-1);
1695     var y1=getPreviewY(sy-1);
1696     var x2=getPreviewX(sx+2);
1697     var y2=getPreviewY(sy+2);
1698     getSelectedObjects(x1, y1, x2, y2);
1699    
1700     if(selectedItems.length == 0){
1701     setSVGMode('selector');
1702     }else if(selectedItems.length == 1){
1703     targetItem=selectedItems[0];
1704     setSVGMode(getElementTag(targetItem));
1705     }else{
1706     setSVGMode('Group');
1707     }
1708     }else { // CreateMode
1709     if(selectedItems.length == 0){
1710 isao-hara 2
1711 isao-hara 35 var fill=document.getElementById('svg_fill');
1712     var color=document.getElementById('svg_color');
1713     var L=document.getElementById('svg_stroke');
1714 isao-hara 2
1715 isao-hara 35 var x = getPreviewX(sx);
1716     var y = getPreviewY(sy);
1717 isao-hara 2
1718 isao-hara 35 switch(modeSVG){
1719     case 'text':
1720     var txt=document.getElementById('svg_text');
1721     var size=document.getElementById('svg_size');
1722     if(txt.value){
1723     y = y + parseInt(size.value)*0.8;
1724     targetItem=createSVGText(txt.value, x, y, size.value, color.value);
1725     }else{
1726     putInputForm(x, y, txt.value, size.value);
1727     }
1728     break;
1729     case 'rect':
1730     var attr = 'x='+x+',y='+y+',width='+svg_width+',height='+svg_height;
1731     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
1732     break;
1733     case 'circle':
1734     var attr = 'cx='+x+',cy='+y+',r='+svg_rx;
1735     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
1736     break;
1737     case 'ellipse':
1738     var attr = 'cx='+x+',cy='+y+',rx='+svg_rx+',ry='+svg_ry;
1739     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
1740     break;
1741     case 'newPath':
1742     var attr = 'd=M '+x+' '+y+' L '+x+' '+y;
1743     targetItem=createSVGObj('path' ,attr, 'none', color.value, L.value);
1744     break;
1745     case 'newLine':
1746     var x2=x+1;
1747     var attr = 'x1='+x+',y1='+y+',x2='+x2+',y2='+y;
1748     targetItem=createSVGObj('line' ,attr, 'none', color.value, L.value);
1749     break;
1750     case 'image':
1751     var attr = 'x='+x+',y='+y;
1752     var txt=document.getElementById('svg_text');
1753     var w=document.getElementById('svg_w');
1754     var h=document.getElementById('svg_h');
1755     if(txt.value) targetItem=createSVGImage(txt.value ,w.value, h.value, attr);
1756     default:
1757     break;
1758 isao-hara 2 }
1759 isao-hara 35 if (targetItem){
1760     appendSVGObj(targetItem);
1761     selectedItems[0]=targetItem;
1762     }
1763     }else{
1764     var x1=getPreviewX(sx-1);
1765     var y1=getPreviewY(sy-1);
1766     var x2=getPreviewX(sx+2);
1767     var y2=getPreviewY(sy+2);
1768     if(!checkIntersection(svg_select, x1, y1, x2, y2)){
1769     setSVGMode('selector');
1770     }
1771 isao-hara 2 }
1772 isao-hara 35 }
1773 isao-hara 2 }
1774    
1775 isao-hara 25
1776     function onDoubleTap(e){
1777     if(selectedItems.length == 1 ){
1778 isao-hara 29 svg_select.setAttribute("visibility","hidden");
1779    
1780 isao-hara 25 var obj = selectedItems[0];
1781     switch(obj.tagName){
1782     case 'svg:text':
1783 isao-hara 29 case 'text':
1784 isao-hara 25 var txt = trim(obj.textContent);
1785     var size = parseInt(obj.getAttribute("font-size"));
1786     var x = parseInt(obj.getAttribute("x"));
1787     var y = parseInt(obj.getAttribute("y"));
1788     x = x-20;
1789     y = y-size*0.8 -10;
1790     putInputForm(x, y, txt, size, obj.id);
1791     obj.style.display = 'none';
1792     editingTextObj = obj;
1793     break;
1794     default:
1795     break;
1796     }
1797 isao-hara 29 return false;
1798 isao-hara 25 }
1799     }
1800    
1801 isao-hara 2 function getPoints(d){
1802     var p = d.split(' ');
1803     var res = new Array();
1804     var isx=true;
1805     var x, y;
1806     for(var i=0; i<p.length;i++){
1807     if(p[i].match('[MLHVCSQTA]','i')){
1808     }else{
1809     if(isx){
1810     x = parseInt(p[i]);
1811     }else{
1812     y = parseInt(p[i]);
1813     res.push(new Array(x, y));
1814     }
1815     isx = !isx;
1816     }
1817     }
1818     return res;
1819     }
1820    
1821     function updatePath(d, x, y){
1822     var p = d.split(' ');
1823     var res = "";
1824     var isx=true;
1825     var val;
1826     for(var i=0; i<p.length;i++){
1827     if(p[i].match('[MLHVCSQTA]','i')){
1828     res += ' '+p[i];
1829     }else{
1830     if(isx){
1831     val = parseInt(p[i])+x;
1832     }else{
1833     val = parseInt(p[i])+y;
1834     }
1835    
1836     res += ' '+ val;
1837     isx = !isx;
1838     }
1839     }
1840     return trim(res);
1841     }
1842    
1843     function updateTransform(obj){
1844     try{
1845     var trans = obj.getAttribute("transform");
1846     if(!trans) return;
1847     if(trans.match(new RegExp("translate(.+,.+)","i"))){
1848     var str = RegExp.lastMatch;
1849     var vals = str.substr(10,str.length-11).split(',') ;
1850     var dx = parseInt(vals[0]);
1851     var dy = parseInt(vals[1]);
1852     switch(getElementTag(obj)){
1853     case 'text':
1854     case 'rect':
1855 isao-hara 6 case 'image':
1856 isao-hara 2 addAttributeVal(obj, "x", dx);
1857     addAttributeVal(obj, "y", dy);
1858     break;
1859     case 'circle':
1860     case 'ellipse':
1861     addAttributeVal(obj, "cx", dx);
1862     addAttributeVal(obj, "cy", dy);
1863     break;
1864    
1865     case 'path':
1866     var path = obj.getAttribute("d");
1867     obj.setAttribute("d", updatePath(path, dx, dy));
1868     break;
1869     case 'line':
1870     var x1 = getAttributeVal(obj,"x1");
1871     var y1 = getAttributeVal(obj,"y1");
1872     var x2 = getAttributeVal(obj,"x2");
1873     var y2 = getAttributeVal(obj,"y2");
1874     obj.setAttribute("x1", x1+dx);
1875     obj.setAttribute("y1", y1+dy);
1876     obj.setAttribute("x2", x2+dx);
1877     obj.setAttribute("y2", y2+dy);
1878    
1879     break;
1880     default:
1881     break;
1882     }
1883     obj.removeAttribute("transform");
1884     }
1885     }catch (e){
1886     }
1887     }
1888    
1889     function moveSelectedRectangle(dx, dy){
1890     if(!svg_select) return;
1891     svg_select.setAttribute("transform","translate("+dx+","+dy+")");
1892     }
1893    
1894     function updateSelectedRectangle(){
1895     if(!svg_select) return;
1896     if(selectedItems.length > 0){
1897 isao-hara 29 svg_select.setAttribute("visibility","visible");
1898 isao-hara 2 }else{
1899 isao-hara 29 svg_select.setAttribute("visibility","hidden");
1900 isao-hara 2 }
1901     }
1902    
1903     function clearSelectedItems(){
1904     for(i in selectedItems) delete selectedItems[i];
1905     }
1906    
1907 isao-hara 19 function onTouchMoveCode1(pageX, pageY){
1908 isao-hara 35 if(targetItem || selectedItems.length > 0){
1909     switch(modeSVG){
1910     case 'newPath':
1911     if(targetItem.tagName == 'path' ){
1912     var path = targetItem.getAttribute("d");
1913     path = path + ' '+ getPreviewX(pageX) + ' '+ getPreviewY(pageY) ;
1914     targetItem.setAttribute("d",path);
1915     }
1916     break;
1917     case 'newLine':
1918     if(targetItem.tagName == 'line' ){
1919     var x2 = getPreviewX(pageX);
1920     var y2 = getPreviewY(pageY);
1921     targetItem.setAttribute("x2",x2);
1922     targetItem.setAttribute("y2",y2);
1923     }
1924     break;
1925     default:
1926     var dx = pageX - sx;
1927     var dy = pageY - sy;
1928 isao-hara 2
1929 isao-hara 35 if(selectedItems.length == 1 && getElementTag(selectedItems[0]) == 'line'){
1930     var lx = getPreviewX(pageX);
1931     var ly = getPreviewY(pageY);
1932 isao-hara 2
1933 isao-hara 35 if(!lineEdit){
1934     var x1 = getAttributeVal(selectedItems[0],"x1");
1935     var y1 = getAttributeVal(selectedItems[0],"y1");
1936     var x2 = getAttributeVal(selectedItems[0],"x2");
1937     var y2 = getAttributeVal(selectedItems[0],"y2");
1938     var xc = (x1+x2)/2;
1939     var yc = (y1+y2)/2;
1940     var eS = Math.min(Math.abs(x1-lx), Math.abs(y1-ly));
1941     var eC = Math.min(Math.abs(xc-lx), Math.abs(yc-ly));
1942     var eE = Math.min(Math.abs(x2-lx), Math.abs(y2-ly));
1943 isao-hara 2
1944 isao-hara 35 var minVal = Math.min(eS, Math.min(eC, eE));
1945     if(minVal == eS) lineEdit='start';
1946     else if(minVal == eE) lineEdit='end';
1947     else lineEdit='move';
1948     }
1949     if(lineEdit=='start'){
1950     selectedItems[0].setAttribute("x1",lx );
1951     selectedItems[0].setAttribute("y1",ly );
1952     setSelectBox();
1953     }else if(lineEdit == 'end'){
1954     selectedItems[0].setAttribute("x2",lx );
1955     selectedItems[0].setAttribute("y2",ly );
1956     setSelectBox();
1957 isao-hara 4 }else{
1958 isao-hara 35 selectedItems[0].setAttribute("transform","translate("+dx+","+dy+")");
1959 isao-hara 4 moveSelectedRectangle(dx, dy);
1960     }
1961 isao-hara 35 }else{
1962     for(var i=0; i<selectedItems.length;i++){
1963     if(selectedItems[i]) selectedItems[i].setAttribute("transform","translate("+dx+","+dy+")");
1964     }
1965     moveSelectedRectangle(dx, dy);
1966     }
1967     break;
1968     }
1969     updateShowMenu();
1970     }else if(modeSVG == 'selector'){
1971     ex = pageX;
1972     ey = pageY;
1973     var x1=sx;
1974     var y1=sy;
1975     var x2=ex;
1976     var y2=ey;
1977 isao-hara 2
1978 isao-hara 35 if(sx > ex){ x1=ex; x2=sx; }
1979     if(sy > ey){ y1=ey; y2=sy; }
1980 isao-hara 2
1981 isao-hara 35 selectedRectangle(getPreviewX(x1), getPreviewY(y1), getPreviewX(x2), getPreviewY(y2));
1982     }
1983 isao-hara 2 }
1984    
1985 isao-hara 23 function svgInputTextExec(e){
1986     var inputform = document.getElementById('svg_input');
1987     var color=document.getElementById('svg_color');
1988     var size=document.getElementById('svg_size');
1989 isao-hara 29 if(!color || !size) return;
1990 isao-hara 24 var y = parseInt(inputform.style.top) + parseInt(size.value)*0.8 + 12;
1991     var x = parseInt(inputform.style.left) + 12;
1992 isao-hara 23 if(inputform.value){
1993 isao-hara 24 targetItem=createSVGText(inputform.value, x, y, size.value, color.value);
1994    
1995 isao-hara 23 appendSVGObj(targetItem);
1996     }
1997     inputform.setAttribute("type", "hidden");
1998     setSVGMode('selector');
1999     }
2000    
2001 isao-hara 25 function svgModifyTextExec(e){
2002     var inputform = document.getElementById('svg_input');
2003     editingTextObj.textContent = inputform.value;
2004     inputform.setAttribute("type", "hidden");
2005     editingTextObj.style.display='block';
2006 isao-hara 27
2007     editingTextObj=null;
2008 isao-hara 25 setSVGMode('selector');
2009     }
2010    
2011 isao-hara 23 function svgInputFormAdjust(e){
2012     var inputform = document.getElementById('svg_input');
2013 isao-hara 24 inputform.size = jstrlen(inputform.value) + 1;
2014 isao-hara 23 }
2015    
2016 isao-hara 24 function jstrlen(str) {
2017     var len = 0;
2018     str = escape(str);
2019     for (var i = 0; i < str.length; i++, len++) {
2020     if (str.charAt(i) == "%") {
2021     if (str.charAt(++i) == "u") {
2022     i += 3;
2023     len++;
2024     }
2025     i++;
2026     }
2027     }
2028     return len;
2029     }
2030    
2031 isao-hara 25 function putInputForm(x, y, txt, size, id){
2032 isao-hara 23 var inputform = document.getElementById('svg_input');
2033     if(!inputform){
2034     inputform = document.createElement('input');
2035     inputform.setAttribute("id", "svg_input");
2036     inputform.setAttribute("style", "position:absolute;top:0px;left:0px; border:0px none");
2037 isao-hara 29 preview.appendChild(inputform);
2038     }
2039    
2040     if(document.addEventListner){
2041     if(id){
2042     inputform.addEventListener("onChange", svgModifyTextExec,false);
2043 isao-hara 23 }else{
2044 isao-hara 29 inputform.addEventListener("onChange", svgInputTextExec,false);
2045 isao-hara 23 }
2046 isao-hara 29 inputform.addEventListener("onkeydown", svgInputFormAdjust,false);
2047     }else{
2048     if(id){
2049     inputform.setAttribute("onChange", "svgModifyTextExec()");
2050     }else{
2051     inputform.setAttribute("onChange", "svgInputTextExec()");
2052     }
2053     inputform.setAttribute("onkeydown", "svgInputFormAdjust()");
2054 isao-hara 23 }
2055    
2056     inputform.setAttribute("type", "text");
2057     inputform.style.left=x+'px';
2058     inputform.style.top=y+'px';
2059     inputform.style.fontSize=size+'px';
2060     inputform.style.background='none';
2061     inputform.value = txt;
2062 isao-hara 25 inputform.size = jstrlen(txt) + 1;
2063 isao-hara 23 inputform.focus();
2064     }
2065    
2066     ///// EventHandler for iPad
2067 isao-hara 25 var firstTouch = new Date();
2068    
2069 isao-hara 23 function onTouchStart(e){
2070     //e.preventDefault();
2071     sx=e.touches[0].pageX;
2072     sy=e.touches[0].pageY;
2073     ex=e.touches[0].pageX;
2074     ey=e.touches[0].pageY;
2075    
2076     if (e.touches.length == 1){
2077 isao-hara 25 var touchtime = new Date();
2078     var dt = touchtime.getTime() - firstTouch.getTime();
2079 isao-hara 27 if(editingTextObj){
2080     var inputform = document.getElementById('svg_input');
2081     inputform.setAttribute("type", "hidden");
2082     editingTextObj.style.display='block';
2083     editingTextObj=null;
2084     }
2085 isao-hara 35 if(dt < 300 ){ onDoubleTap(); }else{ onTouchStartCore(); }
2086 isao-hara 25 firstTouch = touchtime;
2087 isao-hara 35 updateSelectedRectangle();
2088 isao-hara 25 }
2089 isao-hara 23 }
2090    
2091     function onTouchMove(e){
2092     if (e.touches.length == 1){
2093 isao-hara 25 e.preventDefault();
2094 isao-hara 23 onTouchMoveCode1(e.touches[0].pageX, e.touches[0].pageY);
2095     }else if (e.touches.length == 2){
2096 isao-hara 35 if(selectedItems.length == 1){
2097     e.preventDefault();
2098     var dx = e.touches[0].pageX-e.touches[1].pageX;
2099     var dy = e.touches[0].pageY-e.touches[1].pageY;
2100     var th = Math.abs(Math.atan2(dy , dx)/Math.PI *180);
2101 isao-hara 23
2102 isao-hara 35 if(th > 165 || th < 25) svg_scale_dir = 'x';
2103     else if(th > 65 && th < 115) svg_scale_dir = 'y';
2104     else svg_scale_dir = null;
2105     }
2106 isao-hara 23 }
2107     }
2108    
2109 isao-hara 2 function onTouchEnd(e){
2110     e.preventDefault();
2111    
2112     for(var i=0; i<selectedItems.length;i++){
2113     updateTransform(selectedItems[i]);
2114     }
2115    
2116     if(modeSVG == 'selector'){
2117     var x1 = getPreviewX(sx);
2118     var y1 = getPreviewY(sy);
2119     var x2 = getPreviewX(ex);
2120     var y2 = getPreviewY(ey);
2121     getSelectedObjects(x1, y1, x2, y2);
2122     }
2123    
2124     updateSelectedRectangle();
2125     updateTransform(svg_select);
2126    
2127     setSelectBox();
2128     updateShowMenu();
2129 isao-hara 4 var btn = document.getElementById('b_'+modeSVG);
2130     if(btn){
2131     btn.style.border="3px solid red";
2132     }
2133 isao-hara 2 lineEdit=null;
2134     }
2135    
2136    
2137     function onGestureStart(e){
2138     targetItem = selectedItems[0];
2139     if(targetItem){
2140     svg_wo = targetItem.getAttribute("width");
2141     svg_ho = targetItem.getAttribute("height");
2142     svg_ro = targetItem.getAttribute("r");
2143     svg_rxo = targetItem.getAttribute("rx");
2144     svg_ryo = targetItem.getAttribute("ry");
2145     svg_fsize = targetItem.getAttribute("font-size");
2146     }else{
2147     svg_wo = null;
2148     svg_ho = null;
2149     svg_ro = null;
2150     svg_rxo = null;
2151     svg_ryo = null;
2152     svg_fsize = null;
2153     }
2154     }
2155    
2156     function onGestureChange(e){
2157 isao-hara 4 var scale = e.scale;
2158     var rotation = e.rotation;
2159 isao-hara 2
2160 isao-hara 4 if(targetItem){
2161 isao-hara 7 e.preventDefault();
2162 isao-hara 4 if (svg_wo && svg_scale_dir != 'y') targetItem.setAttribute("width", Math.round(svg_wo*scale ));
2163     if (svg_ho && svg_scale_dir != 'x') targetItem.setAttribute("height", Math.round(svg_ho*scale ));
2164     if (svg_ro) targetItem.setAttribute("r", Math.round(svg_ro*scale ));
2165     if (svg_rxo && svg_scale_dir != 'y') targetItem.setAttribute("rx", Math.round(svg_rxo*scale) );
2166     if (svg_ryo && svg_scale_dir != 'x') targetItem.setAttribute("ry", Math.round(svg_ryo*scale) );
2167     if (svg_fsize) targetItem.setAttribute("font-size", Math.round(svg_fsize*scale) );
2168 isao-hara 2
2169 isao-hara 4 updateShowMenu();
2170     }
2171 isao-hara 2 }
2172    
2173     function onGestureEnd(e){
2174 isao-hara 7 // e.preventDefault();
2175 isao-hara 4 svg_wo = null;
2176     svg_ho = null;
2177     svg_ro = null;
2178     svg_rxo = null;
2179     svg_ryo = null;
2180     svg_fsize = null;
2181 isao-hara 2 }
2182    
2183 isao-hara 14 function onTouchStartColor(e){
2184     if(e.touches.length == 1){
2185     var ele = e.touches[0].target;
2186 isao-hara 25 var color;
2187     try{
2188 isao-hara 32 color = ele.getAttribute("color-val");
2189 isao-hara 25 }catch(err){ hideItemById('color-palette'); return;}
2190     var palette = document.getElementById('color-palette');
2191     var color_sel = palette.getAttribute("targetType");
2192 isao-hara 14 var fill_input = document.getElementById('svg_fill');
2193     var color_input = document.getElementById('svg_color');
2194     if(color) {
2195 isao-hara 25 for(var i=0;i < selectedItems.length; i++){
2196     selectedItems[i].setAttribute(color_sel, color);
2197     if(color_sel == 'fill'){
2198     if(fill_input) fill_input.value=color;
2199     document.getElementById('toolFill').style.backgroundColor=color;
2200 isao-hara 14 }else{
2201     color_input.value=color;
2202 isao-hara 25 document.getElementById('toolStroke').style.backgroundColor=color;
2203     }
2204 isao-hara 14 }
2205     }
2206 isao-hara 25 palette.style.display='none';
2207 isao-hara 14 }
2208     }
2209 isao-hara 2 ///// For Safari
2210 isao-hara 19 var mouse_state=0;
2211 isao-hara 2
2212 isao-hara 19 function onMouseDown(e){
2213     mouse_state=e.which;
2214 isao-hara 18
2215     sx=e.pageX;
2216     sy=e.pageY;
2217 isao-hara 19 x=e.pageX;
2218 isao-hara 18 ey=e.pageY;
2219    
2220 isao-hara 19 if(isChildById(e.target, 'preview') || e.target.id == 'preview') {
2221 isao-hara 29 if(editingTextObj){
2222     var inputform = document.getElementById('svg_input');
2223     inputform.setAttribute("type", "hidden");
2224     editingTextObj.style.display='block';
2225     editingTextObj=null;
2226     }
2227    
2228     if(e.altKey){
2229     targetItem = selectedItems[0];
2230     if(targetItem){
2231     svg_wo = targetItem.getAttribute("width");
2232     svg_ho = targetItem.getAttribute("height");
2233     svg_ro = targetItem.getAttribute("r");
2234     svg_rxo = targetItem.getAttribute("rx");
2235     svg_ryo = targetItem.getAttribute("ry");
2236     svg_fsize = targetItem.getAttribute("font-size");
2237     }else{
2238     svg_wo = null;
2239     svg_ho = null;
2240     svg_ro = null;
2241     svg_rxo = null;
2242     svg_ryo = null;
2243     svg_fsize = null;
2244     }
2245     }else{
2246     onTouchStartCore();
2247     }
2248 isao-hara 19 updateSelectedRectangle();
2249     return false;
2250     }else if(isChildById(e.target, 'color-palette') && e.target.tagName == 'DIV'){
2251     var ele = e.target;
2252 isao-hara 30 var palette = document.getElementById('color-palette');
2253 isao-hara 19 var color = ele.getAttribute("color-val");
2254 isao-hara 30 var color_sel = palette.getAttribute("targetType");
2255 isao-hara 19 var fill_input = document.getElementById('svg_fill');
2256     var color_input = document.getElementById('svg_color');
2257     if(color) {
2258     for(var i=0;i < selectedItems.length; i++){
2259 isao-hara 30 selectedItems[i].setAttribute(color_sel, color);
2260     if(color_sel == 'fill'){
2261     fill_input.value=color;
2262     document.getElementById('toolFill').style.backgroundColor=color;
2263     }else{
2264     color_input.value=color;
2265     document.getElementById('toolStroke').style.backgroundColor=color;
2266     }
2267 isao-hara 19 }
2268 isao-hara 30 palette.style.display='none';
2269 isao-hara 19 }
2270     return false;
2271     }
2272     return true;
2273     }
2274 isao-hara 18
2275 isao-hara 19 function onMouseMove(e){
2276     if(!isChildById(e.target, 'preview') && e.target.id != 'preview') { return true; }
2277 isao-hara 18
2278 isao-hara 29 if(mouse_state == 1) {
2279     if(e.altKey){
2280     var dx = sx-e.pageX;
2281     var dy = sy-e.pageY;
2282     var th = Math.abs(Math.atan2(dy , dx)/Math.PI *180);
2283     var scale;
2284 isao-hara 18
2285 isao-hara 29 if(th > 165 || th < 25){
2286     svg_scale_dir = 'x';
2287     scale = -dx/50;
2288     }else if(th > 65 && th < 115){
2289     svg_scale_dir = 'y';
2290     scale = -dy/50;
2291     }else{
2292     svg_scale_dir = null;
2293     if(dx > 0 && dy > 0) scale = -dx*dy/2500;
2294     else if(dx < 0 && dy < 0) scale = dx*dy/2500;
2295     else scale = 1;
2296     }
2297    
2298     if(scale >= 0){
2299     if(scale < 1) scale=1;
2300     }else{
2301     if(scale > -1) scale=1;
2302     else{
2303     scale= -1/scale;
2304     }
2305     }
2306     if(targetItem){
2307     if (svg_wo && svg_scale_dir != 'y') targetItem.setAttribute("width", Math.round(svg_wo*scale ));
2308     if (svg_ho && svg_scale_dir != 'x') targetItem.setAttribute("height", Math.round(svg_ho*scale ));
2309     if (svg_ro) targetItem.setAttribute("r", Math.round(svg_ro*scale ));
2310     if (svg_rxo && svg_scale_dir != 'y') targetItem.setAttribute("rx", Math.round(svg_rxo*scale) );
2311     if (svg_ryo && svg_scale_dir != 'x') targetItem.setAttribute("ry", Math.round(svg_ryo*scale) );
2312     if (svg_fsize) targetItem.setAttribute("font-size", Math.round(svg_fsize*scale) );
2313    
2314     updateShowMenu();
2315     }
2316     }else {
2317     onTouchMoveCode1(e.pageX, e.pageY);
2318     }
2319     }
2320    
2321 isao-hara 19 return false;
2322     }
2323 isao-hara 18
2324 isao-hara 19 function onMouseUp(e){
2325     mouse_state = 0;
2326     if(!isChildById(e.target, 'preview') && e.target.id != 'preview') { return true; }
2327 isao-hara 18
2328 isao-hara 19 for(var i=0; i<selectedItems.length;i++){
2329     updateTransform(selectedItems[i]);
2330     }
2331 isao-hara 18
2332 isao-hara 19 if(modeSVG == 'selector'){
2333     var x1 = getPreviewX(sx);
2334     var y1 = getPreviewY(sy);
2335     var x2 = getPreviewX(ex);
2336     var y2 = getPreviewY(ey);
2337     getSelectedObjects(x1, y1, x2, y2);
2338     }
2339 isao-hara 18
2340 isao-hara 19 updateSelectedRectangle();
2341     updateTransform(svg_select);
2342 isao-hara 18
2343 isao-hara 19 setSelectBox();