2016-12-21 12:09:04 +00:00
|
|
|
/*\
|
2016-12-22 08:15:16 +00:00
|
|
|
title: $:/core/modules/savers/beaker.js
|
2016-12-21 12:09:04 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: saver
|
|
|
|
|
2016-12-22 08:15:16 +00:00
|
|
|
Saves files using the Beaker browser's (https://beakerbrowser.com) Dat protocol (https://datproject.org/)
|
2017-06-24 20:54:44 +00:00
|
|
|
Compatible with beaker >= V0.7.2
|
2016-12-21 12:09:04 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
2016-12-22 08:15:16 +00:00
|
|
|
Set up the saver
|
2016-12-21 12:09:04 +00:00
|
|
|
*/
|
2016-12-22 08:15:16 +00:00
|
|
|
var BeakerSaver = function(wiki) {
|
2016-12-21 12:09:04 +00:00
|
|
|
this.wiki = wiki;
|
|
|
|
};
|
|
|
|
|
2016-12-22 08:15:16 +00:00
|
|
|
BeakerSaver.prototype.save = function(text,method,callback) {
|
2017-06-24 21:30:26 +00:00
|
|
|
var dat = new DatArchive("" + window.location),
|
|
|
|
pathname = ("" + window.location.pathname).split("#")[0];
|
|
|
|
dat.stat(pathname).then(function(value) {
|
|
|
|
if(value.isDirectory()) {
|
|
|
|
pathname = pathname + "/index.html";
|
2016-12-29 16:45:47 +00:00
|
|
|
}
|
2017-06-24 21:30:26 +00:00
|
|
|
dat.writeFile(pathname,text,"utf8").then(function(value) {
|
2016-12-29 16:45:47 +00:00
|
|
|
callback(null);
|
|
|
|
},function(reason) {
|
|
|
|
callback("Beaker Saver Write Error: " + reason);
|
2017-06-24 20:54:44 +00:00
|
|
|
});
|
2016-12-21 12:09:04 +00:00
|
|
|
},function(reason) {
|
2016-12-29 16:45:47 +00:00
|
|
|
callback("Beaker Saver Stat Error: " + reason);
|
2016-12-21 12:09:04 +00:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Information about this saver
|
|
|
|
*/
|
2016-12-22 08:15:16 +00:00
|
|
|
BeakerSaver.prototype.info = {
|
|
|
|
name: "beaker",
|
2016-12-21 12:09:04 +00:00
|
|
|
priority: 3000,
|
|
|
|
capabilities: ["save", "autosave"]
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Static method that returns true if this saver is capable of working
|
|
|
|
*/
|
|
|
|
exports.canSave = function(wiki) {
|
2018-05-02 14:57:47 +00:00
|
|
|
return !!window.DatArchive && location.protocol==="dat:";
|
2016-12-21 12:09:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create an instance of this saver
|
|
|
|
*/
|
|
|
|
exports.create = function(wiki) {
|
2016-12-22 08:15:16 +00:00
|
|
|
return new BeakerSaver(wiki);
|
2016-12-21 12:09:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|