Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /iSlideEditor.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 67 - (hide annotations) (download) (as text)
Mon Aug 2 01:36:34 2010 UTC (13 years, 8 months ago) by isao-hara
File MIME type: application/x-javascript
File size: 29593 byte(s)
css bug fix
1 isao-hara 2 /*
2     iSlideEditor.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 currentPage=0;
20     var editarea_h=10;
21     var editarea_w=70;
22     var iSlide_content;
23     var MainTitle="iSlide Maker";
24    
25 isao-hara 16 var iSlideMgr='iSlideManager.php';
26     var MgrPath="";
27    
28 isao-hara 2 //
29     function initEditor(name, dispname, size){
30     initDB(name, dispname, size);
31     fileSelector();
32 isao-hara 34 restoreValues();
33 isao-hara 2 }
34    
35 isao-hara 34 function restoreValues(){
36     if(typeof(localStorage) == 'undefined'){
37     alert('local storage not suported');
38     }
39     MgrPath = localStorage.getItem('MgrPath') ? localStorage.getItem('MgrPath') : "";
40     window.onbeforeuload=function(){ return saveChanges(); }
41     }
42    
43     function clearAll(){
44     localsStorage.clear();
45     restoreValudes();
46     }
47    
48     function saveChanges(){
49     localStorage.setItem('MgrPath', MgrPath);
50     }
51    
52 isao-hara 2 // initialize a database
53 isao-hara 51 function initDB(name, dispname, size){
54 isao-hara 2 try {
55     if (!window.openDatabase) {
56 isao-hara 67 alert('not supported');
57     return null;
58 isao-hara 2 } else {
59 isao-hara 67 var version = '1.0';
60     var myDB = openDatabase(name, version, dispname, size);
61     createTables(myDB);
62     systemDB = myDB;
63     return myDB;
64 isao-hara 2 }
65     } catch(e) {
66     if (e == INVALID_STATE_ERR) { // Version number mismatch.
67     alert("Invalid database version.");
68     } else {
69     alert("Unknown error "+e+".");
70     }
71     return;
72     }
73    
74     }
75    
76 isao-hara 51 function createTables(db){
77     if (0) dropTables(db);
78    
79 isao-hara 2 db.transaction(
80     function (transaction) {
81     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);
82     transaction.executeSql('CREATE TABLE IF NOT EXISTS filedata(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, datablob BLOB NOT NULL DEFAULT "");', [], nullDataHandler, errorHandler);
83     }
84     );
85     }
86    
87 isao-hara 51 function dropTables(db){
88 isao-hara 2 db.transaction(
89     function (transaction) {
90     transaction.executeSql('DROP TABLE files;');
91     transaction.executeSql('DROP TABLE filedata;');
92     }
93     );
94     }
95    
96     //// Delete File
97 isao-hara 51 function deleteUpdateResults(transaction, results){
98     if (results.rowsAffected) { fileSelector(); }
99 isao-hara 2 }
100    
101     function reallyDelete(id){
102     var myDB = systemDB;
103    
104     myDB.transaction(
105     new Function("transaction",
106     "transaction.executeSql('UPDATE files set deleted=1 where id=?;', [ "+id+" ], null, errorHandler);"
107     +"transaction.executeSql('DELETE FROM filedata where id=?;', [ "+id+" ], deleteUpdateResults, errorHandler);")
108     );
109     }
110    
111     function deleteFile(name){
112     var myDB = systemDB;
113    
114     myDB.transaction(
115     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+name+"'],"+
116     "function (transaction, results) {"+
117     "if (confirm('Really delete '+results.rows.item(0)['name']+'?')) {"+
118     "reallyDelete(results.rows.item(0)['id']);"+
119     "}"+
120     "}, errorHandler);")
121     );
122     }
123    
124     function reallyDeleteAll(db){
125     var myDB = db;
126     if(!db) myDB = systemDB;
127    
128     myDB.transaction(
129     new Function("transaction",
130     "transaction.executeSql('DELETE FROM files;',[],null, errorHandler);"
131     +"transaction.executeSql('DELETE FROM filedata;',[],deleteUpdateResults, errorHandler);")
132     );
133     }
134    
135     function deleteAllFile(db){
136 isao-hara 51 if (confirm('Really delete all file?')) { reallyDeleteAll(db); }
137 isao-hara 2 }
138    
139     ///// Rename
140     function reallyRenameFileAction(id, newname){
141     var myDB = systemDB;
142    
143     myDB.transaction(
144     function (transaction) {
145     transaction.executeSql('UPDATE files set name=? where id=?;', [newname,id ], null, errorHandler);
146     }
147     );
148     fileSelector();
149     }
150    
151     function renameFileAction(){
152     var myDB = systemDB;
153     var new_name = document.getElementById('newFilename').value
154     var id = document.getElementById('fileId').value
155    
156     if(new_name == "") {
157     alert('Filename is required.');
158     fileSelector();
159     return;
160     }
161    
162     myDB.transaction(
163     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+new_name+"'],"+
164     "function (transaction, results) {"+
165     "if(results.rows.length == 0){"+
166     "reallyRenameFileAction('"+id+"','"+new_name+"');"+
167     "}else{"+
168     "alert(results.rows.item(0)['name']+' already exist');"+
169     "fileSelector();"+
170     "}"+
171     "}, errorHandler);")
172     );
173     }
174    
175     function renameFile(name, id){
176     var myDB = systemDB;
177     var menuDiv = document.getElementById('menuDiv');
178     var string = "";
179    
180     string += "<H1 class='title'>Rename File</H1>\n";
181     string += "<form action='javascript:renameFileAction()'>\n";
182    
183     string += "New Filename:<input id='newFilename' name='newname' value=\"\" />\n";
184     string += "<input type='hidden' id='fileId' value=\""+id+"\" />\n";
185     string += "<input type='submit' value='Rename' />\n";
186     string += "</form>\n";
187    
188     menuDiv.innerHTML=string;
189     }
190    
191     function docLink(row){
192     var name = row['name'];
193     var files_id = row['id'];
194    
195     var res = "<tr class='filerow'>";
196     res += "<td class='filenamecell' onClick=\"showFile('"+name+"');\">"+name+"</td>";
197     res += "<td class='filelinkcell'>";
198     res += "<button class='blue' onClick=\"editFile('"+name+"');\">&nbsp;Edit&nbsp;</button> &nbsp;";
199     res += "<button class='green' onClick=\"renameFile('"+name+"',"+files_id+");\">&nbsp;Rename&nbsp;</button> &nbsp;";
200     res += "<button class='#ffdddd' onClick=\"uploadFile('"+name+"');\">&nbsp;Upload&nbsp;</button> &nbsp;";
201     res += "<button class='red' onClick=\"deleteFile('"+name+"');\">Delete</button>";
202     res += "</td></tr>\n";
203    
204     return res;
205     }
206    
207 isao-hara 51 function fileSelector(){
208 isao-hara 2 var myDB = systemDB;
209    
210 isao-hara 67 if(!myDB){
211     getRemoteFileList();
212     return;
213     }
214 isao-hara 2 myDB.transaction(
215 isao-hara 51 function (transaction){
216 isao-hara 2 transaction.executeSql("SELECT * from files where deleted=0;", [ ],
217 isao-hara 51 function (transaction, results){
218     var filelist = '';
219     var menuDiv = document.getElementById('menuDiv');
220     for(var i=0; i<results.rows.length; i++) {
221     var row = results.rows.item(i);
222     filelist = filelist + docLink(row);
223     }
224     if (filelist == "") {
225     filelist = "No files.<br />\n";
226     } else {
227     filelist = "<center><table class='filetable'>"+filelist+"</table></center>";
228     }
229     menuDiv.innerHTML="<H1 class='title'>"+MainTitle+"</H1>"+createMenuBar()+filelist;
230     }, errorHandler);
231 isao-hara 2 }
232     );
233     setMode('List');
234     }
235    
236 isao-hara 51 function fileSelectorOnServer(val){
237 isao-hara 16 var filelist = '';
238     var menuDiv = document.getElementById('menuDiv');
239     var results = val.split(',');
240    
241     if(menuDiv){
242     for(var i=0; i<results.length; i++) {
243     var row = results[i];
244     filelist = filelist + fileEntryOnServer(row);
245     }
246     if (filelist == "") {
247     filelist = "<br />No files.<br />\n";
248     } else {
249     filelist = "<center><table class='filetable'>"+filelist+"</table></center>";
250     }
251     var Menu = "<button onClick=\"fileSelector();\">Local Storage</button>";
252     Menu += "<button onClick=\"getRemoteFileList();\">List on Server</button>";
253 isao-hara 51 menuDiv.innerHTML="<H1 class='title'>"+MainTitle+"</H1>"+Menu+filelist;
254     }
255 isao-hara 16 setMode('List');
256     }
257    
258     function fileEntryOnServer(name){
259     name = name.split('.')[0];
260     if(!trim(name)) return "";
261    
262     var res = "<tr class='filerow'>";
263     res += "<td class='filenamecell' onClick=\"getRemoteFile('"+name+"');\">"+name+"</td>";
264     res += "<td class='filelinkcell'>";
265     res += "<button class='green' onClick=\"showRemoteFile('"+name+"');\">View</button>";
266     res += "<button onClick=\"downloaFile('"+name+"');\">Download to local storage</button>";
267     res += "<button onClick=\"downloadToFile('"+name+"');\">Download as SVG File</button>";
268     res += "</td></tr>\n";
269    
270     return res;
271     }
272    
273 isao-hara 2 function createMenuBar(){
274     var menu = "<ul id='menubar'>";
275     menu += "<li><button onClick='createNewFile()'>Create New File</button></li>";
276     menu += "<li><button onClick='deleteAllFile()'>Delete All</button></li>";
277     menu += "<li><button onClick='getRemoteFileList()'>File List on a Server</button></li>";
278 isao-hara 34 menu += "<li><button onClick='configServer()'>Server Configuration</button></li>";
279 isao-hara 2 menu += "</ul><p class='cls'>";
280     return menu;
281     }
282    
283 isao-hara 34 //// Config
284     function setConfig(){
285     var ele = document.getElementById('ServerURL');
286     MgrPath=ele.value;
287     saveChanges();
288     alert("Done");
289     }
290    
291     function configServer(){
292     var popupDiv = document.getElementById('popup');
293     var string = "";
294    
295     string += "<H1 class='title'>Server Configration</H1>\n";
296     string += "<div class=\"input_form\">\n";
297     string += "Filename:<input id='ServerURL' name='url' value=\""+MgrPath+"\" size=\"80\"/><br>\n";
298     string += "<button onClick=\"setConfig();hideItemById('popup'); \">Done</button>\n";
299     string += "<button onClick=\"hideItemById('popup'); \">Cancel</button>\n";
300     string += "</div>\n";
301    
302     popupDiv.innerHTML=string;
303     popupDiv.style.display='block';
304     }
305    
306 isao-hara 2 function linkToCreateNewFile(){
307     return "<p><button onClick='createNewFile()'>Create New File</button>";
308     }
309    
310     /// new File
311     function reallyCreateNewFileAction(name){
312     var myDB = systemDB;
313    
314     myDB.transaction(
315     function (transaction) {
316     var myfunc = new Function("transaction", "results", "transaction.executeSql('INSERT INTO files (name, filedata_id) VALUES (?, ?);', [ '"+name+"', results.insertId], nullDataHandler, killTransaction);");
317    
318     transaction.executeSql('INSERT INTO filedata (datablob) VALUES ("");', [], myfunc, errorHandler);
319     }
320     );
321    
322     fileSelector();
323     }
324    
325     function createNewFileAction(){
326     var myDB = systemDB;
327     var name = document.getElementById('createFilename').value
328     if(name == "") {
329     if (confirm('Filename is required, try agein?')) { createNewFile(); }else{ fileSelector(); }
330     return;
331     }
332    
333     myDB.transaction(
334     new Function("transaction", "transaction.executeSql('SELECT id,name from files where name=?;',['"+name+"'],"+
335     "function (transaction, results) {"+
336     "if(results.rows.length == 0){"+
337     "reallyCreateNewFileAction('"+name+"');"+
338     "}else{"+
339     "if (confirm(results.rows.item(0)['name']+' already exist, try agein?')) {"+
340     "createNewFile();"+
341     "}else{"+
342     "fileSelector();"+
343     "}"+
344     "}"+
345     "}, errorHandler);")
346     );
347    
348     }
349    
350     function createNewFile(){
351     var myDB = systemDB;
352     var menuDiv = document.getElementById('menuDiv');
353     var string = "";
354    
355     string += "<H1 class='title'>Create New File</H1>\n";
356     string += "<form action='javascript:createNewFileAction()'>\n";
357     string += "Filename:<input id='createFilename' name='name' value=\"\" />\n";
358     string += "<input type='submit' value='create' />\n";
359     string += "</form>\n";
360    
361     menuDiv.innerHTML=string;
362     }
363    
364     /// EditMenu
365     function insertStr(str, len){
366     var editarea = document.getElementById('editarea');
367     if(len < 0) len = str.length;
368    
369     if (editarea){
370     if(str=='DQ') str='"';
371     var strs = editarea.value;
372     var cPos = editarea.selectionStart;
373     var tmp = strs.substr(0,cPos);
374     editarea.value = tmp +str+strs.substr(cPos, strs.length);
375     cPos += len;
376     editarea.setSelectionRange(cPos, cPos);
377     editarea.focus();
378     }
379     }
380    
381     function insertTag(tag){
382     var str;
383     var len = -1;
384     if (tag == "p"){
385 isao-hara 51 str = "<p> </p>";
386     len = 4;
387 isao-hara 2 }else if (tag == "ul"){
388 isao-hara 51 str = "<ul class=\" \"> \n\n</ul>";
389     len = 17;
390 isao-hara 2 }else if (tag == "li"){
391 isao-hara 51 str = "<li> </li>";
392     len = 5;
393 isao-hara 2 }else if (tag == "href"){
394 isao-hara 51 str = "xlink:href=\"\"";
395     len = 12;
396 isao-hara 2 }else if (tag == "EQ"){
397 isao-hara 51 str = "=\"\"";
398     len = 2;
399 isao-hara 2 }else if (tag == "SVG_obj"){
400 isao-hara 51 str = "<object width=\"80%\" height=\"50%\" data=\"image.svg?name=\" type=\"image/svg+xml\">";
401     len = 57;
402 isao-hara 2 }
403     insertStr(str, len);
404     }
405    
406     function editMenuBar() {
407 isao-hara 51 var str = "";
408     str += "<button onClick=\"insertNewSlide();\">New Slide</button>\n";
409     str += "<button onClick=\"insertStr('/',1);\">/</button>\n";
410     str += "<button onClick=\"insertStr('DQ',1);\">\"</button>\n";
411     str += "<button onClick=\"insertTag('EQ',1);\">=\"\"</button>\n";
412     str += "<button onClick=\"insertTag('p');\">p</button>\n";
413     str += "<button onClick=\"insertTag('href');\">href</button>\n";
414     str += "<button onClick=\"insertTag('SVG_obj');\">SVG</button>\n";
415 isao-hara 2
416 isao-hara 51 str += "<button onClick=\"chEditareaHeight();\">...</button>\n";
417     return str;
418 isao-hara 2 }
419    
420     function updateEditMenuSimple(){
421     var menuDiv = document.getElementById('menuDiv');
422     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
423     menuDiv.innerHTML+= "<button onClick=\"saveFile();fileSelector();\"> Save </button>\n";
424     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
425     menuDiv.style.display='block';
426     }
427    
428     /// Save File
429     function getContentSimple(){
430     var editarea = document.getElementById('editarea');
431     if(!editarea) return "";
432     return editarea.value;
433     }
434    
435     function getSlideContent(){
436     var editarea = document.getElementById('editarea');
437     var contents = "";
438    
439     if(editarea) iSlide_content[currentPage] = editarea.value;
440    
441     for(var i=0; i<iSlide_content.length ;i++ ){
442     contents += iSlide_content[i] +"\n";
443     }
444     return contents;
445     }
446    
447     function saveFile(){
448     var myDB = systemDB;
449    
450     var contents = getSlideContent();
451    
452     myDB.transaction(
453     function (transaction) {
454     var datadiv = document.getElementById('tempdata');
455     var filedata_id = datadiv.getAttribute('lfdataid');
456    
457     transaction.executeSql("UPDATE filedata set datablob=? where id=?;",
458     [ contents, filedata_id ], nullDataHandler, errorHandler);
459    
460     alert('Saved.');
461     }
462     );
463     }
464    
465     //// Editor
466     function getEditSlideContent(data){
467     var filter = new iSlideFormatter();
468     iSlide_content = filter.parse(data);
469     return iSlide_content[currentPage];
470     }
471    
472     function editFileData(transaction, results){
473     var editDiv = document.getElementById('editDiv');
474     var datadiv = document.getElementById('tempdata');
475    
476     var data = results.rows.item(0);
477     var filename = data['name'];
478     var filedata = data['datablob'];
479     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
480    
481     var data = getEditSlideContent(filedata);
482    
483     var editcontent="<textarea id=\"editarea\" rows=\""+editarea_h+"\" cols=\""+editarea_w+"\">"+data+"</textarea>\n";
484    
485     document.title="EditFile: "+filename;
486     updateEditMenu();
487     editDiv.innerHTML = editMenuBar() +"<br>"+ editcontent;
488    
489     setMode('Edit');
490     }
491    
492     function editFile(name){
493     getFile(name, 'editFileData');
494     }
495    
496     function editCurrentFile(){
497     var datadiv = document.getElementById('tempdata');
498     var name = datadiv.getAttribute('lfname');
499     editFile(name);
500     }
501    
502     // Show File
503     function updateShowMenu(){
504     var menuDiv = document.getElementById('menuDiv');
505     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
506     menuDiv.innerHTML+= "<button onClick=\"editCurrentFile();\"> Edit </button>\n";
507     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
508     menuDiv.style.display='block';
509     }
510    
511     function showFileData(transaction, results){
512     var data = results.rows.item(0);
513     var filename = data['name'];
514     var filedata = data['datablob'];
515    
516     var datadiv = document.getElementById('tempdata');
517     datadiv.setAttribute('lfdataid', parseInt(data['filedata_id']));
518 isao-hara 66 datadiv.setAttribute('lfname', filename);
519 isao-hara 2
520     document.title=filename;
521    
522     currentPage = 0;
523     getEditSlideContent(filedata)
524    
525     startPresentation();
526    
527     updateShowMenu();
528     setMode('Presentation');
529     }
530    
531     function showFile(name){
532     getFile(name, 'showFileData');
533     }
534    
535     function getFile(name, func){
536 isao-hara 50 var myDB = systemDB;
537 isao-hara 2 myDB.transaction(
538     function (transaction) {
539     transaction.executeSql('SELECT * from files, filedata where files.name=? and files.filedata_id = filedata.id;', [name], eval(func), errorHandler);
540     }
541     );
542     }
543    
544     // Error Handlers
545 isao-hara 50 function killTransaction(transaction, error){ return true; }
546 isao-hara 2
547     function errorHandler(transaction, error){
548     alert('Oops. Error was '+error.message+' (Code '+error.code+')');
549    
550     var we_think_this_error_is_fatal = true;
551     if (we_think_this_error_is_fatal) return true;
552     return false;
553     }
554    
555 isao-hara 50 function nullDataHandler(transaction, results){ }
556 isao-hara 2
557     function setMode(m){
558     currentMode=m;
559     switch(m){
560     case 'List':
561     hideItemById('editDiv');
562     hideItemById('preview');
563     showItemById('menuDiv');
564     break;
565     case 'Edit':
566     hideItemById('preview');
567     showItemById('editDiv');
568     showItemById('menuDiv');
569     break;
570     case 'Preview':
571     showItemById('preview');
572     hideItemById('editDiv');
573     showItemById('menuDiv');
574     break;
575     case 'Presentation':
576     hideItemById('preview');
577     hideItemById('editDiv');
578     hideItemById('menuDiv');
579     break;
580     default:
581     break;
582     }
583     }
584    
585     function hideItemById(id){
586     var itm = document.getElementById(id);
587     if(itm) itm.style.display='none';
588     }
589    
590     function showItemById(id){
591     var itm = document.getElementById(id);
592     if(itm) itm.style.display='block';
593     }
594    
595     function removeChildNodes(id){
596     var itm = document.getElementById(id);
597     if(!itm) return;
598    
599     var child = itm.firstChild;
600    
601     while(child){
602     itm.removeChild(child);
603     child = itm.firstChild;
604     }
605     }
606    
607     function chEditareaHeight(){
608     var itm = document.getElementById('editarea');
609     if(!itm) return;
610     var cv = itm.getAttribute('rows');
611     if(parseInt(cv) > editarea_h){
612     itm.setAttribute('rows', editarea_h);
613     }else{
614     itm.setAttribute('rows', editarea_h * 2);
615     }
616     }
617    
618     function format_file(str){
619     return "<pre>"+str+"</pre>";
620     }
621    
622     function previewFile(id, data){
623     var preview = document.getElementById(id);
624     removeChildNodes(id);
625    
626     preview.style.display='block';
627     preview.style.position='absolute';
628     preview.style.top='180px';
629     preview.style.width='80%';
630     preview.style.height='80%';
631    
632     var ele = toSVGElement(data, '100%','100%');
633    
634     preview.appendChild(ele);
635     }
636    
637     function toSVGElement(str, w, h){
638     var xmlsvg = "xmlns:svg=\"http://www.w3.org/2000/svg\""
639     var parser = new DOMParser();
640     var header = "<svg:svg width=\""+w+"\" height=\""+h+"\" "+xmlsvg+">"
641     var footer = "</svg:svg>";
642     var xmlDoc = parser.parseFromString(header+str+footer, "text/xml");
643     var xmlRoot = xmlDoc.documentElement;
644     var ele = document.importNode(xmlRoot,true);
645     return ele;
646     }
647    
648     ///-----------------
649     function insertNewSlide(){
650     var len = iSlide_content.length;
651     var editarea=document.getElementById('editarea');
652    
653     for(var i=len; i > currentPage+1;i--){
654     iSlide_content[i]=iSlide_content[i-1];
655     }
656     currentPage += 1;
657     iSlide_content[currentPage]="#slide\n\n----\n";
658     if(editarea) editarea.value = iSlide_content[currentPage];
659    
660     }
661    
662     function prevPage(){
663     var editarea=document.getElementById('editarea');
664     if(editarea) iSlide_content[currentPage]=editarea.value;
665    
666     if(editarea && currentPage > 0){
667     currentPage -= 1;
668     editarea.value=iSlide_content[currentPage];
669     updateEditMenu();
670     }
671     }
672    
673     function nextPage(){
674     var editarea=document.getElementById('editarea');
675     if(editarea) iSlide_content[currentPage]=editarea.value;
676    
677     if(editarea && iSlide_content.length-1 > currentPage){
678     currentPage += 1;
679     editarea.value=iSlide_content[currentPage];
680     updateEditMenu();
681     }
682     }
683    
684     function previewSlide(){
685     hideItemById('menuDiv');
686     hideItemById('preview');
687     hideItemById('editDiv');
688    
689     var editarea=document.getElementById('editarea');
690     if(editarea) iSlide_content[currentPage]=editarea.value;
691    
692     var ele = document.getElementById('presentation');
693    
694     var formatter = new iSlideFormatter();
695     ele.innerHTML = formatter.format(iSlide_content[currentPage]);
696    
697     iSlide_init();
698     modeEditFile();
699    
700     ele.style.display = 'block';
701     ele.style.top = 200;
702     }
703    
704     function escapeHTML(text) {
705 isao-hara 50 return text.replace( /[<>"&]/g,
706     function (m) { return { '<': '&lt;', '>': '&gt;', '"': '&quot;', '&': '&amp;' }[m]; });
707 isao-hara 2 };
708    
709     function previewCompileCode(){
710     var formatter = new iSlideFormatter();
711     var preview = document.getElementById("preview");
712     var editarea=document.getElementById('editarea');
713     if(editarea) iSlide_content[currentPage]=editarea.value;
714    
715     preview.innerHTML = "<pre>"+escapeHTML(formatter.format(iSlide_content[currentPage])) + "</pre>";
716     preview.style.display = "block";
717     }
718    
719     function updateEditMenu(){
720     var menuDiv = document.getElementById('menuDiv');
721     menuDiv.innerHTML="<h1 class='title'>" +document.title+"</h1>\n";
722     menuDiv.innerHTML+= "<button onClick=\"saveFile();fileSelector();\"> Save </button>\n";
723     menuDiv.innerHTML+= "<button onClick=\"fileSelector();\"> File List </button>\n";
724     menuDiv.innerHTML+= "<button onClick=\"previewSlide();\"> Preview </button>\n";
725     menuDiv.innerHTML+= "<button onClick=\"previewCompileCode();\"> toHTML </button>\n";
726     menuDiv.innerHTML+= "<button onClick=\"prevPage();\"> < </button>\n";
727     menuDiv.innerHTML+= currentPage+1;
728     menuDiv.innerHTML+= "<button onClick=\"nextPage();\"> > </button>\n";
729     menuDiv.innerHTML+= "<button onClick=\"startPresentation();\"> Presentation </button>\n";
730     menuDiv.style.display='block';
731     }
732    
733     function editSlideByIndex(idx){
734     currentPage = idx;
735     var datadiv = document.getElementById('tempdata');
736     name = datadiv.getAttribute('lfname');
737     editFile(name);
738     }
739    
740     // Show File
741     function startPresentation(){
742     hideItemById('menuDiv');
743     hideItemById('editDiv');
744    
745     var presentation = document.getElementById('presentation');
746     var formatter = new iSlideFormatter();
747    
748     var contents = "";
749     for(var i=0; i<iSlide_content.length ;i++ ){
750     contents += iSlide_content[i] +"\n";
751     }
752    
753     presentation.innerHTML="";
754     presentation.innerHTML+=formatter.format(contents);
755     presentation.style.display='block';
756    
757     setMode('Presentation');
758     iSlide_init();
759     setSlideIndex(currentPage);
760     }
761    
762    
763     ///// Formatter
764     //
765     var iSlideFormatter = function(){
766     this.slideNo = 0;
767     this.slideContent = null;
768     this.mode=null;
769     this.filters = new Array();
770     this.initFilter();
771     }
772    
773     iSlideFormatter.prototype.format = function(content){
774     var lines = content.split('\n');
775     var cont = "";
776     var str="";
777    
778     this.mode='start';
779    
780     for(var l=0; l<lines.length ;l++){
781     var line = lines[l];
782     var prev_mode = this.mode;
783    
784     if(line == "") {
785     this.mode=null;
786     }else{
787     for(var i=0; i<this.filters.length ;i++){
788     var pat = this.filters[i][0];
789     if(line.match(new RegExp(pat,"i"))){
790     var func = this.filters[i][1];
791     line = func.call(this, line, RegExp.lastMatch);
792     break;
793     }
794     }
795     }
796     if( prev_mode == this.mode){
797     if(line != ""){
798     str += line + '\n';
799     }
800     }else{
801     if( this.mode == 'title'){
802     str += line +'\n';
803     cont += str;
804     str = "";
805     }else{
806     if(prev_mode == 'title'){
807 isao-hara 20 if(trim(str)) str = "<h1>"+str+"</h1>\n";
808 isao-hara 2 }else{
809     }
810     if(str != "")
811     cont += str;
812     str = line;
813     if(line != "") str += '\n';
814     this.mode='content';
815     }
816     }
817     }
818    
819     cont += str;
820     if(this.slideNo > 0) {
821     if(this.slideContent != null) cont += "</div>\n";
822     cont += "</div>\n";
823     }
824     return cont;
825     }
826    
827     iSlideFormatter.prototype.parse = function(content){
828     var lines = content.split('\n');
829     var cont = new Array();
830    
831     var str = "";
832     for(var l=0; l<lines.length ;l++){
833     var line = lines[l];
834    
835     if(line.match(new RegExp("^#slide","i"))){
836     if(str != "") cont.push(trim(str));
837     str = line + "\n";
838     }else{
839     str += line + "\n";
840     }
841     }
842     if(str != "") cont.push(trim(str));
843    
844     if(cont.length < currentPage || currentPage < 0) currentPage = 0 ;
845    
846     return cont;
847     }
848    
849     iSlideFormatter.prototype.addPatternFilter = function(pat, func){
850     this.filters.push(new Array(pat,func));
851     }
852    
853     iSlideFormatter.prototype.initFilter = function(){
854     this.addPatternFilter('^#slide', this.new_slide);
855     this.addPatternFilter('^----', this.new_slidecontent);
856     this.addPatternFilter('^!+', this.tagH);
857     this.addPatternFilter('^\\.(ULi|OLi)\\s*', this.tagULi);
858     this.addPatternFilter('^\\.canvas\\s*', this.tagCanvas);
859     this.addPatternFilter('^\\.svg\\s*', this.tagSVG);
860     this.addPatternFilter('^\\.\\w+/\\s*', this.tagSelfClosing);
861     this.addPatternFilter('^\\.\\w+\\s*', this.tagHTML);
862     this.addPatternFilter('^\\,\\w+\\s*', this.tagClosingHTML);
863     this.addPatternFilter('^\\./\\w+', this.tagHTML);
864     }
865    
866     iSlideFormatter.prototype.tagH = function(l,m){
867     var n=m.length;
868     return "<h"+n+">"+l.substr(n)+"</h"+n+">";
869     }
870    
871     iSlideFormatter.prototype.tagULi = function(l,m){
872     var tag=trim(m.substr(1));
873     tag=tag.substr(0,tag.length-1);
874     this.mode =tag;
875    
876     return "<"+tag+" class=\"incremental\">";
877     }
878    
879     iSlideFormatter.prototype.tagHTML = function(l,m){
880     var tag=trim(m.substr(1));
881     this.mode =null;
882     var ele = l.substr(m.length);
883     var val = ele.split('//');
884     if(val.length == 2) {
885     return "<"+tag+" "+val[0]+">"+val[1];
886     }else{
887     return "<"+tag+">"+ele;
888     }
889     }
890    
891     iSlideFormatter.prototype.tagClosingHTML = function(l,m){
892     var tag=trim(m.substr(1));
893     this.mode =null;
894    
895     var ele = l.substr(m.length);
896     var val = ele.split('//');
897     if(val.length == 2) {
898     return "<"+tag+" "+val[0]+">"+val[1]+"</"+tag+">";
899     }else{
900     return "<"+tag+">"+ele+ "</"+tag+">";
901     }
902     }
903    
904     iSlideFormatter.prototype.tagCanvas = function(l,m){
905     var n=m.length;
906     var ele = trim(l.substr(n));
907     this.mode ='canvas';
908     var wh = ele.split(',');
909     if(wh.length == 2){
910     return "<canvas width=\""+wh[0]+"\" height=\""+wh[1]+"\">";
911     }else if(wh.length == 4){
912     return "<canvas class=\"abs\" style=\"top:"+wh[0]+";left:"+wh[1]+";\" width=\""+wh[2]+"\" height=\""+wh[3]+"\">";
913     }else{
914     return "<canvas "+ele+" >";
915     }
916     }
917    
918     iSlideFormatter.prototype.tagSVG = function(l,m){
919     var n=m.length;
920     var ele = trim(l.substr(n));
921 isao-hara 24
922 isao-hara 2 this.mode ='svg_image';
923     var args = ele.split(' ');
924 isao-hara 24
925 isao-hara 2 var fname = args[0];
926 isao-hara 24 var size = '800,600';
927 isao-hara 2
928 isao-hara 24 if(args.length > 1){ size = args[1]; }
929     var wh = size.split(',');
930    
931 isao-hara 2 if(wh.length == 2){
932     return "<object data=\"image.svg?name="+fname+"\" width=\""+wh[0]+"\" height=\""+wh[1]+"\" type=\"image/svg+xml\" />";
933     }else if(wh.length == 4){
934     return "<object data=\"image.svg?name="+fname+"\" width=\""+wh[0]+"\" height=\""+wh[1]+"\" type=\"image/svg+xml\" />";
935     }else{
936     return "<object data=\"image.svg?name="+fname+"\" type=\"image/svg+xml\" />";
937     }
938     }
939    
940     iSlideFormatter.prototype.tagSelfClosing = function(l,m){
941     var tag=m.substr(1, m.indexOf('/')-1);
942     this.mode =tag;
943    
944     return "<"+tag+" "+trim(l.substr(m.length))+" />";
945     }
946    
947     iSlideFormatter.prototype.new_slide = function(l,m){
948     var res = l.replace("#slide", "<div class=\"slide");
949     res += "\">";
950    
951     if(this.slideNo > 0){
952     if(this.slideContent != null) res = "</div>\n"+res;
953     res = "</div>\n"+res;
954     }
955    
956     this.slideNo += 1;
957     this.slideContent = null;
958     this.mode = 'title';
959     return res;
960     }
961    
962     iSlideFormatter.prototype.new_slidecontent = function(l,m){
963     this.slideContent = "";
964     this.mode='content';
965     return "<div class=\"slidecontent\">";
966     }
967    
968     ////////// Utils
969     function changeColor(item){
970     item.style.background='green';
971     item.style.color='white';
972     }
973    
974     function hideItemById(id){
975     var itm = document.getElementById(id);
976     if(itm) itm.style.display='none';
977     }
978    
979     function showItemById(id){
980     var itm = document.getElementById(id);
981     if(itm) itm.style.display='block';
982     }
983    
984     function trim(str){
985     return str.replace(/(^\s+)|(\s+$)/g, "");
986     }
987    
988    
989     //////// upload
990     function newXMLRequest(){
991     if(this.XMLHttpRequest){
992     return new XMLHttpRequest();
993     }else {
994     return new ActiveXObject("Microsoft.XMLHTTP");
995     }
996     }
997    
998     function createRequestData(data){
999     var str="dummpy=0";
1000     for (var i in data){
1001     str = str +"&"+ i +"="+encodeURIComponent(data[i]);
1002     }
1003     return str;
1004     }
1005    
1006     function postRequest(url, data, func){
1007    
1008     var postData=createRequestData(data);
1009     var obj=newXMLRequest();
1010    
1011     obj.onreadystatechange = function(){
1012     if (obj.readyState == 4 && obj.status == 200){
1013     func(obj.responseText);
1014     }
1015     }
1016    
1017     obj.open("POST", url, true);
1018     obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
1019     obj.send(postData);
1020     }
1021    
1022 isao-hara 16 function commnadFinishAlert(s){
1023 isao-hara 2 alert(s);
1024     }
1025    
1026     function uploadFileData(transaction, results){
1027     var data = results.rows.item(0);
1028 isao-hara 16 var updata = new Array(0);
1029     updata['name']= data['name'];
1030     updata['datalob'] = data['datablob'];
1031     updata['cmd'] = 'upload'
1032 isao-hara 2
1033 isao-hara 16 postRequest(MgrPath+iSlideMgr, updata, commnadFinishAlert);
1034 isao-hara 2 }
1035    
1036     function uploadFile(name){
1037     getFile(name, 'uploadFileData');
1038     }
1039    
1040     function getRemoteFileList(){
1041     var data=new Array();
1042     data['name'] = "Slide";
1043     data['cmd'] = "list";
1044 isao-hara 16 postRequest(MgrPath+iSlideMgr, data, fileSelectorOnServer);
1045 isao-hara 2 }
1046    
1047 isao-hara 16 function previewRemoteFile(content){
1048 isao-hara 67 currentPage = 0;
1049     getEditSlideContent(content)
1050    
1051     startPresentation();
1052    
1053     updateShowMenu();
1054     setMode('Presentation');
1055 isao-hara 16 setMode('Preview');
1056     }
1057 isao-hara 67
1058    
1059 isao-hara 2 function getRemoteFile(name){
1060     var data=new Array();
1061     data['name'] = name;
1062     data['cmd'] = "get";
1063 isao-hara 16 postRequest(MgrPath+iSlideMgr, data, previewRemoteFile);
1064 isao-hara 2 }
1065 isao-hara 16
1066     function downloadToFile(name){
1067     var downloadForm = "";
1068     downloadForm += "<form action=\"iSlideManager.php\" method=\"post\">";
1069     downloadForm += "<input type=\"hidden\" name=\"cmd\" value=\"download\">";
1070     downloadForm += "<input type=\"hidden\" name=\"name\" value=\""+name+"\">";
1071     downloadForm += "<input type=\"hidden\" name=\"filetype\" value=\"slide\">";
1072     downloadForm += "<input type=\"submit\">";
1073     downloadForm += "</form>";
1074    
1075     var cmdForm = document.getElementById('cmdForm');
1076     cmdForm.innerHTML = downloadForm;
1077     cmdForm.firstChild.submit();
1078     }
1079 isao-hara 67
1080     function showRemoteFile(name){
1081     getRemoteFile(name);
1082     }
1083    

Properties

Name Value
svn:executable *

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