Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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