Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26