Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (hide annotations) (download) (as text)
Wed Jul 28 08:17:03 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 82322 byte(s)
menu changed
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 46 var svg_rotate_locked = false;
64     var svg_scale_locked = false;
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 isao-hara 51 new Function("transaction",
273     "transaction.executeSql('SELECT id,name from files where name=?;',['"+new_name+"'],"+
274 isao-hara 2 "function (transaction, results) {"+
275     "if(results.rows.length == 0){"+
276     "reallyRenameFileAction('"+id+"','"+new_name+"');"+
277     "}else{"+
278     "alert(results.rows.item(0)['name']+' already exist');"+
279     "fileSelector();"+
280     "}"+
281     "}, errorHandler);")
282     );
283    
284     }
285    
286     /// Save File
287     function saveFile(db){
288     var myDB = db;
289     if(!db) myDB = systemDB;
290    
291     var editarea = document.getElementById('editarea');
292     var contents = "";
293     contents = editarea.value;
294    
295     myDB.transaction(
296     function (transaction) {
297     var datadiv = document.getElementById('tempdata');
298     var filedata_id = datadiv.getAttribute('lfdataid');
299    
300     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
301     [ contents, filedata_id ], nullDataHandler, errorHandler);
302    
303     alert('Saved.');
304     }
305     );
306     }
307    
308     function saveData(db){
309     var myDB = db;
310     if(!db) myDB = systemDB;
311    
312     var contents = "";
313     contents = getSVGContent();
314    
315     myDB.transaction(
316     function (transaction) {
317     var datadiv = document.getElementById('tempdata');
318     var filedata_id = datadiv.getAttribute('lfdataid');
319    
320     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
321     [ contents, filedata_id ], nullDataHandler, errorHandler);
322    
323     alert('Saved.');
324     }
325     );
326     }
327    
328 isao-hara 14 function saveContent(contents, db){
329     var myDB = db;
330     if(!db) myDB = systemDB;
331    
332     if(!contents) {
333     alert("Invalid content");
334     return;
335     }
336    
337     myDB.transaction(
338     function (transaction) {
339     var datadiv = document.getElementById('tempdata');
340     var filedata_id = datadiv.getAttribute('lfdataid');
341    
342     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
343     [ contents, filedata_id ], nullDataHandler, errorHandler);
344    
345     alert('Saved.');
346     }
347     );
348     }
349    
350 isao-hara 17 function reallySaveNewFileAction(fname, db){
351     var myDB = db;
352     if(!db) myDB = systemDB;
353    
354     var datadiv = document.getElementById('tempdata');
355     content = datadiv.textContent;
356    
357     myDB.transaction(
358     function (transaction) {
359     var myfunc = new Function("transaction", "results", "transaction.executeSql('INSERT INTO files (name, filedata_id) VALUES (?, ?);', [ '"+fname+"', results.insertId], nullDataHandler, killTransaction);");
360    
361     transaction.executeSql('INSERT INTO filedata (datablob) VALUES (?);', [content], myfunc, errorHandler);
362     }
363     );
364     datadiv.textContent="";
365     alert("download to "+fname);
366     }
367    
368     function saveContentFilename(id, db){
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     function renameFile(name, id){
576     var popupDiv = document.getElementById('popup');
577     var string = "";
578    
579     string += "<H1 class='title'>Rename File</H1>\n";
580     string += "<div class='input_form'>\n";
581     string += "Old Filename: "+name+"<br>";
582     string += "New Filename:<input id='newFilename' name='newname' value=\"\" />\n";
583     string += "<input type='hidden' id='fileId' value=\""+id+"\" /><br>\n";
584     string += "<button onClick=\"renameFileAction();hideItemById('popup');\">Rename</button>\n";
585     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
586     string += "</div>\n";
587    
588     popupDiv.innerHTML=string;
589     popupDiv.style.display='block';
590     }
591    
592     /// EditMenu
593     function insertStr(str, len){
594     var editarea = document.getElementById('editarea');
595     if(len < 0) len = str.length;
596    
597     if (editarea){
598     if(str=='DQ') str='"';
599     var strs = editarea.value;
600     var cPos = editarea.selectionStart;
601     var tmp = strs.substr(0,cPos);
602     editarea.value = tmp +str+strs.substr(cPos, strs.length);
603     cPos += len;
604     editarea.setSelectionRange(cPos, cPos);
605     editarea.focus();
606     }
607     }
608    
609     function insertTag(tag){
610     var str;
611     var len = -1;
612     if (tag == "p"){
613 isao-hara 51 str = "<p> </p>";
614     len = 4;
615 isao-hara 2 }else if (tag == "ul"){
616 isao-hara 51 str = "<ul class=\" \"> \n\n</ul>";
617     len = 17;
618 isao-hara 2 }else if (tag == "li"){
619 isao-hara 51 str = "<li> </li>";
620     len = 5;
621 isao-hara 2 }else if (tag == "href"){
622 isao-hara 51 str = "xlink:href=\"\"";
623     len = 12;
624 isao-hara 2 }else if (tag == "EQ"){
625 isao-hara 51 str = "=\"\"";
626     len = 2;
627 isao-hara 2 }else if (tag == "svg:text"){
628 isao-hara 51 str = "<svg:text x=\"100\" y=\"100\" fill=\"black\" font-size=\"12\"> </svg:text>";
629     len = 58;
630 isao-hara 2 }else if (tag == "svg:rect"){
631 isao-hara 51 str = "<svg:rect x=\"10\" y=\"10\" width=\"100\" height=\"100\" fill=\"white\" stroke=\"black\" />";
632     len = 13;
633 isao-hara 2 }else if (tag == "svg:circle"){
634 isao-hara 51 str = "<svg:circle cx=\"10\" cy=\"100\" r=\"100\" fill=\"white\" stroke=\"black\" strokc-width=\"1\"/>";
635     len = 17;
636 isao-hara 2 }
637     insertStr(str, len);
638     }
639    
640     function editMenuBar()
641     {
642     var str = "";
643     str += "<button onClick=\"insertStr('/',1);\">/</button>\n";
644     str += "<button onClick=\"insertStr('DQ',1);\">\"</button>\n";
645     str += "<button onClick=\"insertTag('EQ',1);\">=\"\"</button>\n";
646     str += "<button onClick=\"insertTag('p');\">p</button>\n";
647     str += "<button onClick=\"insertTag('href');\">href</button>\n";
648    
649     str += "<button onClick=\"insertTag('svg:text');\">TEXT</button>\n";
650     str += "<button onClick=\"insertTag('svg:rect');\">rect</button>\n";
651     str += "<button onClick=\"insertTag('svg:circle');\">circle</button>\n";
652    
653     str += "<button onClick=\"chEditareaHeight();\">...</button>\n";
654    
655     return str;
656     }
657    
658     function updateEditMenu(){
659     var menuDiv = document.getElementById('menuDiv');
660     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
661     menuDiv.innerHTML+= "<button onClick=\"saveFile();fileSelector();\"> Save </button>\n";
662     menuDiv.innerHTML+= "<button onClick=\"previewData();\"> Preview </button>\n";
663     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
664    
665     menuDiv.style.display='block';
666     }
667    
668     //// Editor
669     function editFileData(transaction, results){
670     var editDiv = document.getElementById('editDiv');
671     var datadiv = document.getElementById('tempdata');
672    
673 isao-hara 18 if( results.rows.length == 0) return;
674 isao-hara 2 var data = results.rows.item(0);
675     var filename = data['name'];
676     var filedata = data['datablob'];
677     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
678    
679     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+filedata+"</textarea>\n";
680    
681     document.title="EditFile: "+filename;
682     updateEditMenu();
683     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
684    
685     setMode('Edit');
686     }
687    
688     function editFile(name){
689     if(currentMode == 'Preview'){
690     var data = getSVGContent();
691     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+data+"</textarea>\n";
692     updateEditMenu();
693     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
694    
695     setMode('Edit');
696     }else{
697     getFile(name, 'editFileData');
698     }
699     }
700    
701     function editCurrentFile(){
702     var datadiv = document.getElementById('tempdata');
703     var name = datadiv.getAttribute('lfname');
704    
705     editFile(name);
706     }
707    
708 isao-hara 36
709 isao-hara 51 // Show File (GUI Editor)
710 isao-hara 2 function updateShowMenu(){
711     var menuDiv = document.getElementById('menuDiv');
712     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
713 isao-hara 51 var menu_str = "<img src=\"images/menu.png\" usemap=\"#topmenu\" />\n";
714     menu_str+= "<map name=\"topmenu\">";
715     menu_str+= "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"fileSelector();\">";
716     menu_str+= "<area shape=\"rect\" coords=\"30,0,60,25\" onClick=\"saveData();\">";
717     menu_str+= "<area shape=\"rect\" coords=\"60,0,90,25\" onClick=\"showSVGSource();\">";
718     menu_str+= "</map>";
719 isao-hara 41
720 isao-hara 51 menu_str+= updateSVGObjMenu();
721     menuDiv.innerHTML+= menu_str;
722 isao-hara 52 updateStrokeWidth();
723 isao-hara 54 updatePathTypeMenu();
724     updateArrowMenu();
725 isao-hara 2 menuDiv.style.display='block';
726     updateToolBar();
727     }
728    
729 isao-hara 25 function showColorPalette(val){
730     var palette = document.getElementById('color-palette');
731     var ele;
732     var current = palette.getAttribute("targetType");
733 isao-hara 2
734 isao-hara 25 if(palette.style.display=='block' && current == val){
735     palette.style.display='none';
736     return;
737     }
738    
739     if(val == 'fill'){
740     ele = document.getElementById('toolFill');
741     }else{
742     ele = document.getElementById('toolStroke');
743     }
744    
745     var pos= ele.offsetTop + 110;
746     palette.style.top = pos +"px";
747     palette.style.display='block';
748     palette.setAttribute("targetType", val);
749    
750     }
751    
752 isao-hara 51 function selectToolBar(idx){
753     var ele=document.getElementById('tool_select');
754     var pos = idx *25;
755     ele.style.top= pos+'px';
756     }
757    
758    
759 isao-hara 2 function updateToolBar(){
760     var toolbar = document.getElementById('toolBar');
761 isao-hara 25 var str = "";
762 isao-hara 14 if(!toolbar.innerHTML){
763 isao-hara 51 str += "<li><img src=\"images/tools.png\" usemap=\"#toolbar\" />\n";
764     str += "<img id=\"tool_select\" src=\"images/select.png\" style=\"position:absolute;top:0;left:10px;\" />\n";
765     str += "<map name=\"toolbar\">";
766     str += "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"setSVGMode('selector');\">";
767     str += "<area shape=\"rect\" coords=\"0,25,30,50\" onClick=\"setSVGMode('newPath');\">";
768     str += "<area shape=\"rect\" coords=\"0,50,30,75\" onClick=\"setSVGMode('newLine');\">";
769     str += "<area shape=\"rect\" coords=\"0,75,30,100\" onClick=\"setSVGMode('text');\">";
770     str += "<area shape=\"rect\" coords=\"0,100,30,125\" onClick=\"setSVGMode('rect');\">";
771     str += "<area shape=\"rect\" coords=\"0,125,30,150\" onClick=\"setSVGMode('circle');\">";
772     str += "<area shape=\"rect\" coords=\"0,150,30,175\" onClick=\"setSVGMode('ellipse');\">";
773     str += "<area shape=\"rect\" coords=\"0,175,30,200\" onClick=\"setSVGMode('image');\">";
774     str += "<area shape=\"rect\" coords=\"0,210,30,235\" onClick=\"showColorPalette('fill');\">";
775     str += "<area shape=\"rect\" coords=\"0,240,30,265\" onClick=\"showColorPalette('stroke');\">";
776     str += "<area shape=\"rect\" coords=\"0,280,30,305\" onClick=\"topItem();\">";
777     str += "<area shape=\"rect\" coords=\"0,305,30,330\" onClick=\"upItem();\">";
778     str += "<area shape=\"rect\" coords=\"0,330,30,355\" onClick=\"downItem();\">";
779     str += "<area shape=\"rect\" coords=\"0,355,30,380\" onClick=\"bottomItem();\">";
780     str += "<area shape=\"rect\" coords=\"0,390,30,415\" onClick=\"dupObject();\">";
781     str += "<area shape=\"rect\" coords=\"0,415,30,440\" onClick=\"delSVGObj();\">";
782     str += "</map>";
783     str += "<div id=\"toolFill\"></div>\n";
784     str += "<div id=\"toolStroke\"></div>\n";
785     str += "</li>\n";
786     str += "<li><p /></li>\n";
787     str += "<li><img src=\"images/rot.png\" onClick=\"toggleRotateLock();\" id=\"rot-unlock\"/>\n";
788     str += "<img src=\"images/rot-lock.png\" style=\"display:none;\" onClick=\"toggleRotateLock();\"id=\"rot-lock\" />\n";
789     str += "</li>\n";
790     str += "<li><img src=\"images/scale.png\" onClick=\"toggleScaleLock();\" id=\"scale-unlock\"/>\n";
791     str += "<img src=\"images/scale-lock.png\" style=\"display:none;\" onClick=\"toggleScaleLock();\" id=\"scale-lock\"/>\n";
792     str += "</li>\n";
793 isao-hara 25 toolbar.innerHTML= "<ul>"+str+"</ul>";
794 isao-hara 14 }
795 isao-hara 2 toolbar.style.display='block';
796     }
797    
798     function setMode(m){
799     currentMode=m;
800     switch(m){
801     case 'List':
802     hideItemById('editDiv');
803     hideItemById('preview');
804     hideItemById('popup');
805     hideItemById('toolBar');
806 isao-hara 14 hideItemById('color-palette');
807 isao-hara 2 showItemById('menuDiv');
808     break;
809     case 'Edit':
810     hideItemById('preview');
811     hideItemById('popup');
812     hideItemById('toolBar');
813 isao-hara 14 hideItemById('color-palette');
814 isao-hara 2 showItemById('editDiv');
815     showItemById('menuDiv');
816     break;
817     case 'Preview':
818     showItemById('preview');
819     hideItemById('editDiv');
820     hideItemById('popup');
821 isao-hara 25 hideItemById('color-palette');
822 isao-hara 2 showItemById('menuDiv');
823     showItemById('toolBar');
824     break;
825     default:
826     break;
827     }
828     }
829    
830     function hideItemById(id){
831     var itm = document.getElementById(id);
832     if(itm) itm.style.display='none';
833     }
834    
835     function showItemById(id){
836     var itm = document.getElementById(id);
837     if(itm) itm.style.display='block';
838     }
839    
840     function removeChildNodes(id){
841     var itm = document.getElementById(id);
842     if(!itm) return;
843    
844     var child = itm.firstChild;
845    
846     while(child){
847     itm.removeChild(child);
848     child = itm.firstChild;
849     }
850     }
851    
852     function chEditareaHeight(){
853     var itm = document.getElementById('editarea');
854     if(!itm) return;
855     var cv = itm.getAttribute('rows');
856     if(parseInt(cv) > editarea_h){
857     itm.setAttribute('rows', editarea_h);
858     }else{
859     itm.setAttribute('rows', editarea_h * 2);
860     }
861     }
862    
863     function format_file(str){
864     return "<pre>"+str+"</pre>";
865     }
866    
867     function previewData(data){
868     if(!data) data = document.getElementById('editarea').value;
869    
870     previewFile(data);
871    
872     updateShowMenu();
873     setMode('Preview');
874     }
875    
876     function previewFile(data){
877     removeChildNodes('preview');
878    
879     preview.style.display='block';
880     preview.style.position='absolute';
881     preview.style.top='180px';
882 isao-hara 14 preview.style.bottom='50px';
883     preview.style.left='0px';
884     preview.style.right='10px';
885 isao-hara 21 preview.style.width='800px';
886     preview.style.height='525px';
887 isao-hara 14 mkColorPalette();
888 isao-hara 2
889     var ele = toSVGElement(data, '100%','100%');
890    
891     preview.appendChild(ele);
892    
893     svg_top = document.getElementById('svg_top');
894     initSVGElementId(svg_top);
895     svg_select = createSVGObj('rect', 'x=1,y=1,width=1,height=1,visibility=hidden,stroke-dasharray=9 5', 'none', 'blue', 2);
896     svg_select.setAttribute("id","svg_select");
897     appendSVGObj(svg_select);
898 isao-hara 14 }
899 isao-hara 2
900 isao-hara 14 function mkColorPalette(){
901     var palette = document.getElementById('color-palette');
902     if(!palette) return;
903     if(palette.innerHTML) return;
904    
905     palette.innerHTML="";
906 isao-hara 32 palette.addEventListener("touchstart", onTouchStartColor, false);
907 isao-hara 30
908 isao-hara 14 for(var i=0; i<colors.length ;i++){
909     if(colors[i] == 'none')
910     palette.innerHTML +="<div class=\"item\" style=\"background-color:"+colors[i]+";\" color-val=\""+colors[i]+"\">X</div>";
911     else
912     palette.innerHTML +="<div class=\"item\" style=\"background-color:"+colors[i]+";\" color-val=\""+colors[i]+"\"> </div>";
913     }
914 isao-hara 25 palette.style.width='120px';
915 isao-hara 2 }
916    
917     ////// for SVG object
918     ////////////////
919 isao-hara 42 function downItem(){
920     if(selectedItems.length != 1) return;
921     var itm = selectedItems[0];
922     var nodes = svg_top.childNodes;
923     for(var i=0; i< nodes.length; i++){ if(nodes[i] == itm) break; }
924     if (i > 0) svg_top.insertBefore(itm, nodes[i-1]);
925     }
926    
927     function upItem(){
928     if(selectedItems.length != 1) return;
929     var itm = selectedItems[0];
930     var nodes = svg_top.childNodes;
931     for(var i=0; i< nodes.length; i++){ if(nodes[i] == itm) break; }
932    
933     if (i == nodes.length-2){
934     svg_top.appendChild(itm);
935     }else if (i < nodes.length-1){
936     svg_top.insertBefore(itm, nodes[i+2]);
937     }
938     }
939    
940     function bottomItem(){
941     if(selectedItems.length != 1) return;
942     var itm = selectedItems[0];
943     svg_top.insertBefore(itm, svg_top.firstChild);
944     }
945    
946     function topItem(){
947     if(selectedItems.length != 1) return;
948     var itm = selectedItems[0];
949     svg_top.removeChild(itm);
950     svg_top.appendChild(itm);
951     }
952    
953 isao-hara 2 function updateSVGObjMenu(){
954     var res = "";
955     var tag = modeSVG;
956     if(selectedItems.length > 1) return res;
957     if(selectedItems.length == 1) tag = getElementTag(selectedItems[0]);
958    
959     switch(tag){
960     case 'text':
961     if(selectedItems[0]){
962     res += setSVGObjectProp(selectedItems[0]);
963     }else{
964 isao-hara 36 res += propSVGText("", svg_font_size, svg_color, 0);
965 isao-hara 2 }
966     break;
967     case 'rect':
968     case 'circle':
969     case 'ellipse':
970     case 'newPath':
971     case 'newLine':
972     case 'path':
973     case 'line':
974     case 'polyline':
975     case 'polygon':
976     if(selectedItems[0]){
977     res += setSVGObjectProp(selectedItems[0]);
978     }else{
979 isao-hara 39 res += propSVGObj(tag+":", svg_line_width, svg_color, svg_fill_color, "", 0);
980 isao-hara 2 }
981     break;
982 isao-hara 5 case 'image':
983     if(selectedItems[0]){
984     res += setSVGObjectProp(selectedItems[0]);
985     }else{
986 isao-hara 36 res += propSVGImage("", 100, 100, 0);
987 isao-hara 5 }
988     break;
989 isao-hara 2 default:
990     break;
991     }
992     return res;
993     }
994    
995 isao-hara 36 function propSVGText(str, size, color, rot){
996 isao-hara 2 if(!size) size = 24;
997     if(!color) color = '#000000';
998 isao-hara 36 if(!rot) rot = 0;
999 isao-hara 2
1000     var res = "Text:";
1001 isao-hara 28 res += "<input type=\"hidden\" id=\"svg_text\" value=\""+str+"\"/>";
1002     res += "<input type=\"hidden\" id=\"svg_color\" value=\""+color+"\" size=\"8\"/>";
1003 isao-hara 37 res += "Rot:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_rotate\" value=\""+rot+"\" size=\"4\"/>";
1004 isao-hara 36
1005 isao-hara 37 res += "Size:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_size\" value=\""+size+"\" size=\"4\"/>";
1006 isao-hara 25
1007     document.getElementById('toolFill').style.backgroundColor=color;
1008 isao-hara 2 return res;
1009     }
1010    
1011 isao-hara 39 function propSVGObj(type, stroke, color, fill, style, rot){
1012 isao-hara 2 var res = type;
1013     if(!stroke) stroke = 1;
1014     if(!color) color = '#000000';
1015     if(!fill) fill = '#ffffff';
1016 isao-hara 36 if(!rot) rot = 0;
1017 isao-hara 39
1018 isao-hara 28 res += "<input type=\"hidden\" id=\"svg_fill\" value=\""+fill+"\" size=\"8\"/>";
1019     res += "<input type=\"hidden\" id=\"svg_color\" value=\""+color+"\" size=\"8\"/>";
1020 isao-hara 37 res += "Width:<input type=\"text\" id=\"svg_stroke\" onChange=\"updateSVGObj();\" value=\""+stroke+"\" size=\"2\"/>";
1021     res += "Rot:<input type=\"text\" id=\"svg_rotate\" onChange=\"updateSVGObj();\" value=\""+rot+"\" size=\"4\"/>";
1022 isao-hara 25
1023 isao-hara 39 if(style==null) style="";
1024     res += "Type:<input type=\"text\" id=\"svg_stroke_type\" onChange=\"updateSVGObj();\" value=\""+style+"\" size=\"8\"/>";
1025    
1026 isao-hara 25 document.getElementById('toolFill').style.backgroundColor=fill;
1027     document.getElementById('toolStroke').style.backgroundColor=color;
1028 isao-hara 2 return res;
1029     }
1030    
1031 isao-hara 36 function propSVGLine(type, stroke, color, fill, style, rot){
1032 isao-hara 39 var res = propSVGObj(type, stroke, color, fill, style, rot);
1033 isao-hara 36 if(!rot) rot = 0;
1034 isao-hara 39
1035 isao-hara 54 res += "<img id=\"arrow_l\" src=\"images/arrow_l.png\" usemap=\"#arrow_l\" />\n";
1036     res += "<map name=\"arrow_l\">";
1037     res += "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"setLeftArrow();\">";
1038     res += "<area shape=\"rect\" coords=\"0,25,30,50\" onClick=\"removeLeftArrow();\">";
1039     res += "</map>";
1040 isao-hara 25
1041 isao-hara 54 res += "<img id=\"arrow_r\" src=\"images/arrow_r.png\" usemap=\"#arrow_r\" />\n";
1042     res += "<map name=\"arrow_r\">";
1043     res += "<area shape=\"rect\" coords=\"0,0,30,25\" onClick=\"setRightArrow();\">";
1044     res += "<area shape=\"rect\" coords=\"0,25,30,50\" onClick=\"removeRightArrow();\">";
1045     res += "</map>";
1046 isao-hara 52
1047 isao-hara 54 res += "<img id=\"path_type\" src=\"images/path_type.png\" usemap=\"#path_type\" />\n";
1048     res += "<map name=\"path_type\">";
1049     res += "<area shape=\"rect\" coords=\"0,0,30,50\" onClick=\"togglePathType();\">";
1050     res += "</map>";
1051    
1052 isao-hara 52 res += "<img id=\"strokeW\" src=\"images/strokeW.png\" usemap=\"#strokemenu\" />\n";
1053     res += "<map name=\"strokemenu\">";
1054     res += "<area shape=\"rect\" coords=\"0,25,20,200\" onClick=\"upStrokeW();\">";
1055     res += "<area shape=\"rect\" coords=\"50,0,70,175\" onClick=\"downStrokeW();\">";
1056     res += "</map>";
1057    
1058 isao-hara 25 document.getElementById('toolFill').style.backgroundColor=fill;
1059     document.getElementById('toolStroke').style.backgroundColor=color;
1060    
1061 isao-hara 2 return res;
1062     }
1063    
1064 isao-hara 52 function updateStrokeWidth(){
1065     if(selectedItems.length == 1){
1066     var obj = selectedItems[0];
1067 isao-hara 54 if(!obj){ return; }
1068 isao-hara 52 var val = obj.getAttribute('stroke-width');
1069     if(!val) return;
1070     updateStrokeWImg(parseInt(val));
1071     }
1072     }
1073    
1074     function updateStrokeWImg(lw){
1075     var ele = document.getElementById('strokeW');
1076     if(!ele) return;
1077     var v1 = 200-lw*25;
1078     var v2 = v1+25;
1079     var v3 = (lw-1)*25 - 90;
1080     ele.style.clip = "rect("+v1+"px,70px,"+v2+"px,0px)";
1081     ele.style.top = v3+"px";
1082     }
1083    
1084     function upStrokeW(){
1085     if(selectedItems.length == 1){
1086     var obj = selectedItems[0];
1087     var val = obj.getAttribute('stroke-width');
1088     if(!val) return;
1089     var lw = parseInt(val)+1;
1090     obj.setAttribute('stroke-width', lw);
1091     updateStrokeWImg(lw);
1092     }
1093     }
1094    
1095     function downStrokeW(){
1096     if(selectedItems.length == 1){
1097     var obj = selectedItems[0];
1098     var val = obj.getAttribute('stroke-width');
1099     if(!val) return;
1100     var lw = parseInt(val)-1;
1101     obj.setAttribute('stroke-width', lw);
1102     updateStrokeWImg(lw);
1103     }
1104     }
1105    
1106 isao-hara 36 function propSVGImage(str, w, h, rot){
1107 isao-hara 5 var res = "image:";
1108 isao-hara 36 if(!rot) rot = 0;
1109 isao-hara 5 res += "<input type=\"text\" id=\"svg_text\" value=\""+str+"\" />";
1110 isao-hara 51 res += "Width:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_w\" value=\""+w+"\" size=\"4\"/>";
1111     res += "Height:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_h\" value=\""+h+"\" size=\"4\"/>";
1112 isao-hara 37 res += "Rot:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_rotate\" value=\""+rot+"\" size=\"4\"/>";
1113 isao-hara 5 return res;
1114     }
1115    
1116 isao-hara 2 function setSVGMode(m){
1117 isao-hara 51 // var btn = document.getElementById('b_'+modeSVG);
1118     // if(btn){ btn.style.border="1px solid black"; }
1119 isao-hara 14
1120 isao-hara 2 modeSVG = m;
1121     updateShowMenu();
1122 isao-hara 4
1123 isao-hara 51 switch(m){
1124     case 'selector':
1125     targetItem=null;
1126     clearSelectedItems();
1127     selectToolBar(0);
1128     break;
1129     case 'newPath':
1130     selectToolBar(1);
1131     break;
1132     case 'newLine':
1133     selectToolBar(2);
1134     break;
1135     case 'text':
1136     selectToolBar(3);
1137     break;
1138     case 'rect':
1139     selectToolBar(4);
1140     break;
1141     case 'circle':
1142     selectToolBar(5);
1143     break;
1144     case 'ellipse':
1145     selectToolBar(6);
1146     break;
1147     case 'image':
1148     selectToolBar(7);
1149     break;
1150     default:
1151     break;
1152 isao-hara 2 }
1153 isao-hara 51
1154     // var btn = document.getElementById('b_'+modeSVG);
1155     // if(btn){ btn.style.border="3px solid red"; }
1156 isao-hara 2 }
1157    
1158    
1159     function toSVGElement(str, w, h){
1160     var xmlsvg = "xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
1161     var parser = new DOMParser();
1162     var header = "<svg:svg width=\""+w+"\" height=\""+h+"\" "+xmlsvg+" id=\"svg_top\">";
1163     var footer = "</svg:svg>";
1164     var xmlDoc = parser.parseFromString(header+str+footer, "text/xml");
1165     var xmlRoot = xmlDoc.documentElement;
1166     var ele = document.importNode(xmlRoot,true);
1167    
1168     return ele;
1169     }
1170    
1171 isao-hara 47 function newID(){
1172     var id = 'svg_'+nextId;
1173     nextId++;
1174     return id;
1175     }
1176    
1177 isao-hara 2 function createSVGElement(tag, id){
1178 isao-hara 51 if(!id || id=='new') id = newID();
1179 isao-hara 2 var ele= document.createElementNS(svg_ns, tag);
1180     ele.setAttribute("id", id);
1181     return ele;
1182     }
1183    
1184     function defSVGElement(node){
1185     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1186     if(!svg_defs){
1187     svg_defs = createSVGElement('defs', 'svg_defs');
1188     svg_top.insertBefore(svg_defs, svg_top.firstChild);
1189     }
1190     if(node) svg_defs.appendChild(node);
1191     }
1192    
1193     function initSVGElementId(svg_top){
1194     nextId = 1;
1195     checkSVGElementId(svg_top);
1196     appendSVGElementId(svg_top);
1197     }
1198    
1199     function checkSVGElementId(top){
1200     var objs = top.childNodes;
1201    
1202     for(var i=0; i<objs.length ;i++){
1203     if(objs[i].tagName){
1204     var id = objs[i].getAttribute("id");
1205    
1206     if(id && id.match(/svg_[0-9]+/i)){
1207     var n = parseInt(RegExp.lastMatch.substr(4));
1208     if(n >= nextId){
1209     nextId = n+1;
1210     }
1211    
1212     }
1213     checkSVGElementId(objs[i]);
1214     }
1215     }
1216     }
1217    
1218     function appendSVGElementId(top){
1219     var objs = top.childNodes;
1220    
1221     for(var i=0; i<objs.length ;i++){
1222     if(objs[i].tagName){
1223     var id = objs[i].getAttribute("id");
1224     if(!id){
1225     objs[i].setAttribute("id", "svg_"+nextId);
1226     nextId++;
1227     }
1228     }
1229     appendSVGElementId(objs[i]);
1230     }
1231     }
1232    
1233     function setAttributes(obj, attrs){
1234     var attr_array = attrs.split(',');
1235    
1236     for (var i=0; i<attr_array.length;i++){
1237     var x = attr_array[i].split('=');
1238     if(x.length == 2){
1239     obj.setAttribute(x[0], x[1]);
1240     }
1241     }
1242     }
1243    
1244     function createSVGObj(tag, attrs, fill, color, lw){
1245     var ele = createSVGElement(tag, 'new');
1246     setAttributes(ele, attrs);
1247     if (fill) ele.setAttribute('fill', fill);
1248     if (color) ele.setAttribute('stroke', color);
1249     if(lw) ele.setAttribute('stroke-width', lw);
1250    
1251     return ele;
1252     }
1253    
1254     function createSVGText(txt, x, y, size, color){
1255     var ele = createSVGElement('text', 'new');
1256    
1257     ele.setAttribute('x', x);
1258     ele.setAttribute('y', y);
1259     ele.setAttribute('font-size', size);
1260     ele.setAttribute('fill', color);
1261     ele.textContent=txt;
1262    
1263     return ele;
1264     }
1265    
1266     function createSVGImage(fname, width, height, attrs){
1267     var ele = createSVGElement('image', 'new');
1268 isao-hara 7 ele.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', fname);
1269 isao-hara 2 ele.setAttribute('width', width);
1270     ele.setAttribute('height', height);
1271     setAttributes(ele, attrs);
1272    
1273     return ele;
1274     }
1275    
1276     function createSVGMarker(pid, id, child){
1277     var parent = document.getElementById(pid);
1278     if(!parent) return;
1279     var ele = createSVGElement('marker', pid+'_'+id);
1280     ele.appendChild(child);
1281     return ele;
1282     }
1283    
1284     function setLeftArrow(){
1285     if( selectedItems.length == 1 ){
1286     setArrow(selectedItems[0], 'start', '');
1287 isao-hara 36 updateShowMenu();
1288 isao-hara 2 }
1289     }
1290    
1291     function setRightArrow(){
1292     if( selectedItems.length == 1 ){
1293     setArrow(selectedItems[0], 'end', '');
1294 isao-hara 36 updateShowMenu();
1295 isao-hara 2 }
1296     }
1297    
1298     function removeLeftArrow(){
1299     if( selectedItems.length == 1 ){
1300     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1301     var pid = selectedItems[0].getAttribute('id');
1302     var marker = document.getElementById(pid+'_startarrow');
1303     svg_defs.removeChild(marker);
1304     selectedItems[0].removeAttribute('marker-start');
1305 isao-hara 36 updateShowMenu();
1306 isao-hara 2 }
1307     }
1308    
1309     function removeRightArrow(){
1310     if( selectedItems.length == 1 ){
1311     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1312     var pid = selectedItems[0].getAttribute('id');
1313     var marker = document.getElementById(pid+'_endarrow');
1314     svg_defs.removeChild(marker);
1315     selectedItems[0].removeAttribute('marker-end');
1316 isao-hara 54 updateShowMenu();
1317 isao-hara 2 }
1318     }
1319    
1320     function hasArrow(pos){
1321     var pobj = selectedItems[0];
1322     if(!pobj) return false;
1323     var pid = pobj.getAttribute('id');
1324     var marker = document.getElementById(pid+'_'+pos+'arrow');
1325     if(marker) return true;
1326     return false;
1327     }
1328    
1329     function getArrowMarker(obj,pos){
1330     if(!obj) return false;
1331     var pid = obj.getAttribute('id');
1332     var marker = document.getElementById(pid+'_'+pos+'arrow');
1333     return false;
1334     }
1335    
1336     function setArrow(pobj, pos, type){
1337     if(!pobj) return;
1338     var pid = pobj.getAttribute('id');
1339     var marker = document.getElementById(pid+'_'+pos+'arrow');
1340     if(marker) return;
1341    
1342     var obj = createSVGElement('path', 'new');
1343    
1344     var refX = 0;
1345     var refY = 0;
1346    
1347     switch(pos){
1348     case 'start':
1349     refX=10;
1350     refY=10;
1351     setAttributes(obj,'d=M 20 0 L 0 10 20 20 Z');
1352     break;
1353     case 'end':
1354     setAttributes(obj,'d=M 0 0 L 20 10 0 20 Z');
1355     refX= 10;
1356     refY=10;
1357     break;
1358     default:
1359     return;
1360     }
1361    
1362     marker = createSVGMarker(pid, pos+'arrow' , obj);
1363     setAttributes(marker,'markerWidth=10,markerHeight=10,orient=auto,viewBox=0 0 20 20,markerUnits=strokeWidth,refX='+refX+',refY='+refY);
1364    
1365     marker.setAttribute("fill",pobj.getAttribute("stroke"));
1366    
1367     defSVGElement(marker);
1368     var mid = "url(#"+marker.getAttribute("id")+")";
1369     var mattr = "marker-"+pos;
1370     pobj.setAttribute(mattr, mid);
1371     }
1372    
1373 isao-hara 42 function togglePathType(){
1374     if(selectedItems.length != 1) return;
1375     var itm = selectedItems[0];
1376     var path = itm.getAttribute("d").split(' ');
1377     var newpath = "";
1378    
1379     for(var i=0;i<path.length ;i++){
1380 isao-hara 51 if(path[i] == "L"){ path[i] = "C"; }
1381     else if("LCQST".indexOf(path[i])> 0){ path[i] = "L"; }
1382 isao-hara 42 newpath += path[i] + ' ';
1383     }
1384 isao-hara 46 if(((i-4) % 4) != 0) {
1385     newpath += path[i-2] + ' ';
1386     newpath += path[i-1] + ' ';
1387     }
1388 isao-hara 42
1389     itm.setAttribute("d",trim(newpath));
1390 isao-hara 54 updatePathTypeMenu();
1391 isao-hara 42 }
1392    
1393 isao-hara 54 function updatePathTypeMenu(){
1394     if(selectedItems.length != 1) return;
1395     var itm = selectedItems[0];
1396     if(!itm) return;
1397     var path = itm.getAttribute("d");
1398     if(!path) return;
1399     var img = document.getElementById('path_type');
1400    
1401     if(path.indexOf("L") < 0){
1402     selectMenuImage(img, 1);
1403     }else{
1404     selectMenuImage(img, 0);
1405     }
1406     }
1407    
1408     function updateArrowMenu(){
1409     try{
1410     var imgl = document.getElementById('arrow_l');
1411     if(hasArrow('start')){ selectMenuImage(imgl, 1); }else{ selectMenuImage(imgl, 0); }
1412     }catch(e){}
1413    
1414     try{
1415     var imgr = document.getElementById('arrow_r');
1416     if(hasArrow('end')){ selectMenuImage(imgr, 1); }else{ selectMenuImage(imgr, 0); }
1417     }catch(e){}
1418     }
1419    
1420     function selectMenuImage(img, pos){
1421     if(!img) return;
1422     var clip = new Array( "rect(0px,30px,25px,0px)", "rect(25px,30px,50px,0px)");
1423     var top = new Array( "85px", "60px");
1424    
1425     img.style.clip = clip[pos];
1426     img.style.top = top[pos];
1427     }
1428    
1429 isao-hara 44 function scalePath(itm, scale){
1430 isao-hara 52 if(!scale) return;
1431 isao-hara 44 var path = itm.getAttribute("d").split(' ');
1432 isao-hara 46 var bbox = itm.getBBox();
1433 isao-hara 44 var newpath = "";
1434 isao-hara 51 var sX=1;
1435     var sY=1;
1436 isao-hara 46 var isX=true;
1437     var dx = bbox.x - sX*bbox.x;
1438     var dy = bbox.y - sY*bbox.y;
1439 isao-hara 51 if(scale){
1440     sX=scale[0];
1441     sY=scale[1];
1442     }
1443 isao-hara 44
1444     for(var i=0;i<path.length ;i++){
1445     if(path[i].match(/[0-9]+/)){
1446     var val = parseInt(path[i]);
1447 isao-hara 46 if(isX){
1448     path[i] = Math.round(val*sX) + dx ;
1449     isX = false;
1450     }else{
1451     path[i] = Math.round(val*sY) + dy ;
1452     isX = true;
1453     }
1454 isao-hara 44 }
1455     newpath += path[i] + ' ';
1456     }
1457    
1458     itm.setAttribute("d",trim(newpath));
1459     }
1460    
1461     function scaleLine(itm, scale){
1462 isao-hara 52 if(!scale) return;
1463 isao-hara 46 var sX=scale[0];
1464     var sY=scale[1];
1465 isao-hara 44 var x1 = parseInt(itm.getAttribute("x1"));
1466     var y1 = parseInt(itm.getAttribute("y1"));
1467     var x2 = parseInt(itm.getAttribute("x2"));
1468     var y2 = parseInt(itm.getAttribute("y2"));
1469 isao-hara 46 var bbox = itm.getBBox();
1470     var dx = bbox.x - sX*bbox.x;
1471     var dy = bbox.y - sY*bbox.y;
1472 isao-hara 44
1473 isao-hara 46 itm.setAttribute("x1", Math.round(x1*sX)+dx);
1474     itm.setAttribute("y1", Math.round(y1*sY)+dy);
1475     itm.setAttribute("x2", Math.round(x2*sX)+dx);
1476     itm.setAttribute("y2", Math.round(y2*sY)+dy);
1477 isao-hara 44 }
1478    
1479 isao-hara 2 function appendSVGObj(obj){
1480     var svg_top = document.getElementById('svg_top');
1481     if(!svg_top) return;
1482    
1483     svg_top.appendChild(obj);
1484     }
1485    
1486     function isChildById(element, id) {
1487     if (element == null || element.parentNode == null || element.parentNode.nodeName=='BODY') return false;
1488     else if (element.parentNode.id == id) return true;
1489     else return isChildById(element.parentNode, id);
1490     }
1491    
1492     /////////////////////// Formatting SVG DOM
1493     function escapeHTML(text) {
1494     return text.replace( /[<>"&]/g,
1495     function (m) { return { '<': '&lt;', '>': '&gt;', '"': '&quot;', '&': '&amp;' }[m]; }
1496     );
1497     };
1498    
1499     function formatTag(ele){
1500     var str="";
1501     if(ele.nodeType == 1){
1502     var tag_a = ele.tagName.split(':');
1503     var tag;
1504     if(tag_a.length == 1){ tag = "svg:"+tag_a[0]; }else{ tag = ele.tagName; }
1505    
1506     str += "<"+tag;
1507     var attrs = ele.attributes;
1508     for(var i=0; i<attrs.length; i++){
1509     str += " "+attrs[i].nodeName+"=\""+attrs[i].nodeValue+"\"";
1510     }
1511     var cn = ele.childNodes;
1512     if(cn.length > 0){
1513     str +=">\n";
1514     for(var i=0; i<cn.length; i++){
1515 isao-hara 13 var tmp = trim(formatTag(cn[i]));
1516     if(tmp) str += " "+tmp+"\n";
1517 isao-hara 2 }
1518     str += "</"+tag+">";
1519     }else{
1520     str +=" />";
1521     }
1522     return str;
1523     }else if(ele.nodeType==3){
1524     return ele.textContent;
1525     }
1526     }
1527    
1528     function getSVGContent(){
1529     if(!svg_top) return "";
1530    
1531     var str = "";
1532     var elements = svg_top.childNodes;
1533     for(var i=0; i<elements.length; i++){
1534 isao-hara 12 if(elements[i] != svg_select){
1535     var tmp = trim(formatTag(elements[i]));
1536     if(tmp) str += tmp + '\n';
1537     }
1538 isao-hara 2 }
1539     return str;
1540     }
1541    
1542     function trim(str){
1543     return str.replace(/(^\s+)|(\s+$)/g, "");
1544     }
1545    
1546     /////// Access Server
1547     function newXMLRequest(){
1548     if(this.XMLHttpRequest){
1549     return new XMLHttpRequest();
1550     }else {
1551     return new ActiveXObject("Microsoft.XMLHTTP");
1552     }
1553     }
1554    
1555     function createRequestData(data){
1556     var str="filetype=svg";
1557     for (var i in data){
1558     str = str +"&"+ i +"="+encodeURIComponent(data[i]);
1559     }
1560     return str;
1561     }
1562    
1563     function postRequest(url, data, func){
1564     var postData=createRequestData(data);
1565     var obj=newXMLRequest();
1566    
1567     obj.onreadystatechange = function(){
1568     if (obj.readyState == 4 && obj.status == 200){
1569     func(obj.responseText);
1570     }
1571     }
1572     obj.open("POST", url, true);
1573     obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
1574     obj.send(postData);
1575     }
1576    
1577     function commnadFinishAlert(s){
1578     alert(s);
1579     }
1580    
1581     function uploadFileData(transaction, results){
1582     var data = results.rows.item(0);
1583     var updata = new Array(0);
1584     updata['name']= data['name'];
1585     updata['datalob'] = data['datablob'];
1586     updata['cmd'] = 'upload'
1587    
1588     postRequest(MgrPath+iSlideMgr, updata, commnadFinishAlert);
1589     }
1590    
1591     function uploadFile(name){
1592     getFile(name, 'uploadFileData');
1593     }
1594    
1595     function getRemoteFileList(){
1596     var data=new Array();
1597     data['name'] = "SVG";
1598     data['cmd'] = "list";
1599     postRequest(MgrPath+iSlideMgr, data, fileSelectorOnServer);
1600     }
1601    
1602     function previewRemoteFile(content){
1603     previewFile(content);
1604     setMode('Preview');
1605     }
1606    
1607     function getRemoteFile(name){
1608     var data=new Array();
1609     data['name'] = name;
1610     data['cmd'] = "get";
1611     postRequest(MgrPath+iSlideMgr, data, previewRemoteFile);
1612     }
1613    
1614 isao-hara 16 function saveRemoteFile(content){
1615 isao-hara 17 var datadiv = document.getElementById('tempdata');
1616     var fname = datadiv.getAttribute('lfname');
1617     saveContentWithFilename(fname, content);
1618 isao-hara 16 }
1619    
1620     function downloadFile(name){
1621     var data=new Array();
1622     data['name'] = name;
1623     data['cmd'] = "get";
1624     var datadiv = document.getElementById('tempdata');
1625     datadiv.setAttribute('lfname', name);
1626     postRequest(MgrPath+iSlideMgr, data, saveRemoteFile);
1627     }
1628    
1629 isao-hara 2 function showRemoteFile(name){
1630     getRemoteFile(name);
1631     }
1632    
1633 isao-hara 16 function downloadToFile(name){
1634     var downloadForm = "";
1635     downloadForm += "<form action=\"iSlideManager.php\" method=\"post\">";
1636     downloadForm += "<input type=\"hidden\" name=\"cmd\" value=\"download\">";
1637     downloadForm += "<input type=\"hidden\" name=\"name\" value=\""+name+"\">";
1638     downloadForm += "<input type=\"hidden\" name=\"filetype\" value=\"svg\">";
1639     downloadForm += "<input type=\"submit\">";
1640     downloadForm += "</form>";
1641    
1642     var cmdForm = document.getElementById('cmdForm');
1643     cmdForm.innerHTML = downloadForm;
1644     cmdForm.firstChild.submit();
1645     }
1646    
1647 isao-hara 2 //////// Event Handler
1648 isao-hara 51 function selectedRectangle(x, y, ex, ey){
1649     if(!svg_select) return null;
1650    
1651     svg_select.setAttribute("x",x);
1652     svg_select.setAttribute("y",y);
1653     svg_select.setAttribute("width", ex-x);
1654     svg_select.setAttribute("height", ey-y);
1655     svg_select.setAttribute("visibility", "visible");
1656     return svg_select;
1657     }
1658    
1659     function hideSelectedRectangle(){
1660     svg_select.setAttribute("x",0);
1661     svg_select.setAttribute("y",0);
1662     svg_select.setAttribute("width",0);
1663     svg_select.setAttribute("height",0);
1664     svg_select.setAttribute("visibility","hidden");
1665     }
1666    
1667 isao-hara 2 function setInnerHTML(id, val){
1668     var itm=document.getElementById(id);
1669     if(itm) itm.innerHTML=val;
1670     }
1671    
1672 isao-hara 41 function toggleRotateLock(){
1673 isao-hara 51 var lock = document.getElementById('rot-lock');
1674     var unlock = document.getElementById('rot-unlock');
1675     if(svg_rotate_locked){
1676     svg_rotate_locked = false;
1677     lock.style.display='none';
1678     unlock.style.display='block';
1679     }else{
1680     svg_rotate_locked = true;
1681     lock.style.display='block';
1682     unlock.style.display='none';
1683     }
1684 isao-hara 42 }
1685    
1686     function toggleScaleLock(){
1687 isao-hara 51 var lock = document.getElementById('scale-lock');
1688     var unlock = document.getElementById('scale-unlock');
1689     if(svg_scale_locked){
1690     svg_scale_locked = false;
1691     lock.style.display='none';
1692     unlock.style.display='block';
1693     }else{
1694     svg_scale_locked = true;
1695     lock.style.display='block';
1696     unlock.style.display='none';
1697     }
1698 isao-hara 41 }
1699    
1700 isao-hara 2 function popupInfo(val){
1701     var str="<button onClick=\"hideItemById('popup');\">Close</button><hr> ";
1702     str += val;
1703     setInnerHTML('popup', str);
1704     showItemById('popup');
1705     }
1706    
1707     function addAttributeVal(obj, itm, dv){
1708     if(obj.hasAttribute(itm)){
1709     var x = parseInt(obj.getAttribute(itm));
1710     x = x + dv;
1711     obj.setAttribute(itm, x)
1712     }
1713     }
1714    
1715     function showSVGSource(){
1716     var str = getSVGContent();
1717 isao-hara 14 var escstr="<button onClick=\"hideItemById('popup');\">Close</button>";
1718     escstr+="<button onClick=\"saveContent(document.getElementById('ContentView').value); previewData(document.getElementById('ContentView').value);\">Save</button><hr> ";
1719 isao-hara 51 escstr += "<textarea cols=\"100\" rows=\"30\" id=\"ContentView\">"+str+"</textarea>";
1720 isao-hara 2 setInnerHTML('popup', escstr);
1721     showItemById('popup');
1722     }
1723    
1724    
1725     function getElementTag(obj){
1726 isao-hara 6 if (!obj) return null;
1727 isao-hara 2 var tag = obj.tagName.split(':');
1728     if(tag.length == 2 && tag[0]=='svg') return tag[1];
1729     return tag[0];
1730     }
1731    
1732     function setSVGObjectProp(obj){
1733     var tag = getElementTag(obj);
1734     var res ="";
1735     switch(tag){
1736     case 'text':
1737 isao-hara 36 res = propSVGText(obj.textContent, obj.getAttribute("font-size"), obj.getAttribute("fill"),getRotateAngle(obj));
1738 isao-hara 2 break;
1739     case 'rect':
1740     case 'circle':
1741     case 'ellipse':
1742 isao-hara 39 res = propSVGObj(tag+":", obj.getAttribute("stroke-width"), obj.getAttribute("stroke"),
1743     obj.getAttribute("fill"),obj.getAttribute("stroke-dasharray"),getRotateAngle(obj));
1744 isao-hara 2 break;
1745     case 'path':
1746     case 'line':
1747     res = propSVGLine(tag+":",obj.getAttribute("stroke-width"),
1748 isao-hara 36 obj.getAttribute("stroke"),obj.getAttribute("fill"), obj.getAttribute("stroke-dasharray"),getRotateAngle(obj));
1749 isao-hara 2 break;
1750 isao-hara 5 case 'image':
1751 isao-hara 36 res = propSVGImage(obj.getAttribute("xlink:href"),obj.getAttribute("width"),obj.getAttribute("height"),getRotateAngle(obj));
1752 isao-hara 5 break;
1753 isao-hara 2 default:
1754     break;
1755     }
1756    
1757     return res
1758     }
1759    
1760     function updateSVGObj(){
1761     if(selectedItems.length != 1) return;
1762     var obj = selectedItems[0];
1763     var tag = getElementTag(obj);
1764 isao-hara 36 var deg=document.getElementById('svg_rotate');
1765 isao-hara 2 var res ="";
1766     switch(tag){
1767     case 'text':
1768     var txt=document.getElementById('svg_text');
1769     var color=document.getElementById('svg_color');
1770     var size=document.getElementById('svg_size');
1771     obj.textContent = txt.value;
1772     obj.setAttribute("fill", color.value);
1773     obj.setAttribute("font-size", size.value);
1774 isao-hara 36 setRotate(obj,deg.value);
1775 isao-hara 2 break;
1776     case 'rect':
1777     case 'circle':
1778     case 'ellipse':
1779     var fill=document.getElementById('svg_fill');
1780     var color=document.getElementById('svg_color');
1781     var L=document.getElementById('svg_stroke');
1782 isao-hara 39 var dash=document.getElementById('svg_stroke_type');
1783 isao-hara 2 obj.setAttribute("fill", fill.value);
1784     obj.setAttribute("stroke", color.value);
1785     obj.setAttribute("stroke-width", L.value);
1786 isao-hara 39 if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1787 isao-hara 36 setRotate(obj,deg.value);
1788 isao-hara 2 break;
1789     case 'path':
1790     case 'line':
1791     var fill=document.getElementById('svg_fill');
1792     var color=document.getElementById('svg_color');
1793     var L=document.getElementById('svg_stroke');
1794     var dash=document.getElementById('svg_stroke_type');
1795     obj.setAttribute("fill", fill.value);
1796     obj.setAttribute("stroke", color.value);
1797     obj.setAttribute("stroke-width", L.value);
1798 isao-hara 39 if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1799 isao-hara 36 setRotate(obj,deg.value);
1800 isao-hara 39
1801 isao-hara 2 var id = obj.getAttribute("id");
1802     var marker = document.getElementById(id+'_startarrow');
1803     if(marker) marker.setAttribute("fill", color.value);
1804     var marker = document.getElementById(id+'_endarrow');
1805     if(marker) marker.setAttribute("fill", color.value);
1806     break;
1807 isao-hara 5 case 'image':
1808     var fname=document.getElementById('svg_text');
1809     var w=document.getElementById('svg_w');
1810     var h=document.getElementById('svg_h');
1811 isao-hara 6 obj.setAttribute("xlink:href", fname.value);
1812 isao-hara 5 obj.setAttribute("width", w.value);
1813     obj.setAttribute("height", h.value);
1814 isao-hara 36 setRotate(obj,deg.value);
1815 isao-hara 5 break;
1816 isao-hara 2 default:
1817     break;
1818     }
1819    
1820     }
1821    
1822     function delSVGObj(){
1823     if(!svg_top) return;
1824 isao-hara 43 for(var i=0; i< selectedItems.length; i++){
1825     var obj = selectedItems[i];
1826     svg_top.removeChild(obj);
1827     }
1828 isao-hara 51 hideSelectedRectangle();
1829 isao-hara 2 }
1830    
1831     ///// For MobileSafari
1832     function getPreviewX(x){ return x - preview.offsetLeft; }
1833     function getPreviewY(y){ return y - preview.offsetTop; }
1834    
1835 isao-hara 51 function calcDict(x1, y1, x2, y2){
1836 isao-hara 2 return (x1-x2)*(x1-x2) +(y1-y2)*(y1-y2);
1837     }
1838    
1839     function isIncludeEllipse( x1, y1, cx, cy, rx, ry){
1840     return ((x1-cx)*(x1-cx)/rx/rx +(y1-cy)*(y1-cy)/ry/ry) < 1;
1841     }
1842    
1843     function getAttributeVal(obj, name){
1844     try{
1845     return parseInt(obj.getAttribute(name));
1846     }catch(e){ return 0; }
1847     }
1848    
1849     function checkIntersection(obj, x, y, ex, ey){
1850 isao-hara 18 if(!obj) return false;
1851 isao-hara 2 var res = true;
1852     var bbox = obj.getBBox();
1853     var ox = bbox.x;
1854     var oy = bbox.y;
1855     var oex = bbox.x+bbox.width;
1856     var oey = bbox.y+bbox.height;
1857     if( ex < ox || oex < x || ey < oy || oey < y) res = false;
1858    
1859     var tag = getElementTag(obj);
1860     switch(tag){
1861     case 'circle':
1862     case 'ellipse':
1863     var cx = getAttributeVal(obj,"cx");
1864     var cy = getAttributeVal(obj,"cy");
1865     var rx = getAttributeVal(obj,"r");
1866     var ry = rx;
1867     if(!rx){
1868     var rx = getAttributeVal(obj,"rx");
1869     var ry = getAttributeVal(obj,"ry");
1870     }
1871    
1872     if(res){
1873     if(cx <x && cy < y && !isIncludeEllipse(x,y,cx,cy,rx,ry)) res=false;
1874     else if(cx < x && cy > ey && !isIncludeEllipse(x,ey,cx,cy,rx,ry)) res=false;
1875     else if(cx > ex && cy > ey && !isIncludeEllipse(ex,ey,cx,cy,rx,ry)) res=false;
1876     else if(cx > ex && cy < y && !isIncludeEllipse( ex,y,cx,cy,rx,ry)) res=false;
1877     }
1878     break;
1879     case 'path':
1880     var d = obj.getAttribute("d");
1881     var p = getPoints(d);
1882     for(var i=0;i<p.length;i++){
1883     var ox=p[i][0];
1884     var oy=p[i][1];
1885     if(x < ox && ox < ex && y < oy && oy < ey) {
1886     return true;
1887     }
1888     }
1889     return false;
1890     break;
1891     case 'line':
1892     var x1 = getAttributeVal(obj,"x1");
1893     var y1 = getAttributeVal(obj,"y1");
1894     var x2 = getAttributeVal(obj,"x2");
1895     var y2 = getAttributeVal(obj,"y2");
1896 isao-hara 3 var d = (y2-y1)/(x2-x1);
1897    
1898     if(res){
1899     var xx = Math.abs(x2-x1);
1900     var sign = 1;
1901     if(x2-x1 < 0){ sign = -1; }
1902     for(var i=0; i < xx; i++){
1903     var nx = i*sign + x1;
1904     var ny = d * i*sign + y1;
1905     if(x < nx && nx < ex && y < ny && ny < ey) return true;
1906     }
1907     }
1908 isao-hara 2 return false;
1909    
1910 isao-hara 3 return res;
1911 isao-hara 2 break;
1912     case 'text':
1913     case 'rect':
1914     case 'polygon':
1915     case 'polyline':
1916     case 'image':
1917     break;
1918     default:
1919     res=false;
1920     break;
1921     }
1922     return res;
1923     }
1924    
1925     function getBoundingBox(obj){
1926     var res = new Array(4);
1927     var bbox = obj.getBBox();
1928     res[0] = bbox.x-1;
1929     res[1] = bbox.y-1;
1930     res[2] = bbox.x+bbox.width+2;
1931     res[3] = bbox.y+bbox.height+2;
1932     return res;
1933     }
1934    
1935     function setSelectBox(){
1936     if(!svg_select) return;
1937    
1938     if(selectedItems.length == 0){
1939 isao-hara 51 hideSelectedRectangle()
1940 isao-hara 2 return;
1941     }
1942    
1943 isao-hara 51 var bbox = new Array(1000,1000,0,0);
1944 isao-hara 2
1945     for(var i=0;i<selectedItems.length;i++){
1946     var bp = getBoundingBox(selectedItems[i]);
1947     if(bp[0] < bbox[0]) bbox[0]=bp[0];
1948     if(bp[1] < bbox[1]) bbox[1]=bp[1];
1949     if(bp[2] > bbox[2]) bbox[2]=bp[2];
1950     if(bp[3] > bbox[3]) bbox[3]=bp[3];
1951     }
1952 isao-hara 51 selectedRectangle(bbox[0], bbox[1], bbox[2], bbox[3]);
1953 isao-hara 2 }
1954    
1955     function getSelectedObjects(x1, y1, x2, y2){
1956     if(x1 > x2) { var tmp = x1; x1=x2; x2=tmp; }
1957     if(y1 > y2) { var tmp = y1; y1=y2; y2=tmp; }
1958    
1959 isao-hara 3 var val="";
1960 isao-hara 2 if(svg_top){
1961     var val ="";
1962     var objs = svg_top.childNodes;
1963     selectedItems = new Array();
1964     for(var i=0; i<objs.length;i++){
1965     if(objs[i].tagName){
1966 isao-hara 3
1967 isao-hara 2 if(objs[i] != svg_select && checkIntersection(objs[i], x1, y1, x2, y2)){
1968     selectedItems.push(objs[i]);
1969     }
1970 isao-hara 3 val += objs[i].tagName+" ";
1971 isao-hara 2 }
1972     }
1973     }
1974     setSelectBox();
1975     }
1976    
1977 isao-hara 42 function dupObject(){
1978     if(selectedItems.length == 0){ return; }
1979     dupItems = selectedItems;
1980     dupX = parseInt(svg_select.getAttribute("x"));
1981     dupY = parseInt(svg_select.getAttribute("y"));
1982     setSVGMode('Duplicate');
1983     }
1984    
1985     function pasteObject(x,y){
1986     if(selectedItems.length == 0){ return; }
1987     for(var i=0;i<dupItems.length;i++){
1988     var itm = dupItems[i].cloneNode(true);
1989 isao-hara 47 itm.setAttribute("id", newID());
1990 isao-hara 42 replaceTranslate(itm,x-dupX,y-dupY);
1991     updateTransform(itm);
1992     appendSVGObj(itm);
1993     }
1994     }
1995    
1996 isao-hara 19 function onTouchStartCore(){
1997 isao-hara 35 if((!modeSVG || modeSVG == 'selector') && selectedItems.length == 0){ // Selector Mode
1998     var x1=getPreviewX(sx-1);
1999     var y1=getPreviewY(sy-1);
2000     var x2=getPreviewX(sx+2);
2001     var y2=getPreviewY(sy+2);
2002     getSelectedObjects(x1, y1, x2, y2);
2003    
2004     if(selectedItems.length == 0){
2005     setSVGMode('selector');
2006     }else if(selectedItems.length == 1){
2007     targetItem=selectedItems[0];
2008     setSVGMode(getElementTag(targetItem));
2009     }else{
2010     setSVGMode('Group');
2011     }
2012     }else { // CreateMode
2013     if(selectedItems.length == 0){
2014 isao-hara 2
2015 isao-hara 35 var fill=document.getElementById('svg_fill');
2016     var color=document.getElementById('svg_color');
2017     var L=document.getElementById('svg_stroke');
2018 isao-hara 2
2019 isao-hara 35 var x = getPreviewX(sx);
2020     var y = getPreviewY(sy);
2021 isao-hara 2
2022 isao-hara 35 switch(modeSVG){
2023     case 'text':
2024     var txt=document.getElementById('svg_text');
2025     var size=document.getElementById('svg_size');
2026     if(txt.value){
2027     y = y + parseInt(size.value)*0.8;
2028     targetItem=createSVGText(txt.value, x, y, size.value, color.value);
2029     }else{
2030     putInputForm(x, y, txt.value, size.value);
2031     }
2032     break;
2033     case 'rect':
2034     var attr = 'x='+x+',y='+y+',width='+svg_width+',height='+svg_height;
2035     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2036     break;
2037     case 'circle':
2038     var attr = 'cx='+x+',cy='+y+',r='+svg_rx;
2039     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2040     break;
2041     case 'ellipse':
2042     var attr = 'cx='+x+',cy='+y+',rx='+svg_rx+',ry='+svg_ry;
2043     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2044     break;
2045     case 'newPath':
2046 isao-hara 51 var attr = 'd=M '+x+' '+y+' L '+x+' '+y;
2047 isao-hara 35 targetItem=createSVGObj('path' ,attr, 'none', color.value, L.value);
2048     break;
2049     case 'newLine':
2050     var x2=x+1;
2051     var attr = 'x1='+x+',y1='+y+',x2='+x2+',y2='+y;
2052     targetItem=createSVGObj('line' ,attr, 'none', color.value, L.value);
2053     break;
2054     case 'image':
2055     var attr = 'x='+x+',y='+y;
2056     var txt=document.getElementById('svg_text');
2057     var w=document.getElementById('svg_w');
2058     var h=document.getElementById('svg_h');
2059     if(txt.value) targetItem=createSVGImage(txt.value ,w.value, h.value, attr);
2060     default:
2061     break;
2062 isao-hara 2 }
2063 isao-hara 35 if (targetItem){
2064     appendSVGObj(targetItem);
2065     selectedItems[0]=targetItem;
2066     }
2067     }else{
2068     var x1=getPreviewX(sx-1);
2069     var y1=getPreviewY(sy-1);
2070     var x2=getPreviewX(sx+2);
2071     var y2=getPreviewY(sy+2);
2072 isao-hara 42
2073     if(modeSVG == 'Duplicate'){ pasteObject(x1,y1); }
2074     if(modeSVG == 'newPath'){
2075     if(targetItem.tagName == 'path' ){
2076     var path = targetItem.getAttribute("d");
2077     path = path + ' '+ getPreviewX(sx) + ' '+ getPreviewY(sy) ;
2078     targetItem.setAttribute("d",path);
2079     }
2080     return;
2081     }
2082 isao-hara 35 if(!checkIntersection(svg_select, x1, y1, x2, y2)){
2083     setSVGMode('selector');
2084     }
2085 isao-hara 2 }
2086 isao-hara 35 }
2087 isao-hara 2 }
2088    
2089 isao-hara 25
2090     function onDoubleTap(e){
2091     if(selectedItems.length == 1 ){
2092 isao-hara 51 hideSelectedRectangle();
2093 isao-hara 29
2094 isao-hara 25 var obj = selectedItems[0];
2095 isao-hara 51 if(!obj) return;
2096 isao-hara 25 switch(obj.tagName){
2097     case 'svg:text':
2098 isao-hara 29 case 'text':
2099 isao-hara 25 var txt = trim(obj.textContent);
2100     var size = parseInt(obj.getAttribute("font-size"));
2101     var x = parseInt(obj.getAttribute("x"));
2102     var y = parseInt(obj.getAttribute("y"));
2103     x = x-20;
2104     y = y-size*0.8 -10;
2105     putInputForm(x, y, txt, size, obj.id);
2106     obj.style.display = 'none';
2107     editingTextObj = obj;
2108     break;
2109 isao-hara 42 case 'path':
2110     if(modeSVG == 'newPath'){ setSVGMode('selector'); }
2111     break;
2112 isao-hara 25 default:
2113     break;
2114     }
2115 isao-hara 29 return false;
2116 isao-hara 25 }
2117     }
2118    
2119 isao-hara 2 function getPoints(d){
2120     var p = d.split(' ');
2121     var res = new Array();
2122     var isx=true;
2123     var x, y;
2124     for(var i=0; i<p.length;i++){
2125     if(p[i].match('[MLHVCSQTA]','i')){
2126     }else{
2127     if(isx){
2128     x = parseInt(p[i]);
2129     }else{
2130     y = parseInt(p[i]);
2131     res.push(new Array(x, y));
2132     }
2133     isx = !isx;
2134     }
2135     }
2136     return res;
2137     }
2138    
2139     function updatePath(d, x, y){
2140     var p = d.split(' ');
2141     var res = "";
2142     var isx=true;
2143     var val;
2144     for(var i=0; i<p.length;i++){
2145 isao-hara 42 if(trim(p[i]) == "") continue;
2146 isao-hara 2 if(p[i].match('[MLHVCSQTA]','i')){
2147     res += ' '+p[i];
2148     }else{
2149     if(isx){
2150     val = parseInt(p[i])+x;
2151     }else{
2152     val = parseInt(p[i])+y;
2153     }
2154    
2155     res += ' '+ val;
2156     isx = !isx;
2157     }
2158     }
2159     return trim(res);
2160     }
2161    
2162     function updateTransform(obj){
2163     try{
2164     var trans = obj.getAttribute("transform");
2165     if(!trans) return;
2166     if(trans.match(new RegExp("translate(.+,.+)","i"))){
2167     var str = RegExp.lastMatch;
2168     var vals = str.substr(10,str.length-11).split(',') ;
2169     var dx = parseInt(vals[0]);
2170     var dy = parseInt(vals[1]);
2171     switch(getElementTag(obj)){
2172     case 'text':
2173     case 'rect':
2174 isao-hara 6 case 'image':
2175 isao-hara 2 addAttributeVal(obj, "x", dx);
2176     addAttributeVal(obj, "y", dy);
2177     break;
2178     case 'circle':
2179     case 'ellipse':
2180     addAttributeVal(obj, "cx", dx);
2181     addAttributeVal(obj, "cy", dy);
2182     break;
2183    
2184     case 'path':
2185     var path = obj.getAttribute("d");
2186     obj.setAttribute("d", updatePath(path, dx, dy));
2187     break;
2188     case 'line':
2189     var x1 = getAttributeVal(obj,"x1");
2190     var y1 = getAttributeVal(obj,"y1");
2191     var x2 = getAttributeVal(obj,"x2");
2192     var y2 = getAttributeVal(obj,"y2");
2193     obj.setAttribute("x1", x1+dx);
2194     obj.setAttribute("y1", y1+dy);
2195     obj.setAttribute("x2", x2+dx);
2196     obj.setAttribute("y2", y2+dy);
2197    
2198     break;
2199     default:
2200     break;
2201     }
2202 isao-hara 36 replaceTranslate(obj, 0, 0);
2203     updateRotate(obj);
2204 isao-hara 2 }
2205     }catch (e){
2206     }
2207     }
2208    
2209     function moveSelectedRectangle(dx, dy){
2210     if(!svg_select) return;
2211     svg_select.setAttribute("transform","translate("+dx+","+dy+")");
2212     }
2213    
2214     function updateSelectedRectangle(){
2215     if(!svg_select) return;
2216     if(selectedItems.length > 0){
2217 isao-hara 29 svg_select.setAttribute("visibility","visible");
2218 isao-hara 2 }else{
2219 isao-hara 51 hideSelectedRectangle();
2220 isao-hara 2 }
2221     }
2222    
2223     function clearSelectedItems(){
2224     for(i in selectedItems) delete selectedItems[i];
2225     }
2226    
2227 isao-hara 36
2228     function replaceTranslate(obj, dx, dy){
2229     var trans = obj.getAttribute("transform");
2230 isao-hara 38
2231 isao-hara 36 if(trans && trans.indexOf("translate")>=0){
2232     var strs = trans.split(' ');
2233     trans = "";
2234     for(var i=0; i<strs.length; i++){
2235 isao-hara 38 if(strs[i].indexOf("translate")>=0){
2236     if (dx == 0 && dy == 0){ strs[i]=""; }else{ strs[i] = "translate("+dx+","+dy+")"; }
2237     }
2238    
2239 isao-hara 36 trans += strs[i]+" ";
2240     }
2241 isao-hara 38 if(trim(trans)){ obj.setAttribute("transform", trim(trans));}
2242     else obj.removeAttribute("transform");
2243 isao-hara 36 }else{
2244     if(trans){
2245     trans += " translate("+dx+","+dy+")";
2246     obj.setAttribute("transform", trans);
2247     }else{
2248     obj.setAttribute("transform","translate("+dx+","+dy+")");
2249     }
2250     }
2251 isao-hara 38 updateRotate(obj,dx,dy);
2252 isao-hara 36 }
2253    
2254     function updateRotate(obj,dx,dy){
2255     var trans = obj.getAttribute("transform");
2256    
2257     if(!trans || trans.indexOf("rotate") < 0) return;
2258     var bbox = obj.getBBox();
2259     var x = bbox.x+bbox.width/2;
2260     var y = bbox.y+bbox.height/2;
2261     var strs = trans.split(' ');
2262     trans = "";
2263    
2264     for(var i=0; i<strs.length ;i++){
2265     if(strs[i].indexOf("rotate") >= 0){
2266     var deg = strs[i].substr(7, strs[i].indexOf(",")-7);
2267     if(dx) x += dx;
2268     if(dy) y += dy;
2269     strs[i] = "rotate("+deg+","+x+","+y+")";
2270     }
2271     trans += strs[i]+" ";
2272     }
2273     obj.setAttribute("transform",trim(trans));
2274     }
2275    
2276     function getRotateAngle(obj){
2277     var trans = obj.getAttribute("transform");
2278     if(!trans || trans.indexOf("rotate") < 0) return 0;
2279     var strs = trans.split(' ');
2280     for(var i=0; i<strs.length ;i++){
2281     if(strs[i].indexOf("rotate") >= 0){
2282     var deg = strs[i].substr(7, strs[i].indexOf(",")-7);
2283     return parseInt(deg);
2284     }
2285     }
2286     return 0;
2287     }
2288    
2289     function setRotate(obj,deg){
2290     var trans = obj.getAttribute("transform");
2291     var bbox = obj.getBBox();
2292     var x = bbox.x+bbox.width/2;
2293     var y = bbox.y+bbox.height/2;
2294    
2295     if(!trans) trans="";
2296     if(trans.indexOf("rotate") < 0){
2297     trans = "rotate("+deg+","+x+","+y+")";
2298     obj.setAttribute("transform", trans);
2299     }else{
2300     var strs = trans.split(' ');
2301     trans = "";
2302     for(var i=0; i<strs.length ;i++){
2303     if(strs[i].indexOf("rotate") >= 0){ strs[i] = "rotate("+deg+","+x+","+y+")"; }
2304     trans += strs[i]+" ";
2305     }
2306     obj.setAttribute("transform",trim(trans));
2307     }
2308     }
2309    
2310 isao-hara 44 function getScale(obj){
2311     var trans = obj.getAttribute("transform");
2312 isao-hara 46 if(!trans || trans.indexOf("scale") < 0) return null;
2313 isao-hara 44 var strs = trans.split(' ');
2314     for(var i=0; i<strs.length ;i++){
2315     if(strs[i].indexOf("scale") >= 0){
2316 isao-hara 46 var degs = strs[i].substr(6, strs[i].indexOf(")")-6);
2317     var degs = degs.split(',');
2318     return new Array(parseFloat(degs[0]), parseFloat(degs[1]));
2319 isao-hara 44 }
2320     }
2321 isao-hara 46 return null;
2322 isao-hara 44 }
2323    
2324 isao-hara 46 function setScale(obj,scaleX, scaleY){
2325 isao-hara 44 var trans = obj.getAttribute("transform");
2326 isao-hara 46 var x = parseInt(svg_select.