Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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