Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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