commit
9efdb1ea91
@ -0,0 +1,133 @@ |
||||
'use strict' |
||||
|
||||
var EventManager = require('remix-lib').EventManager |
||||
|
||||
function FilesTree (name, storage) { |
||||
var self = this |
||||
var event = new EventManager() |
||||
this.event = event |
||||
this.type = name |
||||
this.structFile = '.' + name + '.tree' |
||||
this.tree = {} |
||||
|
||||
this.exists = function (path, cb) { |
||||
cb(null, this._exists(path)) |
||||
} |
||||
|
||||
function updateRefs (path, type) { |
||||
var split = path.split('/') // this should be unprefixed path
|
||||
var crawlpath = self.tree |
||||
var intermediatePath = '' |
||||
split.forEach((pathPart, index) => { |
||||
intermediatePath += pathPart |
||||
if (!crawlpath[pathPart]) crawlpath[intermediatePath] = {} |
||||
if (index < split.length - 1) { |
||||
crawlpath = crawlpath[intermediatePath] |
||||
intermediatePath += '/' |
||||
} else if (type === 'add') { |
||||
crawlpath[intermediatePath] = path |
||||
} else if (type === 'remove' && crawlpath[intermediatePath]) { |
||||
delete crawlpath[intermediatePath] |
||||
} |
||||
}) |
||||
storage.set(self.structFile, JSON.stringify(self.tree)) |
||||
} |
||||
|
||||
this._exists = function (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
return storage.exists(unprefixedpath) |
||||
} |
||||
|
||||
this.init = function (cb) { |
||||
var tree = storage.get(this.structFile) |
||||
this.tree = tree ? JSON.parse(tree) : {} |
||||
if (cb) cb() |
||||
} |
||||
|
||||
this.get = function (path, cb) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
var content = storage.get(unprefixedpath) |
||||
if (cb) { |
||||
cb(null, content) |
||||
} |
||||
return content |
||||
} |
||||
|
||||
this.set = function (path, content) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
updateRefs(unprefixedpath, 'add') |
||||
var exists = storage.exists(unprefixedpath) |
||||
if (!storage.set(unprefixedpath, content)) { |
||||
return false |
||||
} |
||||
if (!exists) { |
||||
event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) |
||||
} else { |
||||
event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) |
||||
} |
||||
return true |
||||
} |
||||
|
||||
this.addReadOnly = function (path, content) { |
||||
return this.set(path, content) |
||||
} |
||||
|
||||
this.isReadOnly = function (path) { |
||||
return false |
||||
} |
||||
|
||||
this.remove = function (path) { |
||||
var unprefixedpath = this.removePrefix(path) |
||||
updateRefs(unprefixedpath, 'remove') |
||||
if (!this._exists(unprefixedpath)) { |
||||
return false |
||||
} |
||||
if (!storage.remove(unprefixedpath)) { |
||||
return false |
||||
} |
||||
event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) |
||||
return true |
||||
} |
||||
|
||||
this.rename = function (oldPath, newPath, isFolder) { |
||||
var unprefixedoldPath = this.removePrefix(oldPath) |
||||
var unprefixednewPath = this.removePrefix(newPath) |
||||
updateRefs(unprefixedoldPath, 'remove') |
||||
updateRefs(unprefixednewPath, 'add') |
||||
if (storage.exists(unprefixedoldPath)) { |
||||
if (!storage.rename(unprefixedoldPath, unprefixednewPath)) { |
||||
return false |
||||
} |
||||
event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) |
||||
return true |
||||
} |
||||
return false |
||||
} |
||||
|
||||
this.resolveDirectory = function (path, callback) { |
||||
var self = this |
||||
if (path[0] === '/') path = path.substring(1) |
||||
if (!path) return callback(null, { [self.type]: { } }) |
||||
var tree = {} |
||||
path = self.removePrefix(path) |
||||
|
||||
var split = path.split('/') // this should be unprefixed path
|
||||
var crawlpath = self.tree |
||||
split.forEach((pathPart, index) => { |
||||
if (crawlpath[pathPart]) crawlpath = crawlpath[pathPart] |
||||
}) |
||||
|
||||
for (var item in crawlpath) { |
||||
tree[item] = { isDirectory: typeof crawlpath[item] !== 'string' } |
||||
} |
||||
callback(null, tree) |
||||
} |
||||
|
||||
this.removePrefix = function (path) { |
||||
path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path |
||||
if (path[0] === '/') return path.substring(1) |
||||
return path |
||||
} |
||||
} |
||||
|
||||
module.exports = FilesTree |
@ -0,0 +1,46 @@ |
||||
<!DOCTYPE html> |
||||
|
||||
<html> |
||||
<head> |
||||
<meta charset="utf-8"> |
||||
<!-- |
||||
The MIT License (MIT) |
||||
|
||||
Copyright (c) 2014, 2015, the individual contributors |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
--> |
||||
<meta http-equiv="X-UA-Compatible" content="chrome=1"> |
||||
<title></title> |
||||
<link rel="stylesheet" href="assets/css/pygment_trac.css"> |
||||
<link rel="stylesheet" href="assets/css/font-awesome.min.css"> |
||||
<script type="text/javascript" src="remix.js"></script> |
||||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> |
||||
</head> |
||||
<body> |
||||
<div>PLUGIN</div> |
||||
<input type="text" id="filename"></input> |
||||
<input type="text" id="valuetosend"></input> <br> |
||||
<input type="button" id="testmessageadd">add config</input> <br> |
||||
<input type="button" id="testmessageremove">remove config</input> <br> |
||||
<input type="button" id="testmessagerget">get config</input> <br> |
||||
<br> |
||||
<div id='compilationdata'></div> |
||||
</body> |
||||
</html> |
@ -0,0 +1,32 @@ |
||||
|
||||
function receiveMessage (event) { |
||||
console.log('receiveMessage', event.data, event.source, event.origin) |
||||
document.getElementById('compilationdata').innerHTML += event.data + '<br>' |
||||
} |
||||
window.addEventListener('message', receiveMessage, false) |
||||
|
||||
window.onload = function () { |
||||
document.querySelector('input#testmessageadd').addEventListener('click', function () { |
||||
window.parent.postMessage(JSON.stringify({ |
||||
type: 'setConfig', |
||||
arguments: [document.getElementById('filename').value, document.getElementById('valuetosend').value], |
||||
id: 34 |
||||
}), 'http://127.0.0.1:8080') |
||||
}) |
||||
|
||||
document.querySelector('input#testmessageremove').addEventListener('click', function () { |
||||
window.parent.postMessage(JSON.stringify({ |
||||
type: 'removeConfig', |
||||
arguments: [document.getElementById('filename').value], |
||||
id: 35 |
||||
}), 'http://127.0.0.1:8080') |
||||
}) |
||||
|
||||
document.querySelector('input#testmessagerget').addEventListener('click', function () { |
||||
window.parent.postMessage(JSON.stringify({ |
||||
type: 'getConfig', |
||||
arguments: [document.getElementById('filename').value], |
||||
id: 36 |
||||
}), 'http://127.0.0.1:8080') |
||||
}) |
||||
} |
Loading…
Reference in new issue