mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-12-04 23:39:57 +00:00
436 lines
70 KiB
HTML
436 lines
70 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html lang="en">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||
|
<title>jQuery.twFileDemo</title>
|
||
|
<style type="text/css">
|
||
|
#editButton {
|
||
|
float: left;
|
||
|
}
|
||
|
|
||
|
#error {
|
||
|
display: none;
|
||
|
float: left;
|
||
|
text-indent: 20px;
|
||
|
color: #F00;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<h1>jQuery.twFileDemo</h1>
|
||
|
<p>
|
||
|
This is an example file that can be used to test out the saving and loading system.
|
||
|
Press the edit button to view the page HTML and you can then make changes. Press save
|
||
|
to update the page.
|
||
|
</p>
|
||
|
<h2>Dummy Text</h2>
|
||
|
<p>
|
||
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris viverra nisl sagittis lorem
|
||
|
rutrum eu lacinia elit ultrices. In laoreet tortor in dolor dignissim vitae lacinia massa dignissim.
|
||
|
Sed luctus, tortor a aliquam vulputate, orci tellus vulasdasdputate risus, sed vehicula quam massa ac orci.
|
||
|
Cras in nibh purus, ac tempor est. In tincidunt nulla et velit accumsan non tempor massa vulputate.
|
||
|
Nunc condimentum aliquam ligulasdasda, vel tincidunt nunc fringilla eget. Ut arcu nisl, auctor viverra tempor ut,
|
||
|
dapibus et mi. Etiam porttitor, quam venenatis luctus porta, risus sapien rutrum ipsum, quis luctus sem mi
|
||
|
posuere urna. Duis interdum, justo quis sodales vulputate, mauris nisi hendrerit lacus,
|
||
|
porttitor consequat diam tortor ut lorem. Nullam in justo sit amet orci ornare porta quis nec nulla.
|
||
|
Vestibulum eu mattis arcu. In nulla purus, vestibulum id ornare placerat, sagittis quis lacus.
|
||
|
Aenean a eros eu sem pulvinar iaculis. Quisque molestie, sapien ac luctus tempus, eros dui posuere nulla,
|
||
|
quis euismod dolor nisl nec dolor. Duis laoreet varius fermentum. Pellentesque habitant morbi tristique
|
||
|
senectus et netus et malesuada fames ac turpis egestas. Mauris at nulla quam, et eleifend mi.
|
||
|
Maecenas magna arcu, posuere sit amet ullamcorper vehicula, ornare nec lectus. Curabitur nec interdum nisi.
|
||
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||
|
</p>
|
||
|
<h2>Tools</h2>
|
||
|
<input type="button" id="editButton" value="Edit">
|
||
|
<div id="error">Cannot load file</div>
|
||
|
|
||
|
<!-- jQuery -->
|
||
|
<script type="text/javascript">
|
||
|
/*
|
||
|
* jQuery JavaScript Library v1.3.2
|
||
|
* http://jquery.com/
|
||
|
*
|
||
|
* Copyright (c) 2009 John Resig
|
||
|
* Dual licensed under the MIT and GPL licenses.
|
||
|
* http://docs.jquery.com/License
|
||
|
*
|
||
|
* Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)
|
||
|
* Revision: 6246
|
||
|
*/
|
||
|
(function(){var l=this,g,y=l.jQuery,p=l.$,o=l.jQuery=l.$=function(E,F){return new o.fn.init(E,F)},D=/^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,f=/^.[^:#\[\.,]*$/;o.fn=o.prototype={init:function(E,H){E=E||document;if(E.nodeType){this[0]=E;this.length=1;this.context=E;return this}if(typeof E==="string"){var G=D.exec(E);if(G&&(G[1]||!H)){if(G[1]){E=o.clean([G[1]],H)}else{var I=document.getElementById(G[3]);if(I&&I.id!=G[3]){return o().find(E)}var F=o(I||[]);F.context=document;F.selector=E;return F}}else{return o(H).find(E)}}else{if(o.isFunction(E)){return o(document).ready(E)}}if(E.selector&&E.context){this.selector=E.selector;this.context=E.context}return this.setArray(o.isArray(E)?E:o.makeArray(E))},selector:"",jquery:"1.3.2",size:function(){return this.length},get:function(E){return E===g?Array.prototype.slice.call(this):this[E]},pushStack:function(F,H,E){var G=o(F);G.prevObject=this;G.context=this.context;if(H==="find"){G.selector=this.selector+(this.selector?" ":"")+E}else{if(H){G.selector=this.selector+"."+H+"("+E+")"}}return G},setArray:function(E){this.length=0;Array.prototype.push.apply(this,E);return this},each:function(F,E){return o.each(this,F,E)},index:function(E){return o.inArray(E&&E.jquery?E[0]:E,this)},attr:function(F,H,G){var E=F;if(typeof F==="string"){if(H===g){return this[0]&&o[G||"attr"](this[0],F)}else{E={};E[F]=H}}return this.each(function(I){for(F in E){o.attr(G?this.style:this,F,o.prop(this,E[F],G,I,F))}})},css:function(E,F){if((E=="width"||E=="height")&&parseFloat(F)<0){F=g}return this.attr(E,F,"curCSS")},text:function(F){if(typeof F!=="object"&&F!=null){return this.empty().append((this[0]&&this[0].ownerDocument||document).createTextNode(F))}var E="";o.each(F||this,function(){o.each(this.childNodes,function(){if(this.nodeType!=8){E+=this.nodeType!=1?this.nodeValue:o.fn.text([this])}})});return E},wrapAll:function(E){if(this[0]){var F=o(E,this[0].ownerDocument).clone();if(this[0].parentNode){F.insertBefore(this[0])}F.map(function(){var G=this;while(G.firstChild){G=G.firstChild}return G}).append(this)}return this},wrapInner:function(E){return this.each(function(){o(this).contents().wrapAll(E)})},wrap:function(E){return this.each(function(){o(this).wrapAll(E)})},append:function(){return this.domManip(arguments,true,function(E){if(this.nodeType==1){this.appendChild(E)}})},prepend:function(){return this.domManip(arguments,true,function(E){if(this.nodeType==1){this.insertBefore(E,this.firstChild)}})},before:function(){return this.domManip(arguments,false,function(E){this.parentNode.insertBefore(E,this)})},after:function(){return this.domManip(arguments,false,function(E){this.parentNode.insertBefore(E,this.nextSibling)})},end:function(){return this.prevObject||o([])},push:[].push,sort:[].sort,splice:[].splice,find:function(E){if(this.length===1){var F=this.pushStack([],"find",E);F.length=0;o.find(E,this[0],F);return F}else{return this.pushStack(o.unique(o.map(this,function(G){return o.find(E,G)})),"find",E)}},clone:function(G){var E=this.map(function(){if(!o.support.noCloneEvent&&!o.isXMLDoc(this)){var I=this.outerHTML;if(!I){var J=this.ownerDocument.createElement("div");J.appendChild(this.cloneNode(true));I=J.innerHTML}return o.clean([I.replace(/ jQuery\d+="(?:\d+|null)"/g,"").replace(/^\s*/,"")])[0]}else{return this.cloneNode(true)}});if(G===true){var H=this.find("*").andSelf(),F=0;E.find("*").andSelf().each(function(){if(this.nodeName!==H[F].nodeName){return}var I=o.data(H[F],"events");for(var K in I){for(var J in I[K]){o.event.add(this,K,I[K][J],I[K][J].data)}}F++})}return E},filter:function(E){return this.pushStack(o.isFunction(E)&&o.grep(this,function(G,F){return E.call(G,F)})||o.multiFilter(E,o.grep(this,function(F){return F.nodeType===1})),"filter",E)},closest:function(E){var G=o.expr.match.POS.test(E)?o(E):null,F=0;return this.map(function(){var H=this;while(H&&H.ownerDocument){if(G?G.index(H)>-1:o(H).is(E)){o.data(H,"closest",F);return H}H=H.parentNode;F++}})},not:function(E){if(typeof E==="string"){if(f.test(E)){return this.pushStack(o.multiFilter(E,this,true),"not",E)}else{E=o.multiFilter(E,thi
|
||
|
/*
|
||
|
* Sizzle CSS Selector Engine - v0.9.3
|
||
|
* Copyright 2009, The Dojo Foundation
|
||
|
* Released under the MIT, BSD, and GPL Licenses.
|
||
|
* More information: http://sizzlejs.com/
|
||
|
*/
|
||
|
(function(){var R=/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,L=0,H=Object.prototype.toString;var F=function(Y,U,ab,ac){ab=ab||[];U=U||document;if(U.nodeType!==1&&U.nodeType!==9){return[]}if(!Y||typeof Y!=="string"){return ab}var Z=[],W,af,ai,T,ad,V,X=true;R.lastIndex=0;while((W=R.exec(Y))!==null){Z.push(W[1]);if(W[2]){V=RegExp.rightContext;break}}if(Z.length>1&&M.exec(Y)){if(Z.length===2&&I.relative[Z[0]]){af=J(Z[0]+Z[1],U)}else{af=I.relative[Z[0]]?[U]:F(Z.shift(),U);while(Z.length){Y=Z.shift();if(I.relative[Y]){Y+=Z.shift()}af=J(Y,af)}}}else{var ae=ac?{expr:Z.pop(),set:E(ac)}:F.find(Z.pop(),Z.length===1&&U.parentNode?U.parentNode:U,Q(U));af=F.filter(ae.expr,ae.set);if(Z.length>0){ai=E(af)}else{X=false}while(Z.length){var ah=Z.pop(),ag=ah;if(!I.relative[ah]){ah=""}else{ag=Z.pop()}if(ag==null){ag=U}I.relative[ah](ai,ag,Q(U))}}if(!ai){ai=af}if(!ai){throw"Syntax error, unrecognized expression: "+(ah||Y)}if(H.call(ai)==="[object Array]"){if(!X){ab.push.apply(ab,ai)}else{if(U.nodeType===1){for(var aa=0;ai[aa]!=null;aa++){if(ai[aa]&&(ai[aa]===true||ai[aa].nodeType===1&&K(U,ai[aa]))){ab.push(af[aa])}}}else{for(var aa=0;ai[aa]!=null;aa++){if(ai[aa]&&ai[aa].nodeType===1){ab.push(af[aa])}}}}}else{E(ai,ab)}if(V){F(V,U,ab,ac);if(G){hasDuplicate=false;ab.sort(G);if(hasDuplicate){for(var aa=1;aa<ab.length;aa++){if(ab[aa]===ab[aa-1]){ab.splice(aa--,1)}}}}}return ab};F.matches=function(T,U){return F(T,null,null,U)};F.find=function(aa,T,ab){var Z,X;if(!aa){return[]}for(var W=0,V=I.order.length;W<V;W++){var Y=I.order[W],X;if((X=I.match[Y].exec(aa))){var U=RegExp.leftContext;if(U.substr(U.length-1)!=="\\"){X[1]=(X[1]||"").replace(/\\/g,"");Z=I.find[Y](X,T,ab);if(Z!=null){aa=aa.replace(I.match[Y],"");break}}}}if(!Z){Z=T.getElementsByTagName("*")}return{set:Z,expr:aa}};F.filter=function(ad,ac,ag,W){var V=ad,ai=[],aa=ac,Y,T,Z=ac&&ac[0]&&Q(ac[0]);while(ad&&ac.length){for(var ab in I.filter){if((Y=I.match[ab].exec(ad))!=null){var U=I.filter[ab],ah,af;T=false;if(aa==ai){ai=[]}if(I.preFilter[ab]){Y=I.preFilter[ab](Y,aa,ag,ai,W,Z);if(!Y){T=ah=true}else{if(Y===true){continue}}}if(Y){for(var X=0;(af=aa[X])!=null;X++){if(af){ah=U(af,Y,X,aa);var ae=W^!!ah;if(ag&&ah!=null){if(ae){T=true}else{aa[X]=false}}else{if(ae){ai.push(af);T=true}}}}}if(ah!==g){if(!ag){aa=ai}ad=ad.replace(I.match[ab],"");if(!T){return[]}break}}}if(ad==V){if(T==null){throw"Syntax error, unrecognized expression: "+ad}else{break}}V=ad}return aa};var I=F.selectors={order:["ID","NAME","TAG"],match:{ID:/#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,CLASS:/\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,NAME:/\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,ATTR:/\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,TAG:/^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,CHILD:/:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,POS:/:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,PSEUDO:/:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/},attrMap:{"class":"className","for":"htmlFor"},attrHandle:{href:function(T){return T.getAttribute("href")}},relative:{"+":function(aa,T,Z){var X=typeof T==="string",ab=X&&!/\W/.test(T),Y=X&&!ab;if(ab&&!Z){T=T.toUpperCase()}for(var W=0,V=aa.length,U;W<V;W++){if((U=aa[W])){while((U=U.previousSibling)&&U.nodeType!==1){}aa[W]=Y||U&&U.nodeName===T?U||false:U===T}}if(Y){F.filter(T,aa,true)}},">":function(Z,U,aa){var X=typeof U==="string";if(X&&!/\W/.test(U)){U=aa?U:U.toUpperCase();for(var V=0,T=Z.length;V<T;V++){var Y=Z[V];if(Y){var W=Y.parentNode;Z[V]=W.nodeName===U?W:false}}}else{for(var V=0,T=Z.length;V<T;V++){var Y=Z[V];if(Y){Z[V]=X?Y.parentNode:Y.parentNode===U}}if(X){F.filter(U,Z,true)}}},"":function(W,U,Y){var V=L++,T=S;if(!U.match(/\W/)){var X=U=Y?U:U.toUpperCase();T=P}T("parentNode",U,V,W,X,Y)},"~":function(W,U,Y){var V=L++,T=S;if(typeof U==="string"&&!U.match(/\W/)){var X=U=Y?U:U.toUpperCase();T=P}T("previousSibling",U,V,W,X,Y)}},find:{ID:function(U,V,W){if(typeof V.getElementById!=="undefined"&&!W){var T=V.getElementById(U[1]);return T?[T]:[]}},NA
|
||
|
</script>
|
||
|
<!-- plugin -->
|
||
|
<script type="text/javascript">
|
||
|
/*
|
||
|
jQuery.twFile.js
|
||
|
|
||
|
jQuery plugin for loading a file and saving data to a file
|
||
|
|
||
|
Copyright (c) UnaMesa Association 2009
|
||
|
|
||
|
Triple licensed under the BSD, MIT and GPL licenses:
|
||
|
http://www.opensource.org/licenses/bsd-license.php
|
||
|
http://www.opensource.org/licenses/mit-license.php
|
||
|
http://www.gnu.org/licenses/gpl.html
|
||
|
*/
|
||
|
|
||
|
|
||
|
(function($) {
|
||
|
if(!$.twFile) {
|
||
|
$.twFile = {};
|
||
|
}
|
||
|
|
||
|
$.extend($.twFile,{
|
||
|
currentDriver: null,
|
||
|
driverList: ["activeX", "mozilla", "tiddlySaver", "javaLiveConnect"],
|
||
|
|
||
|
// Loads the contents of a text file from the local file system
|
||
|
// filePath is the path to the file in these formats:
|
||
|
// x:\path\path\path\filename - PC local file
|
||
|
// \\server\share\path\path\path\filename - PC network file
|
||
|
// /path/path/path/filename - Mac/Unix local file
|
||
|
// returns the text of the file, or null if the operation cannot be performed or false if there was an error
|
||
|
load: function(filePath) {
|
||
|
return this.getDriver().loadFile(filePath);
|
||
|
},
|
||
|
// Saves a string to a text file on the local file system
|
||
|
// filePath is the path to the file in the format described above
|
||
|
// content is the string to save
|
||
|
// returns true if the file was saved successfully, or null if the operation cannot be performed or false if there was an error
|
||
|
save: function(filePath,content) {
|
||
|
return this.getDriver().saveFile(filePath,content);
|
||
|
},
|
||
|
// Copies a file on the local file system
|
||
|
// dest is the path to the destination file in the format described above
|
||
|
// source is the path to the source file in the format described above
|
||
|
// returns true if the file was copied successfully, or null if the operation cannot be performed or false if there was an error
|
||
|
copy: function(dest,source) {
|
||
|
if(this.getDriver().copyFile)
|
||
|
return this.currentDriver.copyFile(dest,source);
|
||
|
else
|
||
|
return null;
|
||
|
},
|
||
|
// Converts a local file path from the format returned by document.location into the format expected by this plugin
|
||
|
// url is the original URL of the file
|
||
|
// returns the equivalent local file path
|
||
|
convertUriToLocalPath: function (url) {
|
||
|
// Remove any location or query part of the URL
|
||
|
var originalPath = url.split("#")[0].split("?")[0];
|
||
|
// Convert file://localhost/ to file:///
|
||
|
if(originalPath.indexOf("file://localhost/") == 0)
|
||
|
originalPath = "file://" + originalPath.substr(16);
|
||
|
// Convert to a native file format
|
||
|
//# "file:///x:/path/path/path..." - pc local file --> "x:\path\path\path..."
|
||
|
//# "file://///server/share/path/path/path..." - FireFox pc network file --> "\\server\share\path\path\path..."
|
||
|
//# "file:///path/path/path..." - mac/unix local file --> "/path/path/path..."
|
||
|
//# "file://server/share/path/path/path..." - pc network file --> "\\server\share\path\path\path..."
|
||
|
var localPath;
|
||
|
if(originalPath.charAt(9) == ":") // PC local file
|
||
|
localPath = unescape(originalPath.substr(8)).replace(new RegExp("/","g"),"\\");
|
||
|
else if(originalPath.indexOf("file://///") == 0) // Firefox PC network file
|
||
|
localPath = "\\\\" + unescape(originalPath.substr(10)).replace(new RegExp("/","g"),"\\");
|
||
|
else if(originalPath.indexOf("file:///") == 0) // Mac/UNIX local file
|
||
|
localPath = unescape(originalPath.substr(7));
|
||
|
else if(originalPath.indexOf("file:/") == 0) // Mac/UNIX local file
|
||
|
localPath = unescape(originalPath.substr(5));
|
||
|
else if(originalPath.indexOf("//") == 0) // PC network file
|
||
|
localPath = "\\\\" + unescape(originalPath.substr(7)).replace(new RegExp("/","g"),"\\");
|
||
|
return localPath || originalPath;
|
||
|
},
|
||
|
|
||
|
// Private functions
|
||
|
|
||
|
// Returns a reference to the current driver
|
||
|
getDriver: function() {
|
||
|
if(this.currentDriver === null) {
|
||
|
for(var t=0; t<this.driverList.length; t++) {
|
||
|
if(this.currentDriver === null && drivers[this.driverList[t]].isAvailable && drivers[this.driverList[t]].isAvailable())
|
||
|
this.currentDriver = drivers[this.driverList[t]];
|
||
|
}
|
||
|
}
|
||
|
return this.currentDriver;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Deferred initialisation for any drivers that need it
|
||
|
$(function() {
|
||
|
for(var t in drivers) {
|
||
|
if(drivers[t].deferredInit)
|
||
|
drivers[t].deferredInit();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Private driver implementations for each browser
|
||
|
|
||
|
var drivers = {};
|
||
|
|
||
|
// Internet Explorer driver
|
||
|
|
||
|
drivers.activeX = {
|
||
|
name: "activeX",
|
||
|
isAvailable: function() {
|
||
|
try {
|
||
|
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||
|
} catch(ex) {
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
},
|
||
|
loadFile: function(filePath) {
|
||
|
// Returns null if it can't do it, false if there's an error, or a string of the content if successful
|
||
|
try {
|
||
|
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||
|
var file = fso.OpenTextFile(filePath,1);
|
||
|
var content = file.ReadAll();
|
||
|
file.Close();
|
||
|
} catch(ex) {
|
||
|
//# alert("Exception while attempting to load\n\n" + ex.toString());
|
||
|
return null;
|
||
|
}
|
||
|
return content;
|
||
|
},
|
||
|
createPath: function(path) {
|
||
|
//# Remove the filename, if present. Use trailing slash (i.e. "foo\bar\") if no filename.
|
||
|
var pos = path.lastIndexOf("\\");
|
||
|
if(pos!=-1)
|
||
|
path = path.substring(0,pos+1);
|
||
|
//# Walk up the path until we find a folder that exists
|
||
|
var scan = [path];
|
||
|
try {
|
||
|
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||
|
var parent = fso.GetParentFolderName(path);
|
||
|
while(parent && !fso.FolderExists(parent)) {
|
||
|
scan.push(parent);
|
||
|
parent = fso.GetParentFolderName(parent);
|
||
|
}
|
||
|
//# Walk back down the path, creating folders
|
||
|
for(i=scan.length-1;i>=0;i--) {
|
||
|
if(!fso.FolderExists(scan[i])) {
|
||
|
fso.CreateFolder(scan[i]);
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
} catch(ex) {
|
||
|
}
|
||
|
return false;
|
||
|
},
|
||
|
copyFile: function(dest,source) {
|
||
|
drivers.activeX.createPath(dest);
|
||
|
try {
|
||
|
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||
|
fso.GetFile(source).Copy(dest);
|
||
|
} catch(ex) {
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
},
|
||
|
saveFile: function(filePath,content) {
|
||
|
// Returns null if it can't do it, false if there's an error, true if it saved OK
|
||
|
drivers.activeX.createPath(filePath);
|
||
|
try {
|
||
|
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||
|
var file = fso.OpenTextFile(filePath,2,-1,0);
|
||
|
file.Write(content);
|
||
|
file.Close();
|
||
|
} catch (ex) {
|
||
|
return null;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// Mozilla driver
|
||
|
|
||
|
drivers.mozilla = {
|
||
|
name: "mozilla",
|
||
|
isAvailable: function() {
|
||
|
return !!window.Components;
|
||
|
},
|
||
|
loadFile: function(filePath) {
|
||
|
// Returns null if it can't do it, false if there's an error, or a string of the content if successful
|
||
|
if(window.Components) {
|
||
|
try {
|
||
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||
|
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
|
||
|
file.initWithPath(filePath);
|
||
|
if(!file.exists())
|
||
|
return null;
|
||
|
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
|
||
|
inputStream.init(file,0x01,00004,null);
|
||
|
var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
|
||
|
sInputStream.init(inputStream);
|
||
|
var contents = sInputStream.read(sInputStream.available());
|
||
|
sInputStream.close();
|
||
|
inputStream.close();
|
||
|
return contents;
|
||
|
} catch(ex) {
|
||
|
//# alert("Exception while attempting to load\n\n" + ex);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
},
|
||
|
saveFile: function(filePath,content) {
|
||
|
// Returns null if it can't do it, false if there's an error, true if it saved OK
|
||
|
if(window.Components) {
|
||
|
try {
|
||
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||
|
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
|
||
|
file.initWithPath(filePath);
|
||
|
if(!file.exists())
|
||
|
file.create(0,0664);
|
||
|
var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
|
||
|
out.init(file,0x20|0x02,00004,null);
|
||
|
out.write(content,content.length);
|
||
|
out.flush();
|
||
|
out.close();
|
||
|
return true;
|
||
|
} catch(ex) {
|
||
|
alert("Exception while attempting to save\n\n" + ex);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// TiddlySaver driver
|
||
|
|
||
|
drivers.tiddlySaver = {
|
||
|
name: "tiddlySaver",
|
||
|
deferredInit: function() {
|
||
|
if(!document.applets["TiddlySaver"] && !$.browser.mozilla && !$.browser.msie && document.location.toString().substr(0,5) == "file:") {
|
||
|
$(document.body).append("<applet style='position:absolute;left:-1px' name='TiddlySaver' code='TiddlySaver.class' archive='TiddlySaver.jar' width='1'height='1'></applet>");
|
||
|
}
|
||
|
},
|
||
|
isAvailable: function() {
|
||
|
return !!document.applets["TiddlySaver"];
|
||
|
},
|
||
|
loadFile: function(filePath) {
|
||
|
var r;
|
||
|
try {
|
||
|
if(document.applets["TiddlySaver"]) {
|
||
|
r = document.applets["TiddlySaver"].loadFile(javaUrlToFilename(filePath),"UTF-8");
|
||
|
return (r === undefined || r === null) ? null : String(r);
|
||
|
}
|
||
|
} catch(ex) {
|
||
|
}
|
||
|
return null;
|
||
|
},
|
||
|
saveFile: function(filePath,content) {
|
||
|
try {
|
||
|
if(document.applets["TiddlySaver"])
|
||
|
return document.applets["TiddlySaver"].saveFile(javaUrlToFilename(filePath),"UTF-8",content);
|
||
|
} catch(ex) {
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Java LiveConnect driver
|
||
|
|
||
|
drivers.javaLiveConnect = {
|
||
|
name: "javaLiveConnect",
|
||
|
isAvailable: function() {
|
||
|
return !!window.java && !!window.java.io && !!window.java.io.FileReader;
|
||
|
},
|
||
|
loadFile: function(filePath) {
|
||
|
var r;
|
||
|
var content = [];
|
||
|
try {
|
||
|
r = new java.io.BufferedReader(new java.io.FileReader(javaUrlToFilename(filePath)));
|
||
|
var line;
|
||
|
while((line = r.readLine()) != null)
|
||
|
content.push(new String(line));
|
||
|
r.close();
|
||
|
} catch(ex) {
|
||
|
return null;
|
||
|
}
|
||
|
return content.join("\n") + "\n";
|
||
|
},
|
||
|
saveFile: function(filePath,content) {
|
||
|
try {
|
||
|
var s = new java.io.PrintStream(new java.io.FileOutputStream(javaUrlToFilename(filePath)));
|
||
|
s.print(content);
|
||
|
s.close();
|
||
|
} catch(ex) {
|
||
|
return null;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Private utilities
|
||
|
|
||
|
function javaUrlToFilename(url) {
|
||
|
var f = "//localhost";
|
||
|
if(url.indexOf(f) == 0)
|
||
|
return url.substring(f.length);
|
||
|
var i = url.indexOf(":");
|
||
|
return i > 0 ? url.substring(i-1) : url;
|
||
|
}
|
||
|
|
||
|
})(jQuery);
|
||
|
</script>
|
||
|
<!-- demo code -->
|
||
|
<script type="text/javascript">
|
||
|
// Run on document load
|
||
|
jQuery(function() {
|
||
|
var $ = jQuery; // local alias
|
||
|
// When the edit button is clicked, open an editing box
|
||
|
// over the page to edit the html of the page. On save,
|
||
|
// the page reloads with the changes.
|
||
|
$('#editButton').click(function() {
|
||
|
var filepath = document.location.href; // Get the current file
|
||
|
filepath = $.twFile.convertUriToLocalPath(filepath); // Convert the path to a readable format
|
||
|
var text = $.twFile.load(filepath); // Load the file
|
||
|
// If the file loads succesfully create an editing element
|
||
|
if(text){
|
||
|
// Create a textarea
|
||
|
var textarea = $("<textarea></textarea>").css({
|
||
|
margin: "8px auto 0",
|
||
|
display: "block",
|
||
|
width: "90%",
|
||
|
height: "90%"
|
||
|
}).text(text);
|
||
|
// Create a save button
|
||
|
var dButton = $('<input type="button" value="Save">').click(function(){
|
||
|
// On click, write the value of the textarea to file
|
||
|
$.twFile.save(filepath, textarea.val());
|
||
|
// Reload the file
|
||
|
box.animate({ opacity: 0 }, function() {
|
||
|
window.location.reload();
|
||
|
});
|
||
|
});
|
||
|
// Create a div to contain the text area
|
||
|
var box = $("<div></div>").css({
|
||
|
position: "fixed",
|
||
|
textAlign: "center",
|
||
|
top:"0",
|
||
|
left: "0",
|
||
|
width: "100%",
|
||
|
height: "100%",
|
||
|
opacity: "0",
|
||
|
background: "black"
|
||
|
}).append(textarea).append(dButton);
|
||
|
$("body").append(box);
|
||
|
// Fade in
|
||
|
box.animate({ opacity: 1 });
|
||
|
} else {
|
||
|
// Show an error message on fail
|
||
|
$("#error").fadeIn();
|
||
|
}
|
||
|
})
|
||
|
});
|
||
|
</script>
|
||
|
</body>
|
||
|
|
||
|
</html>
|