Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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