Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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