Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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