Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 58 - (hide annotations) (download) (as text)
Thu Jul 29 05:10:06 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 86555 byte(s)
dasharray and stroke-width menu chenage
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     if(hasArrow('start')){ selectMenuImage(imgl, 0); }else{ selectMenuImage(imgl, 1); }
1263     }catch(e){}
1264    
1265     try{
1266     var imgr = document.getElementById('arrow_r');
1267     if(hasArrow('end')){ selectMenuImage(imgr, 0); }else{ selectMenuImage(imgr, 1); }
1268     }catch(e){}
1269     }
1270    
1271     function selectMenuImage(img, pos){
1272     if(!img) return;
1273     var clip = new Array( "rect(25px,30px,50px,0px)","rect(0px,30px,25px,0px)");
1274     var top = new Array( "56px", "81px");
1275    
1276     img.style.clip = clip[pos];
1277     img.style.top = top[pos];
1278     }
1279    
1280 isao-hara 36 function propSVGImage(str, w, h, rot){
1281 isao-hara 5 var res = "image:";
1282 isao-hara 36 if(!rot) rot = 0;
1283 isao-hara 5 res += "<input type=\"text\" id=\"svg_text\" value=\""+str+"\" />";
1284 isao-hara 51 res += "Width:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_w\" value=\""+w+"\" size=\"4\"/>";
1285     res += "Height:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_h\" value=\""+h+"\" size=\"4\"/>";
1286 isao-hara 37 res += "Rot:<input type=\"text\" onChange=\"updateSVGObj();\" id=\"svg_rotate\" value=\""+rot+"\" size=\"4\"/>";
1287 isao-hara 5 return res;
1288     }
1289    
1290 isao-hara 2 function setSVGMode(m){
1291     modeSVG = m;
1292     updateShowMenu();
1293 isao-hara 4
1294 isao-hara 51 switch(m){
1295     case 'selector':
1296     targetItem=null;
1297     clearSelectedItems();
1298     selectToolBar(0);
1299     break;
1300     case 'newPath':
1301     selectToolBar(1);
1302     break;
1303     case 'newLine':
1304     selectToolBar(2);
1305     break;
1306     case 'text':
1307     selectToolBar(3);
1308     break;
1309     case 'rect':
1310     selectToolBar(4);
1311     break;
1312     case 'circle':
1313     selectToolBar(5);
1314     break;
1315     case 'ellipse':
1316     selectToolBar(6);
1317     break;
1318     case 'image':
1319     selectToolBar(7);
1320     break;
1321     default:
1322     break;
1323 isao-hara 2 }
1324     }
1325    
1326    
1327     function toSVGElement(str, w, h){
1328     var xmlsvg = "xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"";
1329     var parser = new DOMParser();
1330     var header = "<svg:svg width=\""+w+"\" height=\""+h+"\" "+xmlsvg+" id=\"svg_top\">";
1331     var footer = "</svg:svg>";
1332     var xmlDoc = parser.parseFromString(header+str+footer, "text/xml");
1333     var xmlRoot = xmlDoc.documentElement;
1334     var ele = document.importNode(xmlRoot,true);
1335    
1336     return ele;
1337     }
1338    
1339 isao-hara 47 function newID(){
1340     var id = 'svg_'+nextId;
1341     nextId++;
1342     return id;
1343     }
1344    
1345 isao-hara 2 function createSVGElement(tag, id){
1346 isao-hara 51 if(!id || id=='new') id = newID();
1347 isao-hara 2 var ele= document.createElementNS(svg_ns, tag);
1348     ele.setAttribute("id", id);
1349     return ele;
1350     }
1351    
1352     function defSVGElement(node){
1353     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1354     if(!svg_defs){
1355     svg_defs = createSVGElement('defs', 'svg_defs');
1356     svg_top.insertBefore(svg_defs, svg_top.firstChild);
1357     }
1358     if(node) svg_defs.appendChild(node);
1359     }
1360    
1361     function initSVGElementId(svg_top){
1362     nextId = 1;
1363     checkSVGElementId(svg_top);
1364     appendSVGElementId(svg_top);
1365     }
1366    
1367     function checkSVGElementId(top){
1368     var objs = top.childNodes;
1369    
1370     for(var i=0; i<objs.length ;i++){
1371     if(objs[i].tagName){
1372     var id = objs[i].getAttribute("id");
1373    
1374     if(id && id.match(/svg_[0-9]+/i)){
1375     var n = parseInt(RegExp.lastMatch.substr(4));
1376     if(n >= nextId){
1377     nextId = n+1;
1378     }
1379    
1380     }
1381     checkSVGElementId(objs[i]);
1382     }
1383     }
1384     }
1385    
1386     function appendSVGElementId(top){
1387     var objs = top.childNodes;
1388    
1389     for(var i=0; i<objs.length ;i++){
1390     if(objs[i].tagName){
1391     var id = objs[i].getAttribute("id");
1392     if(!id){
1393     objs[i].setAttribute("id", "svg_"+nextId);
1394     nextId++;
1395     }
1396     }
1397     appendSVGElementId(objs[i]);
1398     }
1399     }
1400    
1401     function setAttributes(obj, attrs){
1402     var attr_array = attrs.split(',');
1403    
1404     for (var i=0; i<attr_array.length;i++){
1405     var x = attr_array[i].split('=');
1406     if(x.length == 2){
1407     obj.setAttribute(x[0], x[1]);
1408     }
1409     }
1410     }
1411    
1412     function createSVGObj(tag, attrs, fill, color, lw){
1413     var ele = createSVGElement(tag, 'new');
1414     setAttributes(ele, attrs);
1415     if (fill) ele.setAttribute('fill', fill);
1416     if (color) ele.setAttribute('stroke', color);
1417     if(lw) ele.setAttribute('stroke-width', lw);
1418    
1419     return ele;
1420     }
1421    
1422     function createSVGText(txt, x, y, size, color){
1423     var ele = createSVGElement('text', 'new');
1424    
1425     ele.setAttribute('x', x);
1426     ele.setAttribute('y', y);
1427     ele.setAttribute('font-size', size);
1428     ele.setAttribute('fill', color);
1429     ele.textContent=txt;
1430    
1431     return ele;
1432     }
1433    
1434     function createSVGImage(fname, width, height, attrs){
1435     var ele = createSVGElement('image', 'new');
1436 isao-hara 7 ele.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', fname);
1437 isao-hara 2 ele.setAttribute('width', width);
1438     ele.setAttribute('height', height);
1439     setAttributes(ele, attrs);
1440    
1441     return ele;
1442     }
1443    
1444     function createSVGMarker(pid, id, child){
1445     var parent = document.getElementById(pid);
1446     if(!parent) return;
1447     var ele = createSVGElement('marker', pid+'_'+id);
1448     ele.appendChild(child);
1449     return ele;
1450     }
1451    
1452     function setLeftArrow(){
1453     if( selectedItems.length == 1 ){
1454     setArrow(selectedItems[0], 'start', '');
1455 isao-hara 36 updateShowMenu();
1456 isao-hara 2 }
1457     }
1458    
1459     function setRightArrow(){
1460     if( selectedItems.length == 1 ){
1461     setArrow(selectedItems[0], 'end', '');
1462 isao-hara 36 updateShowMenu();
1463 isao-hara 2 }
1464     }
1465    
1466     function removeLeftArrow(){
1467     if( selectedItems.length == 1 ){
1468     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1469     var pid = selectedItems[0].getAttribute('id');
1470     var marker = document.getElementById(pid+'_startarrow');
1471     svg_defs.removeChild(marker);
1472     selectedItems[0].removeAttribute('marker-start');
1473 isao-hara 36 updateShowMenu();
1474 isao-hara 2 }
1475     }
1476    
1477     function removeRightArrow(){
1478     if( selectedItems.length == 1 ){
1479     if(!svg_defs) svg_defs = document.getElementById('svg_defs');
1480     var pid = selectedItems[0].getAttribute('id');
1481     var marker = document.getElementById(pid+'_endarrow');
1482     svg_defs.removeChild(marker);
1483     selectedItems[0].removeAttribute('marker-end');
1484 isao-hara 54 updateShowMenu();
1485 isao-hara 2 }
1486     }
1487    
1488     function hasArrow(pos){
1489     var pobj = selectedItems[0];
1490     if(!pobj) return false;
1491     var pid = pobj.getAttribute('id');
1492     var marker = document.getElementById(pid+'_'+pos+'arrow');
1493     if(marker) return true;
1494     return false;
1495     }
1496    
1497     function getArrowMarker(obj,pos){
1498     if(!obj) return false;
1499     var pid = obj.getAttribute('id');
1500     var marker = document.getElementById(pid+'_'+pos+'arrow');
1501     return false;
1502     }
1503    
1504     function setArrow(pobj, pos, type){
1505     if(!pobj) return;
1506     var pid = pobj.getAttribute('id');
1507     var marker = document.getElementById(pid+'_'+pos+'arrow');
1508 isao-hara 56 if(marker){
1509     popupInfo("marker:"+marker);
1510     return;
1511     }
1512 isao-hara 2
1513     var obj = createSVGElement('path', 'new');
1514    
1515     var refX = 0;
1516     var refY = 0;
1517    
1518     switch(pos){
1519     case 'start':
1520     refX=10;
1521     refY=10;
1522     setAttributes(obj,'d=M 20 0 L 0 10 20 20 Z');
1523     break;
1524     case 'end':
1525     setAttributes(obj,'d=M 0 0 L 20 10 0 20 Z');
1526     refX= 10;
1527     refY=10;
1528     break;
1529     default:
1530     return;
1531     }
1532    
1533     marker = createSVGMarker(pid, pos+'arrow' , obj);
1534     setAttributes(marker,'markerWidth=10,markerHeight=10,orient=auto,viewBox=0 0 20 20,markerUnits=strokeWidth,refX='+refX+',refY='+refY);
1535    
1536     marker.setAttribute("fill",pobj.getAttribute("stroke"));
1537    
1538     defSVGElement(marker);
1539 isao-hara 56 if(!document.getElementById(pid+'_'+pos+'arrow')){
1540     alert("Error in setArrow");
1541     return;
1542     }
1543 isao-hara 2 var mid = "url(#"+marker.getAttribute("id")+")";
1544     var mattr = "marker-"+pos;
1545     pobj.setAttribute(mattr, mid);
1546     }
1547    
1548 isao-hara 44 function scalePath(itm, scale){
1549 isao-hara 52 if(!scale) return;
1550 isao-hara 44 var path = itm.getAttribute("d").split(' ');
1551 isao-hara 46 var bbox = itm.getBBox();
1552 isao-hara 44 var newpath = "";
1553 isao-hara 51 var sX=1;
1554     var sY=1;
1555 isao-hara 46 var isX=true;
1556     var dx = bbox.x - sX*bbox.x;
1557     var dy = bbox.y - sY*bbox.y;
1558 isao-hara 51 if(scale){
1559     sX=scale[0];
1560     sY=scale[1];
1561     }
1562 isao-hara 44
1563     for(var i=0;i<path.length ;i++){
1564     if(path[i].match(/[0-9]+/)){
1565     var val = parseInt(path[i]);
1566 isao-hara 46 if(isX){
1567     path[i] = Math.round(val*sX) + dx ;
1568     isX = false;
1569     }else{
1570     path[i] = Math.round(val*sY) + dy ;
1571     isX = true;
1572     }
1573 isao-hara 44 }
1574     newpath += path[i] + ' ';
1575     }
1576    
1577     itm.setAttribute("d",trim(newpath));
1578     }
1579    
1580     function scaleLine(itm, scale){
1581 isao-hara 52 if(!scale) return;
1582 isao-hara 46 var sX=scale[0];
1583     var sY=scale[1];
1584 isao-hara 44 var x1 = parseInt(itm.getAttribute("x1"));
1585     var y1 = parseInt(itm.getAttribute("y1"));
1586     var x2 = parseInt(itm.getAttribute("x2"));
1587     var y2 = parseInt(itm.getAttribute("y2"));
1588 isao-hara 46 var bbox = itm.getBBox();
1589     var dx = bbox.x - sX*bbox.x;
1590     var dy = bbox.y - sY*bbox.y;
1591 isao-hara 44
1592 isao-hara 46 itm.setAttribute("x1", Math.round(x1*sX)+dx);
1593     itm.setAttribute("y1", Math.round(y1*sY)+dy);
1594     itm.setAttribute("x2", Math.round(x2*sX)+dx);
1595     itm.setAttribute("y2", Math.round(y2*sY)+dy);
1596 isao-hara 44 }
1597    
1598 isao-hara 2 function appendSVGObj(obj){
1599     var svg_top = document.getElementById('svg_top');
1600     if(!svg_top) return;
1601    
1602     svg_top.appendChild(obj);
1603     }
1604    
1605     function isChildById(element, id) {
1606     if (element == null || element.parentNode == null || element.parentNode.nodeName=='BODY') return false;
1607     else if (element.parentNode.id == id) return true;
1608     else return isChildById(element.parentNode, id);
1609     }
1610    
1611     /////////////////////// Formatting SVG DOM
1612     function escapeHTML(text) {
1613     return text.replace( /[<>"&]/g,
1614     function (m) { return { '<': '&lt;', '>': '&gt;', '"': '&quot;', '&': '&amp;' }[m]; }
1615     );
1616     };
1617    
1618     function formatTag(ele){
1619     var str="";
1620     if(ele.nodeType == 1){
1621     var tag_a = ele.tagName.split(':');
1622     var tag;
1623     if(tag_a.length == 1){ tag = "svg:"+tag_a[0]; }else{ tag = ele.tagName; }
1624    
1625     str += "<"+tag;
1626     var attrs = ele.attributes;
1627     for(var i=0; i<attrs.length; i++){
1628     str += " "+attrs[i].nodeName+"=\""+attrs[i].nodeValue+"\"";
1629     }
1630     var cn = ele.childNodes;
1631     if(cn.length > 0){
1632     str +=">\n";
1633     for(var i=0; i<cn.length; i++){
1634 isao-hara 13 var tmp = trim(formatTag(cn[i]));
1635     if(tmp) str += " "+tmp+"\n";
1636 isao-hara 2 }
1637     str += "</"+tag+">";
1638     }else{
1639     str +=" />";
1640     }
1641     return str;
1642     }else if(ele.nodeType==3){
1643     return ele.textContent;
1644     }
1645     }
1646    
1647     function getSVGContent(){
1648     if(!svg_top) return "";
1649    
1650     var str = "";
1651     var elements = svg_top.childNodes;
1652     for(var i=0; i<elements.length; i++){
1653 isao-hara 12 if(elements[i] != svg_select){
1654     var tmp = trim(formatTag(elements[i]));
1655     if(tmp) str += tmp + '\n';
1656     }
1657 isao-hara 2 }
1658     return str;
1659     }
1660    
1661     function trim(str){
1662     return str.replace(/(^\s+)|(\s+$)/g, "");
1663     }
1664    
1665     /////// Access Server
1666     function newXMLRequest(){
1667     if(this.XMLHttpRequest){
1668     return new XMLHttpRequest();
1669     }else {
1670     return new ActiveXObject("Microsoft.XMLHTTP");
1671     }
1672     }
1673    
1674     function createRequestData(data){
1675     var str="filetype=svg";
1676     for (var i in data){
1677     str = str +"&"+ i +"="+encodeURIComponent(data[i]);
1678     }
1679     return str;
1680     }
1681    
1682     function postRequest(url, data, func){
1683     var postData=createRequestData(data);
1684     var obj=newXMLRequest();
1685    
1686     obj.onreadystatechange = function(){
1687     if (obj.readyState == 4 && obj.status == 200){
1688     func(obj.responseText);
1689     }
1690     }
1691     obj.open("POST", url, true);
1692     obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
1693     obj.send(postData);
1694     }
1695    
1696     function commnadFinishAlert(s){
1697     alert(s);
1698     }
1699    
1700     function uploadFileData(transaction, results){
1701     var data = results.rows.item(0);
1702     var updata = new Array(0);
1703     updata['name']= data['name'];
1704     updata['datalob'] = data['datablob'];
1705     updata['cmd'] = 'upload'
1706    
1707     postRequest(MgrPath+iSlideMgr, updata, commnadFinishAlert);
1708     }
1709    
1710     function uploadFile(name){
1711     getFile(name, 'uploadFileData');
1712     }
1713    
1714     function getRemoteFileList(){
1715     var data=new Array();
1716     data['name'] = "SVG";
1717     data['cmd'] = "list";
1718     postRequest(MgrPath+iSlideMgr, data, fileSelectorOnServer);
1719     }
1720    
1721     function previewRemoteFile(content){
1722     previewFile(content);
1723     setMode('Preview');
1724     }
1725    
1726     function getRemoteFile(name){
1727     var data=new Array();
1728     data['name'] = name;
1729     data['cmd'] = "get";
1730     postRequest(MgrPath+iSlideMgr, data, previewRemoteFile);
1731     }
1732    
1733 isao-hara 16 function saveRemoteFile(content){
1734 isao-hara 17 var datadiv = document.getElementById('tempdata');
1735     var fname = datadiv.getAttribute('lfname');
1736     saveContentWithFilename(fname, content);
1737 isao-hara 16 }
1738    
1739     function downloadFile(name){
1740     var data=new Array();
1741     data['name'] = name;
1742     data['cmd'] = "get";
1743     var datadiv = document.getElementById('tempdata');
1744     datadiv.setAttribute('lfname', name);
1745     postRequest(MgrPath+iSlideMgr, data, saveRemoteFile);
1746     }
1747    
1748 isao-hara 2 function showRemoteFile(name){
1749     getRemoteFile(name);
1750     }
1751    
1752 isao-hara 16 function downloadToFile(name){
1753     var downloadForm = "";
1754     downloadForm += "<form action=\"iSlideManager.php\" method=\"post\">";
1755     downloadForm += "<input type=\"hidden\" name=\"cmd\" value=\"download\">";
1756     downloadForm += "<input type=\"hidden\" name=\"name\" value=\""+name+"\">";
1757     downloadForm += "<input type=\"hidden\" name=\"filetype\" value=\"svg\">";
1758     downloadForm += "<input type=\"submit\">";
1759     downloadForm += "</form>";
1760    
1761     var cmdForm = document.getElementById('cmdForm');
1762     cmdForm.innerHTML = downloadForm;
1763     cmdForm.firstChild.submit();
1764     }
1765    
1766 isao-hara 2 //////// Event Handler
1767 isao-hara 51 function selectedRectangle(x, y, ex, ey){
1768     if(!svg_select) return null;
1769    
1770     svg_select.setAttribute("x",x);
1771     svg_select.setAttribute("y",y);
1772     svg_select.setAttribute("width", ex-x);
1773     svg_select.setAttribute("height", ey-y);
1774     svg_select.setAttribute("visibility", "visible");
1775     return svg_select;
1776     }
1777    
1778     function hideSelectedRectangle(){
1779     svg_select.setAttribute("x",0);
1780     svg_select.setAttribute("y",0);
1781     svg_select.setAttribute("width",0);
1782     svg_select.setAttribute("height",0);
1783     svg_select.setAttribute("visibility","hidden");
1784     }
1785    
1786 isao-hara 2 function setInnerHTML(id, val){
1787     var itm=document.getElementById(id);
1788     if(itm) itm.innerHTML=val;
1789     }
1790    
1791 isao-hara 41 function toggleRotateLock(){
1792 isao-hara 51 var lock = document.getElementById('rot-lock');
1793     var unlock = document.getElementById('rot-unlock');
1794     if(svg_rotate_locked){
1795     svg_rotate_locked = false;
1796     lock.style.display='none';
1797     unlock.style.display='block';
1798     }else{
1799     svg_rotate_locked = true;
1800     lock.style.display='block';
1801     unlock.style.display='none';
1802     }
1803 isao-hara 42 }
1804    
1805     function toggleScaleLock(){
1806 isao-hara 51 var lock = document.getElementById('scale-lock');
1807     var unlock = document.getElementById('scale-unlock');
1808     if(svg_scale_locked){
1809     svg_scale_locked = false;
1810     lock.style.display='none';
1811     unlock.style.display='block';
1812     }else{
1813     svg_scale_locked = true;
1814     lock.style.display='block';
1815     unlock.style.display='none';
1816     }
1817 isao-hara 41 }
1818    
1819 isao-hara 2 function popupInfo(val){
1820     var str="<button onClick=\"hideItemById('popup');\">Close</button><hr> ";
1821     str += val;
1822     setInnerHTML('popup', str);
1823     showItemById('popup');
1824     }
1825    
1826     function addAttributeVal(obj, itm, dv){
1827     if(obj.hasAttribute(itm)){
1828     var x = parseInt(obj.getAttribute(itm));
1829     x = x + dv;
1830     obj.setAttribute(itm, x)
1831     }
1832     }
1833    
1834     function showSVGSource(){
1835     var str = getSVGContent();
1836 isao-hara 14 var escstr="<button onClick=\"hideItemById('popup');\">Close</button>";
1837     escstr+="<button onClick=\"saveContent(document.getElementById('ContentView').value); previewData(document.getElementById('ContentView').value);\">Save</button><hr> ";
1838 isao-hara 51 escstr += "<textarea cols=\"100\" rows=\"30\" id=\"ContentView\">"+str+"</textarea>";
1839 isao-hara 2 setInnerHTML('popup', escstr);
1840     showItemById('popup');
1841     }
1842    
1843    
1844     function getElementTag(obj){
1845 isao-hara 6 if (!obj) return null;
1846 isao-hara 2 var tag = obj.tagName.split(':');
1847     if(tag.length == 2 && tag[0]=='svg') return tag[1];
1848     return tag[0];
1849     }
1850    
1851     function setSVGObjectProp(obj){
1852     var tag = getElementTag(obj);
1853     var res ="";
1854     switch(tag){
1855     case 'text':
1856 isao-hara 36 res = propSVGText(obj.textContent, obj.getAttribute("font-size"), obj.getAttribute("fill"),getRotateAngle(obj));
1857 isao-hara 2 break;
1858     case 'rect':
1859     case 'circle':
1860     case 'ellipse':
1861 isao-hara 39 res = propSVGObj(tag+":", obj.getAttribute("stroke-width"), obj.getAttribute("stroke"),
1862     obj.getAttribute("fill"),obj.getAttribute("stroke-dasharray"),getRotateAngle(obj));
1863 isao-hara 2 break;
1864     case 'path':
1865     case 'line':
1866     res = propSVGLine(tag+":",obj.getAttribute("stroke-width"),
1867 isao-hara 36 obj.getAttribute("stroke"),obj.getAttribute("fill"), obj.getAttribute("stroke-dasharray"),getRotateAngle(obj));
1868 isao-hara 2 break;
1869 isao-hara 5 case 'image':
1870 isao-hara 36 res = propSVGImage(obj.getAttribute("xlink:href"),obj.getAttribute("width"),obj.getAttribute("height"),getRotateAngle(obj));
1871 isao-hara 5 break;
1872 isao-hara 2 default:
1873     break;
1874     }
1875    
1876     return res
1877     }
1878    
1879     function updateSVGObj(){
1880     if(selectedItems.length != 1) return;
1881     var obj = selectedItems[0];
1882     var tag = getElementTag(obj);
1883 isao-hara 36 var deg=document.getElementById('svg_rotate');
1884 isao-hara 2 var res ="";
1885     switch(tag){
1886     case 'text':
1887     var txt=document.getElementById('svg_text');
1888     var color=document.getElementById('svg_color');
1889     var size=document.getElementById('svg_size');
1890     obj.textContent = txt.value;
1891     obj.setAttribute("fill", color.value);
1892     obj.setAttribute("font-size", size.value);
1893 isao-hara 36 setRotate(obj,deg.value);
1894 isao-hara 2 break;
1895     case 'rect':
1896     case 'circle':
1897     case 'ellipse':
1898     var fill=document.getElementById('svg_fill');
1899     var color=document.getElementById('svg_color');
1900     var L=document.getElementById('svg_stroke');
1901 isao-hara 39 var dash=document.getElementById('svg_stroke_type');
1902 isao-hara 2 obj.setAttribute("fill", fill.value);
1903     obj.setAttribute("stroke", color.value);
1904     obj.setAttribute("stroke-width", L.value);
1905 isao-hara 39 if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1906 isao-hara 36 setRotate(obj,deg.value);
1907 isao-hara 2 break;
1908     case 'path':
1909     case 'line':
1910     var fill=document.getElementById('svg_fill');
1911     var color=document.getElementById('svg_color');
1912     var L=document.getElementById('svg_stroke');
1913     var dash=document.getElementById('svg_stroke_type');
1914     obj.setAttribute("fill", fill.value);
1915     obj.setAttribute("stroke", color.value);
1916     obj.setAttribute("stroke-width", L.value);
1917 isao-hara 39 if(dash) obj.setAttribute("stroke-dasharray", dash.value);
1918 isao-hara 36 setRotate(obj,deg.value);
1919 isao-hara 39
1920 isao-hara 2 var id = obj.getAttribute("id");
1921     var marker = document.getElementById(id+'_startarrow');
1922     if(marker) marker.setAttribute("fill", color.value);
1923     var marker = document.getElementById(id+'_endarrow');
1924     if(marker) marker.setAttribute("fill", color.value);
1925     break;
1926 isao-hara 5 case 'image':
1927     var fname=document.getElementById('svg_text');
1928     var w=document.getElementById('svg_w');
1929     var h=document.getElementById('svg_h');
1930 isao-hara 6 obj.setAttribute("xlink:href", fname.value);
1931 isao-hara 5 obj.setAttribute("width", w.value);
1932     obj.setAttribute("height", h.value);
1933 isao-hara 36 setRotate(obj,deg.value);
1934 isao-hara 5 break;
1935 isao-hara 2 default:
1936     break;
1937     }
1938    
1939     }
1940    
1941     function delSVGObj(){
1942     if(!svg_top) return;
1943 isao-hara 43 for(var i=0; i< selectedItems.length; i++){
1944     var obj = selectedItems[i];
1945     svg_top.removeChild(obj);
1946     }
1947 isao-hara 51 hideSelectedRectangle();
1948 isao-hara 2 }
1949    
1950     ///// For MobileSafari
1951     function getPreviewX(x){ return x - preview.offsetLeft; }
1952     function getPreviewY(y){ return y - preview.offsetTop; }
1953    
1954 isao-hara 51 function calcDict(x1, y1, x2, y2){
1955 isao-hara 2 return (x1-x2)*(x1-x2) +(y1-y2)*(y1-y2);
1956     }
1957    
1958     function isIncludeEllipse( x1, y1, cx, cy, rx, ry){
1959     return ((x1-cx)*(x1-cx)/rx/rx +(y1-cy)*(y1-cy)/ry/ry) < 1;
1960     }
1961    
1962     function getAttributeVal(obj, name){
1963     try{
1964     return parseInt(obj.getAttribute(name));
1965     }catch(e){ return 0; }
1966     }
1967    
1968     function checkIntersection(obj, x, y, ex, ey){
1969 isao-hara 18 if(!obj) return false;
1970 isao-hara 2 var res = true;
1971     var bbox = obj.getBBox();
1972     var ox = bbox.x;
1973     var oy = bbox.y;
1974     var oex = bbox.x+bbox.width;
1975     var oey = bbox.y+bbox.height;
1976     if( ex < ox || oex < x || ey < oy || oey < y) res = false;
1977    
1978     var tag = getElementTag(obj);
1979     switch(tag){
1980     case 'circle':
1981     case 'ellipse':
1982     var cx = getAttributeVal(obj,"cx");
1983     var cy = getAttributeVal(obj,"cy");
1984     var rx = getAttributeVal(obj,"r");
1985     var ry = rx;
1986     if(!rx){
1987     var rx = getAttributeVal(obj,"rx");
1988     var ry = getAttributeVal(obj,"ry");
1989     }
1990    
1991     if(res){
1992     if(cx <x && cy < y && !isIncludeEllipse(x,y,cx,cy,rx,ry)) res=false;
1993     else if(cx < x && cy > ey && !isIncludeEllipse(x,ey,cx,cy,rx,ry)) res=false;
1994     else if(cx > ex && cy > ey && !isIncludeEllipse(ex,ey,cx,cy,rx,ry)) res=false;
1995     else if(cx > ex && cy < y && !isIncludeEllipse( ex,y,cx,cy,rx,ry)) res=false;
1996     }
1997     break;
1998     case 'path':
1999     var d = obj.getAttribute("d");
2000     var p = getPoints(d);
2001     for(var i=0;i<p.length;i++){
2002     var ox=p[i][0];
2003     var oy=p[i][1];
2004     if(x < ox && ox < ex && y < oy && oy < ey) {
2005     return true;
2006     }
2007     }
2008     return false;
2009     break;
2010     case 'line':
2011     var x1 = getAttributeVal(obj,"x1");
2012     var y1 = getAttributeVal(obj,"y1");
2013     var x2 = getAttributeVal(obj,"x2");
2014     var y2 = getAttributeVal(obj,"y2");
2015 isao-hara 3 var d = (y2-y1)/(x2-x1);
2016    
2017     if(res){
2018     var xx = Math.abs(x2-x1);
2019     var sign = 1;
2020     if(x2-x1 < 0){ sign = -1; }
2021     for(var i=0; i < xx; i++){
2022     var nx = i*sign + x1;
2023     var ny = d * i*sign + y1;
2024     if(x < nx && nx < ex && y < ny && ny < ey) return true;
2025     }
2026     }
2027 isao-hara 2 return false;
2028    
2029 isao-hara 3 return res;
2030 isao-hara 2 break;
2031     case 'text':
2032     case 'rect':
2033     case 'polygon':
2034     case 'polyline':
2035     case 'image':
2036     break;
2037     default:
2038     res=false;
2039     break;
2040     }
2041     return res;
2042     }
2043    
2044     function getBoundingBox(obj){
2045     var res = new Array(4);
2046     var bbox = obj.getBBox();
2047     res[0] = bbox.x-1;
2048     res[1] = bbox.y-1;
2049     res[2] = bbox.x+bbox.width+2;
2050     res[3] = bbox.y+bbox.height+2;
2051     return res;
2052     }
2053    
2054     function setSelectBox(){
2055     if(!svg_select) return;
2056    
2057     if(selectedItems.length == 0){
2058 isao-hara 51 hideSelectedRectangle()
2059 isao-hara 2 return;
2060     }
2061    
2062 isao-hara 51 var bbox = new Array(1000,1000,0,0);
2063 isao-hara 2
2064     for(var i=0;i<selectedItems.length;i++){
2065     var bp = getBoundingBox(selectedItems[i]);
2066     if(bp[0] < bbox[0]) bbox[0]=bp[0];
2067     if(bp[1] < bbox[1]) bbox[1]=bp[1];
2068     if(bp[2] > bbox[2]) bbox[2]=bp[2];
2069     if(bp[3] > bbox[3]) bbox[3]=bp[3];
2070     }
2071 isao-hara 51 selectedRectangle(bbox[0], bbox[1], bbox[2], bbox[3]);
2072 isao-hara 2 }
2073    
2074     function getSelectedObjects(x1, y1, x2, y2){
2075     if(x1 > x2) { var tmp = x1; x1=x2; x2=tmp; }
2076     if(y1 > y2) { var tmp = y1; y1=y2; y2=tmp; }
2077    
2078 isao-hara 3 var val="";
2079 isao-hara 2 if(svg_top){
2080     var val ="";
2081     var objs = svg_top.childNodes;
2082     selectedItems = new Array();
2083     for(var i=0; i<objs.length;i++){
2084     if(objs[i].tagName){
2085 isao-hara 3
2086 isao-hara 2 if(objs[i] != svg_select && checkIntersection(objs[i], x1, y1, x2, y2)){
2087     selectedItems.push(objs[i]);
2088     }
2089 isao-hara 3 val += objs[i].tagName+" ";
2090 isao-hara 2 }
2091     }
2092     }
2093     setSelectBox();
2094     }
2095    
2096 isao-hara 42 function dupObject(){
2097     if(selectedItems.length == 0){ return; }
2098     dupItems = selectedItems;
2099     dupX = parseInt(svg_select.getAttribute("x"));
2100     dupY = parseInt(svg_select.getAttribute("y"));
2101     setSVGMode('Duplicate');
2102     }
2103    
2104     function pasteObject(x,y){
2105     if(selectedItems.length == 0){ return; }
2106     for(var i=0;i<dupItems.length;i++){
2107     var itm = dupItems[i].cloneNode(true);
2108 isao-hara 47 itm.setAttribute("id", newID());
2109 isao-hara 42 replaceTranslate(itm,x-dupX,y-dupY);
2110     updateTransform(itm);
2111     appendSVGObj(itm);
2112     }
2113     }
2114    
2115 isao-hara 19 function onTouchStartCore(){
2116 isao-hara 35 if((!modeSVG || modeSVG == 'selector') && selectedItems.length == 0){ // Selector Mode
2117     var x1=getPreviewX(sx-1);
2118     var y1=getPreviewY(sy-1);
2119     var x2=getPreviewX(sx+2);
2120     var y2=getPreviewY(sy+2);
2121     getSelectedObjects(x1, y1, x2, y2);
2122    
2123     if(selectedItems.length == 0){
2124     setSVGMode('selector');
2125     }else if(selectedItems.length == 1){
2126     targetItem=selectedItems[0];
2127     setSVGMode(getElementTag(targetItem));
2128     }else{
2129     setSVGMode('Group');
2130     }
2131     }else { // CreateMode
2132     if(selectedItems.length == 0){
2133 isao-hara 2
2134 isao-hara 35 var fill=document.getElementById('svg_fill');
2135     var color=document.getElementById('svg_color');
2136     var L=document.getElementById('svg_stroke');
2137 isao-hara 2
2138 isao-hara 35 var x = getPreviewX(sx);
2139     var y = getPreviewY(sy);
2140 isao-hara 2
2141 isao-hara 35 switch(modeSVG){
2142     case 'text':
2143     var txt=document.getElementById('svg_text');
2144     var size=document.getElementById('svg_size');
2145     if(txt.value){
2146     y = y + parseInt(size.value)*0.8;
2147     targetItem=createSVGText(txt.value, x, y, size.value, color.value);
2148     }else{
2149     putInputForm(x, y, txt.value, size.value);
2150     }
2151     break;
2152     case 'rect':
2153     var attr = 'x='+x+',y='+y+',width='+svg_width+',height='+svg_height;
2154     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2155     break;
2156     case 'circle':
2157     var attr = 'cx='+x+',cy='+y+',r='+svg_rx;
2158     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2159     break;
2160     case 'ellipse':
2161     var attr = 'cx='+x+',cy='+y+',rx='+svg_rx+',ry='+svg_ry;
2162     targetItem=createSVGObj(modeSVG,attr, fill.value, color.value, L.value);
2163     break;
2164     case 'newPath':
2165 isao-hara 56 //var attr = 'd=M '+x+' '+y+' L '+x+' '+y;
2166     var attr = 'd=M '+x+' '+y+' L';
2167 isao-hara 35 targetItem=createSVGObj('path' ,attr, 'none', color.value, L.value);
2168 isao-hara 56 is_newPath=true;
2169 isao-hara 35 break;
2170     case 'newLine':
2171     var x2=x+1;
2172     var attr = 'x1='+x+',y1='+y+',x2='+x2+',y2='+y;
2173     targetItem=createSVGObj('line' ,attr, 'none', color.value, L.value);
2174     break;
2175     case 'image':
2176     var attr = 'x='+x+',y='+y;
2177     var txt=document.getElementById('svg_text');
2178     var w=document.getElementById('svg_w');
2179     var h=document.getElementById('svg_h');
2180     if(txt.value) targetItem=createSVGImage(txt.value ,w.value, h.value, attr);
2181     default:
2182     break;
2183 isao-hara 2 }
2184 isao-hara 35 if (targetItem){
2185     appendSVGObj(targetItem);
2186     selectedItems[0]=targetItem;
2187     }
2188     }else{
2189     var x1=getPreviewX(sx-1);
2190     var y1=getPreviewY(sy-1);
2191     var x2=getPreviewX(sx+2);
2192     var y2=getPreviewY(sy+2);
2193 isao-hara 42
2194     if(modeSVG == 'Duplicate'){ pasteObject(x1,y1); }
2195     if(modeSVG == 'newPath'){
2196     if(targetItem.tagName == 'path' ){
2197     var path = targetItem.getAttribute("d");
2198 isao-hara 56
2199     /*
2200     if(is_newPath){
2201     path="M ";
2202     var path_arr=path.aplit(' ');
2203     path += path_arr[1]+' '+path_arr[2]+' L';
2204     is_newPath=false;
2205     }
2206     */
2207 isao-hara 42 path = path + ' '+ getPreviewX(sx) + ' '+ getPreviewY(sy) ;
2208 isao-hara 56 // popupInfo(path);
2209 isao-hara 42 targetItem.setAttribute("d",path);
2210     }
2211     return;
2212     }
2213 isao-hara 35 if(!checkIntersection(svg_select, x1, y1, x2, y2)){
2214     setSVGMode('selector');
2215     }
2216 isao-hara 2 }
2217 isao-hara 35 }
2218 isao-hara 2 }
2219    
2220 isao-hara 25
2221     function onDoubleTap(e){
2222     if(selectedItems.length == 1 ){
2223 isao-hara 51 hideSelectedRectangle();
2224 isao-hara 29
2225 isao-hara 25 var obj = selectedItems[0];
2226 isao-hara 51 if(!obj) return;
2227 isao-hara 25 switch(obj.tagName){
2228     case 'svg:text':
2229 isao-hara 29 case 'text':
2230 isao-hara 25 var txt = trim(obj.textContent);
2231     var size = parseInt(obj.getAttribute("font-size"));
2232     var x = parseInt(obj.getAttribute("x"));
2233     var y = parseInt(obj.getAttribute("y"));
2234     x = x-20;
2235     y = y-size*0.8 -10;
2236     putInputForm(x, y, txt, size, obj.id);
2237     obj.style.display = 'none';
2238     editingTextObj = obj;
2239     break;
2240 isao-hara 42 case 'path':
2241     if(modeSVG == 'newPath'){ setSVGMode('selector'); }
2242     break;
2243 isao-hara 25 default:
2244     break;
2245     }
2246 isao-hara 29 return false;
2247 isao-hara 25 }
2248     }
2249    
2250 isao-hara 2 function getPoints(d){
2251     var p = d.split(' ');
2252     var res = new Array();
2253     var isx=true;
2254     var x, y;
2255     for(var i=0; i<p.length;i++){
2256     if(p[i].match('[MLHVCSQTA]','i')){
2257     }else{
2258     if(isx){
2259     x = parseInt(p[i]);
2260     }else{
2261     y = parseInt(p[i]);
2262     res.push(new Array(x, y));
2263     }
2264     isx = !isx;
2265     }
2266     }
2267     return res;
2268     }
2269    
2270     function updatePath(d, x, y){
2271     var p = d.split(' ');
2272     var res = "";
2273     var isx=true;
2274     var val;
2275     for(var i=0; i<p.length;i++){
2276 isao-hara 42 if(trim(p[i]) == "") continue;
2277 isao-hara 2 if(p[i].match('[MLHVCSQTA]','i')){
2278     res += ' '+p[i];
2279     }else{
2280     if(isx){
2281     val = parseInt(p[i])+x;
2282     }else{
2283     val = parseInt(p[i])+y;
2284     }
2285    
2286     res += ' '+ val;
2287     isx = !isx;
2288     }
2289     }
2290     return trim(res);
2291     }
2292    
2293     function updateTransform(obj){
2294     try{
2295     var trans = obj.getAttribute("transform");
2296     if(!trans) return;
2297     if(trans.match(new RegExp("translate(.+,.+)","i"))){
2298     var str = RegExp.lastMatch;
2299     var vals = str.substr(10,str.length-11).split(',') ;
2300     var dx = parseInt(vals[0]);
2301     var dy = parseInt(vals[1]);
2302     switch(getElementTag(obj)){
2303     case 'text':
2304     case 'rect':
2305 isao-hara 6 case 'image':
2306 isao-hara 2 addAttributeVal(obj, "x", dx);
2307     addAttributeVal(obj, "y", dy);
2308     break;
2309     case 'circle':
2310     case 'ellipse':
2311     addAttributeVal(obj, "cx", dx);
2312     addAttributeVal(obj, "cy", dy);
2313     break;
2314    
2315     case 'path':
2316     var path = obj.getAttribute("d");
2317     obj.setAttribute("d", updatePath(path, dx, dy));
2318     break;
2319     case 'line':
2320     var x1 = getAttributeVal(obj,"x1");
2321     var y1 = getAttributeVal(obj,"y1");
2322     var x2 = getAttributeVal(obj,"x2");
2323     var y2 = getAttributeVal(obj,"y2");
2324     obj.setAttribute("x1", x1+dx);
2325     obj.setAttribute("y1", y1+dy);
2326     obj.setAttribute("x2", x2+dx);
2327     obj.setAttribute("y2", y2+dy);
2328    
2329     break;
2330     default:
2331     break;
2332     }
2333 isao-hara 36 replaceTranslate(obj, 0, 0);
2334     updateRotate(obj