Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /SvgEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


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