Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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