Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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