Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations) (download) (as text)
Fri Jul 23 01:39:08 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 67632 byte(s)
bug fix
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     }
93    
94     function restoreValues(){
95     if(typeof(localStorage) == 'undefined'){
96     alert('local storage not suported');
97     }
98     MgrPath = localStorage.getItem('MgrPath') ? localStorage.getItem('MgrPath') : "";
99     window.onbeforeuload=function(){ return saveChanges(); }
100     }
101 isao-hara 25
102 isao-hara 2 function clearAll(){
103     localsStorage.clear();
104     restoreValudes();
105     }
106    
107     function saveChanges(){
108     localStorage.getItem('MgrPath') = MgrPath;
109     }
110    
111     // initialize a database
112     function initDB(name, dispname, size) {
113     try {
114     if (!window.openDatabase) {
115     alert('not supported');
116     } else {
117     var version = '1.0';
118     var myDB = openDatabase(name, version, dispname, size);
119     }
120     } catch(e) {
121     if (e == INVALID_STATE_ERR) {
122     alert("Invalid database version.");
123     } else {
124     alert("Unknown error "+e+".");
125     }
126     return null;
127     }
128    
129     createTables(myDB);
130     return myDB;
131     }
132    
133     function createTables(db) {
134     if (0) dropTables(db);
135    
136     db.transaction(
137     function (transaction) {
138     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);
139     transaction.executeSql('CREATE TABLE IF NOT EXISTS filedata(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, datablob BLOB NOT NULL DEFAULT "");', [], nullDataHandler, errorHandler);
140     }
141     );
142     }
143    
144     function dropTables(db) {
145     db.transaction(
146     function (transaction) {
147     transaction.executeSql('DROP TABLE files;');
148     transaction.executeSql('DROP TABLE filedata;');
149     }
150     );
151     }
152    
153     /// Create New File
154     function reallyCreateNewFileAction(name, db){
155     var myDB = db;
156     if(!db) myDB = systemDB;
157    
158     myDB.transaction(
159     function (transaction) {
160     var myfunc = new Function("transaction", "results", "transaction.executeSql('INSERT INTO files (name, filedata_id) VALUES (?, ?);', [ '"+name+"', results.insertId], nullDataHandler, killTransaction);");
161    
162     transaction.executeSql('INSERT INTO filedata (datablob) VALUES ("");', [], myfunc, errorHandler);
163     }
164     );
165    
166     fileSelector();
167     }
168    
169     function createNewFileAction(db){
170     var myDB = db;
171     if(!db) myDB = systemDB;
172    
173     var name = document.getElementById('createFilename').value
174     if(name == "") {
175     if (confirm('Filename is required, try agein?')) { createNewFile(); }else{ fileSelector(); }
176     return;
177     }
178    
179     myDB.transaction(
180     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+name+"'],"+
181     "function (transaction, results) {"+
182     "if(results.rows.length == 0){"+
183     "reallyCreateNewFileAction('"+name+"');"+
184     "}else{"+
185     "if (confirm(results.rows.item(0)['name']+' already exist, try agein?')) {"+
186     "createNewFile();"+
187     "}else{"+
188     "fileSelector();"+
189     "}"+
190     "}"+
191     "}, errorHandler);")
192     );
193    
194     }
195    
196     //// Delete File
197     function deleteUpdateResults(transaction, results)
198     {
199     if (results.rowsAffected) {
200     fileSelector();
201     }
202     }
203    
204     function reallyDelete(id, db){
205     var myDB = db;
206     if(!db) myDB = systemDB;
207    
208     myDB.transaction(
209     new Function("transaction",
210     "transaction.executeSql('DELETE FROM files where id=?;', [ "+id+" ], null, errorHandler);"
211     +"transaction.executeSql('DELETE FROM filedata where id=?;', [ "+id+" ], deleteUpdateResults, errorHandler);")
212     );
213     }
214    
215     function deleteFile(name, db){
216     var myDB = db;
217     if(!db) myDB = systemDB;
218    
219     myDB.transaction(
220     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+name+"'],"+
221     "function (transaction, results) {"+
222     "if (confirm('Really delete '+results.rows.item(0)['name']+'?')) {"+
223     "reallyDelete(results.rows.item(0)['id']);"+
224     "}"+
225     "}, errorHandler);")
226     );
227     }
228    
229     function reallyDeleteAll(db){
230     var myDB = db;
231     if(!db) myDB = systemDB;
232    
233     myDB.transaction(
234     new Function("transaction",
235     "transaction.executeSql('DELETE FROM files;',[],null, errorHandler);"
236     +"transaction.executeSql('DELETE FROM filedata;',[],deleteUpdateResults, errorHandler);")
237     );
238     }
239    
240     function deleteAllFile(db){
241     if (confirm('Really delete all file?')) {
242     reallyDeleteAll(db);
243     }
244     }
245    
246     ///// Rename
247     function reallyRenameFileAction(id, newname, db){
248     var myDB = db;
249     if(!db) myDB = systemDB;
250    
251     myDB.transaction(
252     function (transaction) {
253     transaction.executeSql('UPDATE files set name=? where id=?;', [newname,id ], null, errorHandler);
254     }
255     );
256     fileSelector();
257     }
258    
259     function renameFileAction(db){
260     var myDB = db;
261     if(!db) myDB = systemDB;
262    
263     var new_name = document.getElementById('newFilename').value
264     var id = document.getElementById('fileId').value
265    
266     if(new_name == "") {
267     alert('Filename is required.');
268     fileSelector();
269     return;
270     }
271    
272     myDB.transaction(
273     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+new_name+"'],"+
274     "function (transaction, results) {"+
275     "if(results.rows.length == 0){"+
276     "reallyRenameFileAction('"+id+"','"+new_name+"');"+
277     "}else{"+
278     "alert(results.rows.item(0)['name']+' already exist');"+
279     "fileSelector();"+
280     "}"+
281     "}, errorHandler);")
282     );
283    
284     }
285    
286    
287     /// Save File
288     function saveFile(db){
289     var myDB = db;
290     if(!db) myDB = systemDB;
291    
292     var editarea = document.getElementById('editarea');
293     var contents = "";
294     contents = editarea.value;
295    
296     myDB.transaction(
297     function (transaction) {
298     var datadiv = document.getElementById('tempdata');
299     var filedata_id = datadiv.getAttribute('lfdataid');
300    
301     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
302     [ contents, filedata_id ], nullDataHandler, errorHandler);
303    
304     alert('Saved.');
305     }
306     );
307     }
308    
309     function saveData(db){
310     var myDB = db;
311     if(!db) myDB = systemDB;
312    
313     var contents = "";
314     contents = getSVGContent();
315    
316     myDB.transaction(
317     function (transaction) {
318     var datadiv = document.getElementById('tempdata');
319     var filedata_id = datadiv.getAttribute('lfdataid');
320    
321     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
322     [ contents, filedata_id ], nullDataHandler, errorHandler);
323    
324     alert('Saved.');
325     }
326     );
327     }
328    
329 isao-hara 14 function saveContent(contents, db){
330     var myDB = db;
331     if(!db) myDB = systemDB;
332    
333     if(!contents) {
334     alert("Invalid content");
335     return;
336     }
337    
338     myDB.transaction(
339     function (transaction) {
340     var datadiv = document.getElementById('tempdata');
341     var filedata_id = datadiv.getAttribute('lfdataid');
342    
343     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
344     [ contents, filedata_id ], nullDataHandler, errorHandler);
345    
346     alert('Saved.');
347     }
348     );
349     }
350    
351 isao-hara 17 function reallySaveNewFileAction(fname, db){
352     var myDB = db;
353     if(!db) myDB = systemDB;
354    
355     var datadiv = document.getElementById('tempdata');
356     content = datadiv.textContent;
357    
358     myDB.transaction(
359     function (transaction) {
360     var myfunc = new Function("transaction", "results", "transaction.executeSql('INSERT INTO files (name, filedata_id) VALUES (?, ?);', [ '"+fname+"', results.insertId], nullDataHandler, killTransaction);");
361    
362     transaction.executeSql('INSERT INTO filedata (datablob) VALUES (?);', [content], myfunc, errorHandler);
363     }
364     );
365    
366     datadiv.textContent="";
367     alert("download to "+fname);
368     }
369    
370     function saveContentFilename(id, db){
371    
372     var datadiv = document.getElementById('tempdata');
373     datadiv.setAttribute('lfdataid', id);
374     saveContent(datadiv.innerHTML, db);
375     datadiv.textContent="";
376     }
377    
378     function saveContentWithFilename(fname, contents, db){
379     var myDB = db;
380     if(!db) myDB = systemDB;
381    
382     if(!contents) {
383     alert("Invalid content");
384     return;
385     }
386    
387     var datadiv = document.getElementById('tempdata');
388     datadiv.setAttribute('lfname', fname);
389     datadiv.textContent = contents;
390    
391     myDB.transaction(
392     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+fname+"'],"+
393     "function (transaction, results) {"+
394     "if(results.rows.length == 0){"+
395     "reallySaveNewFileAction('"+fname+"');"+
396     "}else{"+
397     "if (confirm(results.rows.item(0)['name']+' already exist, Overwrite?')) {"+
398     "saveContentFilename(results.rows.item(0)['id']);"+
399     "}else{"+
400     "fileSelector();"+
401     "}"+
402     "}"+
403     "}, errorHandler);")
404     );
405    
406     }
407    
408 isao-hara 2 function showFileData(transaction, results){
409     var data = results.rows.item(0);
410     var filename = data['name'];
411     var filedata = data['datablob'];
412    
413     var datadiv = document.getElementById('tempdata');
414     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
415    
416     document.title=filename;
417    
418     previewFile(filedata);
419    
420     updateShowMenu();
421     setMode('Preview');
422     }
423    
424     function showFile(name){
425     getFile(name, 'showFileData');
426     }
427    
428     function getFile(name, func, db){
429     var myDB = db;
430     if(!db) myDB = systemDB;
431    
432     var datadiv = document.getElementById('tempdata');
433     if(datadiv) datadiv.setAttribute('lfname', name);
434    
435     myDB.transaction(
436     function (transaction) {
437     transaction.executeSql('SELECT * from files, filedata where files.name=? and files.filedata_id = filedata.id;', [name], eval(func), errorHandler);
438     }
439     );
440     }
441    
442     // Error Handlers
443     function killTransaction(transaction, error){
444     return true;
445     }
446    
447     function errorHandler(transaction, error){
448     alert('Oops. Error was '+error.message+' (Code '+error.code+')');
449    
450     var we_think_this_error_is_fatal = true;
451     if (we_think_this_error_is_fatal) return true;
452     return false;
453     }
454    
455     function nullDataHandler(transaction, results){
456     }
457    
458     ///////////////////////////
459    
460     function fileSelector(db) {
461     var myDB = db;
462     if(!db) myDB = systemDB;
463    
464     myDB.transaction(
465     function (transaction) {
466     transaction.executeSql("SELECT * from files where deleted=0;", [ ],
467     function (transaction, results) {
468     var filelist = '';
469     var menuDiv = document.getElementById('menuDiv');
470     if(menuDiv){
471     for(var i=0; i<results.rows.length; i++) {
472     var row = results.rows.item(i);
473     filelist = filelist + fileEntry(row);
474     }
475     if (filelist == "") {
476     filelist = "No files.<br />\n";
477     } else {
478     filelist = "<center><table class='filetable'>"+filelist+"</table></center>";
479     }
480     menuDiv.innerHTML="<H1 class='title'>"+MainTitle+"</H1>"+createMenuBar()+filelist;
481     }
482     }, errorHandler);
483     }
484     );
485     setMode('List');
486     }
487    
488     function fileSelectorOnServer(val) {
489     var filelist = '';
490     var menuDiv = document.getElementById('menuDiv');
491     var results = val.split(',');
492    
493     if(menuDiv){
494     for(var i=0; i<results.length; i++) {
495     var row = results[i];
496     filelist = filelist + fileEntryOnServer(row);
497     }
498     if (filelist == "") {
499     filelist = "No files.<br />\n";
500     } else {
501     filelist = "<center><table class='filetable'>"+filelist+"</table></center>";
502     }
503     var Menu = "<button onClick=\"fileSelector();\">Local Storage</button>";
504 isao-hara 16 Menu += "<button onClick=\"getRemoteFileList();\">List on Server</button>";
505 isao-hara 2 menuDiv.innerHTML="<H1 class='title'>"+MainTitle+"</H1>"+Menu+filelist;
506     }
507     setMode('List');
508     }
509    
510     function fileEntryOnServer(name){
511     name = name.split('.')[0];
512     var res = "<tr class='filerow'>";
513     res += "<td class='filenamecell' onClick=\"getRemoteFile('"+name+"');\">"+name+"</td>";
514     res += "<td class='filelinkcell'>";
515     res += "<button class='green' onClick=\"showRemoteFile('"+name+"');\">View</button>";
516 isao-hara 16 res += "<button onClick=\"downloadFile('"+name+"');\">Download to local storage</button>";
517     res += "<button onClick=\"downloadToFile('"+name+"');\">Download as SVG File</button>";
518 isao-hara 2 res += "</td></tr>\n";
519    
520     return res;
521     }
522    
523     function fileEntry(row){
524     var name = row['name'];
525     var files_id = row['id'];
526    
527     var res = "<tr class='filerow'>";
528     res += "<td class='filenamecell' onClick=\"showFile('"+name+"');\">"+name+"</td>";
529     res += "<td class='filelinkcell'>";
530     res += "<button class='blue' onClick=\"editFile('"+name+"');\">&nbsp;Edit&nbsp;</button> &nbsp;";
531     res += "<button class='green' onClick=\"renameFile('"+name+"',"+files_id+");\">&nbsp;Rename&nbsp;</button> &nbsp;";
532     res += "<button onClick=\"uploadFile('"+name+"');\">Upload</button>";
533     res += "<button class='red' onClick=\"deleteFile('"+name+"');\">Delete</button>";
534     res += "</td></tr>\n";
535    
536     return res;
537     }
538    
539     function createMenuBar(){
540     var menu = "<ul id='menubar'>";
541     menu += "<li><button onClick='createNewFile()'>Create New File</button></li>";
542     menu += "<li><button onClick='deleteAllFile()'>Delete All</button></li>";
543     menu += "<li><button onClick='getRemoteFileList()'>File List on a Server</button></li>";
544 isao-hara 25 menu += "<li><button onClick='configServer()'>Server Configuration</button></li>";
545 isao-hara 2 menu += "</ul><p class='cls'>";
546     return menu;
547     }
548    
549 isao-hara 25 //// Config
550     function setConfig(){
551     var ele = document.getElementById('ServerURL');
552     MgrPath=ele.value;
553     alert("Done");
554     }
555    
556     function configServer(){
557     var popupDiv = document.getElementById('popup');
558     var string = "";
559    
560     string += "<H1 class='title'>Server Configration</H1>\n";
561     string += "<div class=\"input_form\">\n";
562     string += "Filename:<input id='ServerURL' name='url' value=\""+MgrPath+"\" size=\"80\"/><br>\n";
563     string += "<button onClick=\"setConfig();hideItemById('popup'); \">Done</button>\n";
564     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
565     string += "</div>\n";
566    
567     popupDiv.innerHTML=string;
568     popupDiv.style.display='block';
569     }
570    
571 isao-hara 2 ////
572     function createNewFile(){
573     var popupDiv = document.getElementById('popup');
574     var string = "";
575    
576     string += "<H1 class='title'>Create New File</H1>\n";
577     string += "<div class=\"input_form\">\n";
578     string += "Filename:<input id='createFilename' name='name' value=\"\" />\n";
579     string += "<button onClick=\"createNewFileAction();hideItemById('popup'); \">Create</button>\n";
580     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
581     string += "</div>\n";
582    
583     popupDiv.innerHTML=string;
584     popupDiv.style.display='block';
585     }
586    
587    
588     /////
589     function renameFile(name, id){
590     var popupDiv = document.getElementById('popup');
591     var string = "";
592    
593     string += "<H1 class='title'>Rename File</H1>\n";
594     string += "<div class='input_form'>\n";
595     string += "Old Filename: "+name+"<br>";
596     string += "New Filename:<input id='newFilename' name='newname' value=\"\" />\n";
597     string += "<input type='hidden' id='fileId' value=\""+id+"\" /><br>\n";
598     string += "<button onClick=\"renameFileAction();hideItemById('popup');\">Rename</button>\n";
599     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
600     string += "</div>\n";
601    
602     popupDiv.innerHTML=string;
603     popupDiv.style.display='block';
604     }
605    
606     /// EditMenu
607     function insertStr(str, len){
608     var editarea = document.getElementById('editarea');
609     if(len < 0) len = str.length;
610    
611     if (editarea){
612     if(str=='DQ') str='"';
613     var strs = editarea.value;
614     var cPos = editarea.selectionStart;
615     var tmp = strs.substr(0,cPos);
616     editarea.value = tmp +str+strs.substr(cPos, strs.length);
617     cPos += len;
618     editarea.setSelectionRange(cPos, cPos);
619     editarea.focus();
620     }
621     }
622    
623     function insertTag(tag){
624     var str;
625     var len = -1;
626     if (tag == "p"){
627     str = "<p> </p>";
628     len = 4;
629     }else if (tag == "ul"){
630     str = "<ul class=\" \"> \n\n</ul>";
631     len = 17;
632     }else if (tag == "li"){
633     str = "<li> </li>";
634     len = 5;
635     }else if (tag == "href"){
636     str = "xlink:href=\"\"";
637     len = 12;
638     }else if (tag == "EQ"){
639     str = "=\"\"";
640     len = 2;
641     }else if (tag == "svg:text"){
642     str = "<svg:text x=\"100\" y=\"100\" fill=\"black\" font-size=\"12\"> </svg:text>";
643     len = 58;
644     }else if (tag == "svg:rect"){
645     str = "<svg:rect x=\"10\" y=\"10\" width=\"100\" height=\"100\" fill=\"white\" stroke=\"black\" />";
646     len = 13;
647     }else if (tag == "svg:circle"){
648     str = "<svg:circle cx=\"10\" cy=\"100\" r=\"100\" fill=\"white\" stroke=\"black\" strokc-width=\"1\"/>";
649     len = 17;
650     }
651     insertStr(str, len);
652     }
653    
654     function editMenuBar()
655     {
656     var str = "";
657     str += "<button onClick=\"insertStr('/',1);\">/</button>\n";
658     str += "<button onClick=\"insertStr('DQ',1);\">\"</button>\n";
659     str += "<button onClick=\"insertTag('EQ',1);\">=\"\"</button>\n";
660     str += "<button onClick=\"insertTag('p');\">p</button>\n";
661     str += "<button onClick=\"insertTag('href');\">href</button>\n";
662    
663     str += "<button onClick=\"insertTag('svg:text');\">TEXT</button>\n";
664     str += "<button onClick=\"insertTag('svg:rect');\">rect</button>\n";
665     str += "<button onClick=\"insertTag('svg:circle');\">circle</button>\n";
666    
667     str += "<button onClick=\"chEditareaHeight();\">...</button>\n";
668    
669     return str;
670     }
671    
672     function updateEditMenu(){
673     var menuDiv = document.getElementById('menuDiv');
674     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
675     menuDiv.innerHTML+= "<button onClick=\"saveFile();fileSelector();\"> Save </button>\n";
676     menuDiv.innerHTML+= "<button onClick=\"previewData();\"> Preview </button>\n";
677     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
678    
679     menuDiv.style.display='block';
680     }
681    
682     //// Editor
683     function editFileData(transaction, results){
684     var editDiv = document.getElementById('editDiv');
685     var datadiv = document.getElementById('tempdata');
686    
687 isao-hara 18 if( results.rows.length == 0) return;
688 isao-hara 2 var data = results.rows.item(0);
689     var filename = data['name'];
690     var filedata = data['datablob'];
691     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
692    
693     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+filedata+"</textarea>\n";
694    
695     document.title="EditFile: "+filename;
696     updateEditMenu();
697     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
698    
699     setMode('Edit');
700     }
701    
702     function editFile(name){
703     if(currentMode == 'Preview'){
704     var data = getSVGContent();
705     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+data+"</textarea>\n";
706     updateEditMenu();
707     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
708    
709     setMode('Edit');
710     }else{
711     getFile(name, 'editFileData');
712     }
713     }
714    
715     function editCurrentFile(){
716     var datadiv = document.getElementById('tempdata');
717     var name = datadiv.getAttribute('lfname');
718    
719     editFile(name);
720     }
721    
722     // Show File
723     function updateShowMenu(){
724     var menuDiv = document.getElementById('menuDiv');
725     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
726     menuDiv.innerHTML+= "<button onClick=\"editCurrentFile();\"> Edit </button>\n";
727     menuDiv.innerHTML+= "<button onClick=\"saveData();\"> Save </button>\n";
728     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
729     menuDiv.innerHTML+= "<button onClick=\"showSVGSource();\"> &lt;SVG &gt; </button>\n";
730     menuDiv.innerHTML+= updateSVGObjMenu();
731     menuDiv.style.display='block';
732     updateToolBar();
733     }
734    
735 isao-hara 25 function showColorPalette(val){
736     var palette = document.getElementById('color-palette');
737     var ele;
738     var current = palette.getAttribute("targetType");
739 isao-hara 2
740 isao-hara 25 if(palette.style.display=='block' && current == val){
741     palette.style.display='none';
742     return;
743     }
744    
745     if(val == 'fill'){
746     ele = document.getElementById('toolFill');
747     }else{
748     ele = document.getElementById('toolStroke');
749     }
750    
751     var pos= ele.offsetTop + 110;
752     palette.style.top = pos +"px";
753     palette.style.display='block';
754     palette.setAttribute("targetType", val);
755    
756     }
757    
758 isao-hara 2 function updateToolBar(){
759     var toolbar = document.getElementById('toolBar');
760 isao-hara 25 var str = "";
761 isao-hara 14 if(!toolbar.innerHTML){
762 isao-hara 25 str += "<li><button id=\"b_selector\" onClick=\"setSVGMode('selector');\"><img src=\"images/arrow.png\" /></button> </li>\n";
763     str += "<li><button id=\"b_newPath\" onClick=\"setSVGMode('newPath'); \"><img src=\"images/path.png\" /></button></li> \n";
764     str += "<li><button id=\"b_newLine\" onClick=\"setSVGMode('newLine'); \"><img src=\"images/line.png\" /> </button></li> \n";
765     str += "<li><button id=\"b_text\" onClick=\"setSVGMode('text'); \"><img src=\"images/text.png\" /></button> </li>\n";
766     str += "<li><button id=\"b_rect\" onClick=\"setSVGMode('rect'); \"> <img src=\"images/rect.png\" /> </button></li> \n";
767     str += "<li><button id=\"b_circle\" onClick=\"setSVGMode('circle'); \"> <img src=\"images/circle.png\" /> </button></li> \n";
768     str += "<li><button id=\"b_ellipse\" onClick=\"setSVGMode('ellipse'); \"> <img src=\"images/ellipse.png\" /> </button></li> \n";
769     str += "<li><button id=\"b_image\" onClick=\"setSVGMode('image'); \"> <img src=\"images/img.png\" /> </button></li> \n";
770     str += "<li>&nbsp;</li>";
771     str += "<li><button id=\"b_fillColor\" onClick=\"showColorPalette('fill');\"><img src=\"images/Fill.png\" /><div id=\"toolFill\"></div></button></li> \n";
772     str += "<li><button id=\"b_strokeColor\" onClick=\"showColorPalette('stroke');\"><img src=\"images/Stroke.png\" /><div id=\"toolStroke\"></div></button></li> \n";
773     toolbar.innerHTML= "<ul>"+str+"</ul>";
774 isao-hara 14 }
775 isao-hara 25
776 isao-hara 2 toolbar.style.display='block';
777     }
778    
779     function setMode(m){
780     currentMode=m;
781     switch(m){
782     case 'List':
783     hideItemById('editDiv');
784     hideItemById('preview');
785     hideItemById('popup');
786     hideItemById('toolBar');
787 isao-hara 14 hideItemById('color-palette');
788 isao-hara 2 showItemById('menuDiv');
789     break;
790     case 'Edit':
791     hideItemById('preview');
792     hideItemById('popup');
793     hideItemById('toolBar');
794 isao-hara 14 hideItemById('color-palette');
795 isao-hara 2 showItemById('editDiv');
796     showItemById('menuDiv');
797     break;
798     case 'Preview':
799     showItemById('preview');
800     hideItemById('editDiv');
801     hideItemById('popup');
802 isao-hara 25 hideItemById('color-palette');
803 isao-hara 2 showItemById('menuDiv');
804     showItemById('toolBar');
805     break;
806     default:
807     break;
808     }
809     }
810    
811     function hideItemById(id){
812     var itm = document.getElementById(id);
813     if(itm) itm.style.display='none';
814     }
815    
816     function showItemById(id){
817     var itm = document.getElementById(id);
818     if(itm) itm.style.display='block';
819     }
820    
821     function removeChildNodes(id){
822     var itm = document.getElementById(id);
823     if(!itm) return;
824    
825     var child = itm.firstChild;
826    
827     while(child){
828     itm.removeChild(child);
829     child = itm.firstChild;
830     }
831     }
832    
833     function chEditareaHeight(){
834     var itm = document.getElementById('editarea');
835     if(!itm) return;
836     var cv = itm.getAttribute('rows');
837     if(parseInt(cv) > editarea_h){
838     itm.setAttribute('rows', editarea_h);
839     }else{
840     itm.setAttribute('rows', editarea_h * 2);
841     }
842     }
843    
844     function format_file(str){
845     return "<pre>"+str+"</pre>";
846     }
847    
848     function previewData(data){
849     if(!data) data = document.getElementById('editarea').value;
850    
851     previewFile(data);
852    
853     updateShowMenu();
854     setMode('Preview');
855     }
856    
857     function previewFile(data){
858     removeChildNodes('preview');
859    
860     preview.style.display='block';
861     preview.style.position='absolute';
862     preview.style.top='180px';
863 isao-hara 14 preview.style.bottom='50px';
864     preview.style.left='0px';
865     preview.style.right='10px';
866 isao-hara 21 preview.style.width='800px';
867     // preview.style.height='600px';
868     preview.style.height='525px';
869 isao-hara 14 mkColorPalette();
870 isao-hara 2
871     var ele = toSVGElement(data, '100%','100%');
872    
873     preview.appendChild(ele);
874    
875     svg_top = document.getElementById('svg_top');
876     initSVGElementId(svg_top);
877     svg_select = createSVGObj('rect', 'x=1,y=1,width=1,height=1,visibility=hidden,stroke-dasharray=9 5', 'none', 'blue', 2);
878     svg_select.setAttribute("id","svg_select");
879     appendSVGObj(svg_select);
880 isao-hara 14 }
881 isao-hara 2
882 isao-hara 14 function mkColorPalette(){
883     var palette = document.getElementById('color-palette');
884     if(!palette) return;
885     if(palette.innerHTML) return;
886    
887     palette.innerHTML="";
888    
889 isao-hara 32 palette.addEventListener("touchstart", onTouchStartColor, false);
890 isao-hara 30
891 isao-hara 25 // palette.innerHTML="<select id=\"color_sel\"><option value=\"fill\">Fill</option><option value=\"stroke\">Color</option></select>";
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 2 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    
1711     var fill=document.getElementById('svg_fill');
1712     var color=document.getElementById('svg_color');
1713     var L=document.getElementById('svg_stroke');
1714    
1715     var x = getPreviewX(sx);
1716     var y = getPreviewY(sy);
1717    
1718     switch(modeSVG){
1719     case 'text':
1720     var txt=document.getElementById('svg_text');
1721     var size=document.getElementById('svg_size');
1722 isao-hara 23 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 isao-hara 2 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 isao-hara 6 break;
1750 isao-hara 5 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 isao-hara 2 default:
1757     break;
1758     }
1759     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     }
1772     }
1773     }
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 4 if(targetItem || selectedItems.length > 0){
1909     switch(modeSVG){
1910     case 'newPath':
1911     if(targetItem.tagName == 'path' ){
1912     var path = targetItem.getAttribute("d");
1913 isao-hara 19 path = path + ' '+ getPreviewX(pageX) + ' '+ getPreviewY(pageY) ;
1914 isao-hara 4 targetItem.setAttribute("d",path);
1915     }
1916     break;
1917     case 'newLine':
1918     if(targetItem.tagName == 'line' ){
1919 isao-hara 19 var x2 = getPreviewX(pageX);
1920     var y2 = getPreviewY(pageY);
1921 isao-hara 4 targetItem.setAttribute("x2",x2);
1922     targetItem.setAttribute("y2",y2);
1923     }
1924     break;
1925 isao-hara 2
1926 isao-hara 4 default:
1927 isao-hara 19 var dx = pageX - sx;
1928     var dy = pageY - sy;
1929 isao-hara 2
1930 isao-hara 4 if(selectedItems.length == 1 && getElementTag(selectedItems[0]) == 'line'){
1931 isao-hara 19 var lx = getPreviewX(pageX);
1932     var ly = getPreviewY(pageY);
1933 isao-hara 2
1934 isao-hara 4 if(!lineEdit){
1935     var x1 = getAttributeVal(selectedItems[0],"x1");
1936     var y1 = getAttributeVal(selectedItems[0],"y1");
1937     var x2 = getAttributeVal(selectedItems[0],"x2");
1938     var y2 = getAttributeVal(selectedItems[0],"y2");
1939     var xc = (x1+x2)/2;
1940     var yc = (y1+y2)/2;
1941     var eS = Math.min(Math.abs(x1-lx), Math.abs(y1-ly));
1942     var eC = Math.min(Math.abs(xc-lx), Math.abs(yc-ly));
1943     var eE = Math.min(Math.abs(x2-lx), Math.abs(y2-ly));
1944 isao-hara 2
1945 isao-hara 4 var minVal = Math.min(eS, Math.min(eC, eE));
1946     if(minVal == eS) lineEdit='start';
1947     else if(minVal == eE) lineEdit='end';
1948     else lineEdit='move';
1949     }
1950     if(lineEdit=='start'){
1951     selectedItems[0].setAttribute("x1",lx );
1952     selectedItems[0].setAttribute("y1",ly );
1953     setSelectBox();
1954     }else if(lineEdit == 'end'){
1955     selectedItems[0].setAttribute("x2",lx );
1956     selectedItems[0].setAttribute("y2",ly );
1957     setSelectBox();
1958     }else{
1959     selectedItems[0].setAttribute("transform","translate("+dx+","+dy+")");
1960     moveSelectedRectangle(dx, dy);
1961     }
1962     }else{
1963     for(var i=0; i<selectedItems.length;i++){
1964 isao-hara 19 if(selectedItems[i]) selectedItems[i].setAttribute("transform","translate("+dx+","+dy+")");
1965 isao-hara 4 }
1966     moveSelectedRectangle(dx, dy);
1967     }
1968     break;
1969     }
1970     updateShowMenu();
1971     }else if(modeSVG == 'selector'){
1972 isao-hara 19 ex = pageX;
1973     ey = pageY;
1974 isao-hara 4 var x1=sx;
1975     var y1=sy;
1976     var x2=ex;
1977     var y2=ey;
1978 isao-hara 2
1979 isao-hara 4 if(sx > ex){ x1=ex; x2=sx; }
1980     if(sy > ey){ y1=ey; y2=sy; }
1981 isao-hara 2
1982 isao-hara 4 selectedRectangle(getPreviewX(x1), getPreviewY(y1), getPreviewX(x2), getPreviewY(y2));
1983     }
1984 isao-hara 2 }
1985    
1986 isao-hara 23 function svgInputTextExec(e){
1987     var inputform = document.getElementById('svg_input');
1988     var color=document.getElementById('svg_color');
1989     var size=document.getElementById('svg_size');
1990 isao-hara 29 if(!color || !size) return;
1991 isao-hara 24 var y = parseInt(inputform.style.top) + parseInt(size.value)*0.8 + 12;
1992     var x = parseInt(inputform.style.left) + 12;
1993 isao-hara 23 if(inputform.value){
1994 isao-hara 24 targetItem=createSVGText(inputform.value, x, y, size.value, color.value);
1995    
1996 isao-hara 23 appendSVGObj(targetItem);
1997     }
1998     inputform.setAttribute("type", "hidden");
1999     setSVGMode('selector');
2000     }
2001    
2002 isao-hara 25 function svgModifyTextExec(e){
2003     var inputform = document.getElementById('svg_input');
2004     editingTextObj.textContent = inputform.value;
2005     inputform.setAttribute("type", "hidden");
2006     editingTextObj.style.display='block';
2007 isao-hara 27
2008     editingTextObj=null;
2009 isao-hara 25 setSVGMode('selector');
2010     }
2011    
2012 isao-hara 23 function svgInputFormAdjust(e){
2013     var inputform = document.getElementById('svg_input');
2014 isao-hara 24 inputform.size = jstrlen(inputform.value) + 1;
2015 isao-hara 23 }
2016    
2017 isao-hara 24 function jstrlen(str) {
2018     var len = 0;
2019     str = escape(str);
2020     for (var i = 0; i < str.length; i++, len++) {
2021     if (str.charAt(i) == "%") {
2022     if (str.charAt(++i) == "u") {
2023     i += 3;
2024     len++;
2025     }
2026     i++;
2027     }
2028     }
2029     return len;
2030     }
2031    
2032 isao-hara 25 function putInputForm(x, y, txt, size, id){
2033 isao-hara 23 var inputform = document.getElementById('svg_input');
2034     if(!inputform){
2035     inputform = document.createElement('input');
2036     inputform.setAttribute("id", "svg_input");
2037     inputform.setAttribute("style", "position:absolute;top:0px;left:0px; border:0px none");
2038 isao-hara 29 preview.appendChild(inputform);
2039     }
2040    
2041     if(document.addEventListner){
2042     if(id){
2043     inputform.addEventListener("onChange", svgModifyTextExec,false);
2044 isao-hara 23 }else{
2045 isao-hara 29 inputform.addEventListener("onChange", svgInputTextExec,false);
2046 isao-hara 23 }
2047 isao-hara 29 inputform.addEventListener("onkeydown", svgInputFormAdjust,false);
2048     }else{
2049     if(id){
2050     inputform.setAttribute("onChange", "svgModifyTextExec()");
2051     }else{
2052     inputform.setAttribute("onChange", "svgInputTextExec()");
2053     }
2054     inputform.setAttribute("onkeydown", "svgInputFormAdjust()");
2055 isao-hara 23 }
2056    
2057     inputform.setAttribute("type", "text");
2058     inputform.style.left=x+'px';
2059     inputform.style.top=y+'px';
2060     inputform.style.fontSize=size+'px';
2061     inputform.style.background='none';
2062     inputform.value = txt;
2063 isao-hara 25 inputform.size = jstrlen(txt) + 1;
2064 isao-hara 23 inputform.focus();
2065     }
2066    
2067     ///// EventHandler for iPad
2068 isao-hara 25 var firstTouch = new Date();
2069    
2070 isao-hara 23 function onTouchStart(e){
2071     //e.preventDefault();
2072     sx=e.touches[0].pageX;
2073     sy=e.touches[0].pageY;
2074     ex=e.touches[0].pageX;
2075     ey=e.touches[0].pageY;
2076    
2077     if (e.touches.length == 1){
2078 isao-hara 25 var touchtime = new Date();
2079     var dt = touchtime.getTime() - firstTouch.getTime();
2080 isao-hara 27 if(editingTextObj){
2081     var inputform = document.getElementById('svg_input');
2082     inputform.setAttribute("type", "hidden");
2083     editingTextObj.style.display='block';
2084     editingTextObj=null;
2085     }
2086 isao-hara 23
2087 isao-hara 25 if(dt < 300 ){
2088     onDoubleTap();
2089     }else{
2090     onTouchStartCore();
2091     }
2092     firstTouch = touchtime;
2093     }
2094    
2095 isao-hara 23 updateSelectedRectangle();
2096     }
2097    
2098     function onTouchMove(e){
2099    
2100     if (e.touches.length == 1){
2101 isao-hara 25 e.preventDefault();
2102 isao-hara 23 onTouchMoveCode1(e.touches[0].pageX, e.touches[0].pageY);
2103     }else if (e.touches.length == 2){
2104 isao-hara 25 e.preventDefault();
2105 isao-hara 23 var dx = e.touches[0].pageX-e.touches[1].pageX;
2106     var dy = e.touches[0].pageY-e.touches[1].pageY;
2107     var th = Math.abs(Math.atan2(dy , dx)/Math.PI *180);
2108    
2109     if(th > 165 || th < 25) svg_scale_dir = 'x';
2110     else if(th > 65 && th < 115) svg_scale_dir = 'y';
2111     else svg_scale_dir = null;
2112     }
2113     }
2114    
2115 isao-hara 2 function onTouchEnd(e){
2116     e.preventDefault();
2117    
2118     for(var i=0; i<selectedItems.length;i++){
2119     updateTransform(selectedItems[i]);
2120     }
2121    
2122     if(modeSVG == 'selector'){
2123     var x1 = getPreviewX(sx);
2124     var y1 = getPreviewY(sy);
2125     var x2 = getPreviewX(ex);
2126     var y2 = getPreviewY(ey);
2127     getSelectedObjects(x1, y1, x2, y2);
2128     }
2129    
2130     updateSelectedRectangle();
2131     updateTransform(svg_select);
2132    
2133     setSelectBox();
2134     updateShowMenu();
2135 isao-hara 4 var btn = document.getElementById('b_'+modeSVG);
2136     if(btn){
2137     btn.style.border="3px solid red";
2138     }
2139 isao-hara 2 lineEdit=null;
2140     }
2141    
2142    
2143     function onGestureStart(e){
2144     targetItem = selectedItems[0];
2145     if(targetItem){
2146     svg_wo = targetItem.getAttribute("width");
2147     svg_ho = targetItem.getAttribute("height");
2148     svg_ro = targetItem.getAttribute("r");
2149     svg_rxo = targetItem.getAttribute("rx");
2150     svg_ryo = targetItem.getAttribute("ry");
2151     svg_fsize = targetItem.getAttribute("font-size");
2152     }else{
2153     svg_wo = null;
2154     svg_ho = null;
2155     svg_ro = null;
2156     svg_rxo = null;
2157     svg_ryo = null;
2158     svg_fsize = null;
2159     }
2160     }
2161    
2162     function onGestureChange(e){
2163 isao-hara 4 var scale = e.scale;
2164     var rotation = e.rotation;
2165 isao-hara 2
2166 isao-hara 4 if(targetItem){
2167 isao-hara 7 e.preventDefault();
2168 isao-hara 4 if (svg_wo && svg_scale_dir != 'y') targetItem.setAttribute("width", Math.round(svg_wo*scale ));
2169     if (svg_ho && svg_scale_dir != 'x') targetItem.setAttribute("height", Math.round(svg_ho*scale ));
2170     if (svg_ro) targetItem.setAttribute("r", Math.round(svg_ro*scale ));
2171     if (svg_rxo && svg_scale_dir != 'y') targetItem.setAttribute("rx", Math.round(svg_rxo*scale) );
2172     if (svg_ryo && svg_scale_dir != 'x') targetItem.setAttribute("ry", Math.round(svg_ryo*scale) );
2173     if (svg_fsize) targetItem.setAttribute("font-size", Math.round(svg_fsize*scale) );
2174 isao-hara 2
2175 isao-hara 4 updateShowMenu();
2176     }
2177 isao-hara 2 }
2178    
2179     function onGestureEnd(e){
2180 isao-hara 7 // e.preventDefault();
2181 isao-hara 4 svg_wo = null;
2182     svg_ho = null;
2183     svg_ro = null;
2184     svg_rxo = null;
2185     svg_ryo = null;
2186     svg_fsize = null;
2187 isao-hara 2 }
2188    
2189 isao-hara 14 function onTouchStartColor(e){
2190     if(e.touches.length == 1){
2191     var ele = e.touches[0].target;
2192 isao-hara 25 var color;
2193     try{
2194 isao-hara 32 color = ele.getAttribute("color-val");
2195 isao-hara 25 }catch(err){ hideItemById('color-palette'); return;}
2196     var palette = document.getElementById('color-palette');
2197     var color_sel = palette.getAttribute("targetType");
2198 isao-hara 14 var fill_input = document.getElementById('svg_fill');
2199     var color_input = document.getElementById('svg_color');
2200     if(color) {
2201 isao-hara 25 for(var i=0;i < selectedItems.length; i++){
2202     selectedItems[i].setAttribute(color_sel, color);
2203     if(color_sel == 'fill'){
2204     if(fill_input) fill_input.value=color;
2205     document.getElementById('toolFill').style.backgroundColor=color;
2206 isao-hara 14 }else{
2207     color_input.value=color;
2208 isao-hara 25 document.getElementById('toolStroke').style.backgroundColor=color;
2209     }
2210 isao-hara 14 }
2211     }
2212 isao-hara 25 palette.style.display='none';
2213 isao-hara 14 }
2214     }
2215 isao-hara 2 ///// For Safari
2216 isao-hara 19 var mouse_state=0;
2217 isao-hara 2
2218 isao-hara 19 function onMouseDown(e){
2219     mouse_state=e.which;
2220 isao-hara 18
2221     sx=e.pageX;
2222     sy=e.pageY;
2223 isao-hara 19 x=e.pageX;
2224 isao-hara 18 ey=e.pageY;
2225    
2226 isao-hara 19 if(isChildById(e.target, 'preview') || e.target.id == 'preview') {
2227 isao-hara 29 if(editingTextObj){
2228     var inputform = document.getElementById('svg_input');
2229     inputform.setAttribute("type", "hidden");
2230     editingTextObj.style.display='block';
2231     editingTextObj=null;
2232     }
2233    
2234     if(e.altKey){
2235     targetItem = selectedItems[0];
2236     if(targetItem){
2237     svg_wo = targetItem.getAttribute("width");
2238     svg_ho = targetItem.getAttribute("height");
2239     svg_ro = targetItem.getAttribute("r");
2240     svg_rxo = targetItem.getAttribute("rx");
2241     svg_ryo = targetItem.getAttribute("ry");
2242     svg_fsize = targetItem.getAttribute("font-size");
2243     }else{
2244     svg_wo = null;
2245     svg_ho = null;
2246     svg_ro = null;
2247     svg_rxo = null;
2248     svg_ryo = null;
2249     svg_fsize = null;
2250     }
2251     }else{
2252     onTouchStartCore();
2253     }
2254 isao-hara 19 updateSelectedRectangle();
2255     return false;
2256     }else if(isChildById(e.target, 'color-palette') && e.target.tagName == 'DIV'){
2257     var ele = e.target;
2258 isao-hara 30 var palette = document.getElementById('color-palette');
2259 isao-hara 19 var color = ele.getAttribute("color-val");
2260 isao-hara 30 var color_sel = palette.getAttribute("targetType");
2261 isao-hara 19 var fill_input = document.getElementById('svg_fill');
2262     var color_input = document.getElementById('svg_color');
2263     if(color) {
2264     for(var i=0;i < selectedItems.length; i++){
2265 isao-hara 30 selectedItems[i].setAttribute(color_sel, color);
2266     if(color_sel == 'fill'){
2267     fill_input.value=color;
2268     document.getElementById('toolFill').style.backgroundColor=color;
2269     }else{
2270     color_input.value=color;
2271     document.getElementById('toolStroke').style.backgroundColor=color;
2272     }
2273 isao-hara 19 }
2274 isao-hara 30 palette.style.display='none';
2275 isao-hara 19 }
2276     return false;
2277     }
2278     return true;
2279     }
2280 isao-hara 18
2281 isao-hara 19 function onMouseMove(e){
2282     if(!isChildById(e.target, 'preview') && e.target.id != 'preview') { return true; }
2283 isao-hara 18
2284 isao-hara 29 if(mouse_state == 1) {
2285     if(e.altKey){
2286     var dx = sx-e.pageX;
2287     var dy = sy-e.pageY;
2288     var th = Math.abs(Math.atan2(dy , dx)/Math.PI *180);
2289     var scale;
2290 isao-hara 18
2291 isao-hara 29 if(th > 165 || th < 25){
2292     svg_scale_dir = 'x';
2293     scale = -dx/50;
2294     }else if(th > 65 && th < 115){
2295     svg_scale_dir = 'y';
2296     scale = -dy/50;
2297     }else{
2298     svg_scale_dir = null;
2299     if(dx > 0 && dy > 0) scale = -dx*dy/2500;
2300     else if(dx < 0 && dy < 0) scale = dx*dy/2500;
2301     else scale = 1;
2302     }
2303    
2304     if(scale >= 0){
2305     if(scale < 1) scale=1;
2306     }else{
2307     if(scale > -1) scale=1;
2308     else{
2309     scale= -1/scale;
2310     }
2311     }
2312     if(targetItem){
2313     if (svg_wo && svg_scale_dir != 'y') targetItem.setAttribute("width", Math.round(svg_wo*scale ));
2314     if (svg_ho && svg_scale_dir != 'x') targetItem.setAttribute("height", Math.round(svg_ho*scale ));
2315     if (svg_ro) targetItem.setAttribute("r", Math.round(svg_ro*scale ));
2316     if (svg_rxo && svg_scale_dir != 'y') targetItem.setAttribute("rx", Math.round(svg_rxo*scale) );
2317     if (svg_ryo && svg_scale_dir != 'x') targetItem.setAttribute("ry", Math.round(svg_ryo*scale) );
2318     if (svg_fsize) targetItem.setAttribute("font-size", Math.round(svg_fsize*scale) );
2319    
2320     updateShowMenu();
2321     }
2322     }else {
2323     onTouchMoveCode1(e.pageX, e.pageY);
2324     }
2325     }
2326    
2327 isao-hara 19 return false;
2328     }
2329 isao-hara 18
2330 isao-hara 19 function onMouseUp(e){
2331     mouse_state = 0;
2332     if(!isChildById(e.target, 'preview') && e.target.id != 'preview') { return true; }
2333 isao-hara 18
2334 isao-hara 19 for(var i=0; i<selectedItems.length;i++){
2335     updateTransform(selectedItems[i]);
2336     }
2337 isao-hara 18
2338 isao-hara 19 if(modeSVG == 'selector'){
2339     var x1 = getPreviewX(sx);
2340     var y1 = getPreviewY(sy);
2341     var x2 = getPreviewX(ex);
2342     var y2 = getPreviewY(ey);
2343     getSelectedObjects(x1, y1, x2, y2);
2344     }
2345 isao-hara 18
2346 isao-hara 19 updateSelectedRectangle();
2347     updateTransform(svg_select);
2348 isao-hara 18