Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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