Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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