mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-28 18:23:35 +00:00
refactor(client) extendProto -> module.exports
This commit is contained in:
parent
95d3cc67cb
commit
9d67bae487
25 changed files with 486 additions and 1127 deletions
272
client/buffer.js
272
client/buffer.js
|
|
@ -1,142 +1,138 @@
|
|||
/* global Util */
|
||||
/* global DOM */
|
||||
/* global CloudCmd */
|
||||
|
||||
(function(Util, DOM, CloudCmd) {
|
||||
'use strict';
|
||||
'use strict';
|
||||
|
||||
const jonny = require('jonny');
|
||||
const exec = require('execon');
|
||||
|
||||
const Storage = require('./storage');
|
||||
const Dialog = require('./dialog');
|
||||
const DOM = require('./dom');
|
||||
|
||||
module.exports = new BufferProto(DOM, CloudCmd);
|
||||
|
||||
function BufferProto(DOM, CloudCmd) {
|
||||
const Info = DOM.CurrentInfo,
|
||||
CLASS = 'cut-file',
|
||||
COPY = 'copy',
|
||||
CUT = 'cut',
|
||||
TITLE = 'Buffer',
|
||||
|
||||
Buffer = {
|
||||
cut : callIfEnabled.bind(null, cut),
|
||||
copy : callIfEnabled.bind(null, copy),
|
||||
clear : callIfEnabled.bind(null, clear),
|
||||
paste : callIfEnabled.bind(null, paste)
|
||||
};
|
||||
|
||||
var DOMProto = Object.getPrototypeOf(DOM);
|
||||
|
||||
DOMProto.Buffer = new BufferProto(Util, DOM, CloudCmd);
|
||||
|
||||
function BufferProto(Util, DOM, CloudCmd) {
|
||||
var Storage = DOM.Storage,
|
||||
Info = DOM.CurrentInfo,
|
||||
json = Util.json,
|
||||
|
||||
CLASS = 'cut-file',
|
||||
|
||||
COPY = 'copy',
|
||||
CUT = 'cut',
|
||||
|
||||
TITLE = 'Buffer',
|
||||
|
||||
Buffer = {
|
||||
cut : callIfEnabled.bind(null, cut),
|
||||
copy : callIfEnabled.bind(null, copy),
|
||||
clear : callIfEnabled.bind(null, clear),
|
||||
paste : callIfEnabled.bind(null, paste)
|
||||
};
|
||||
|
||||
function showMessage(msg) {
|
||||
DOM.Dialog.alert(TITLE, msg);
|
||||
}
|
||||
|
||||
function getNames() {
|
||||
var files = DOM.getActiveFiles(),
|
||||
names = DOM.getFilenames(files);
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
function addCutClass() {
|
||||
var files = DOM.getActiveFiles();
|
||||
|
||||
files.forEach(function(element) {
|
||||
element.classList.add(CLASS);
|
||||
});
|
||||
}
|
||||
|
||||
function rmCutClass() {
|
||||
var files = DOM.getByClassAll(CLASS);
|
||||
|
||||
[]
|
||||
.slice.call(files)
|
||||
.forEach(function(element) {
|
||||
element.classList.remove(CLASS);
|
||||
});
|
||||
}
|
||||
|
||||
function callIfEnabled(callback) {
|
||||
var is = CloudCmd.config('buffer');
|
||||
|
||||
if (is)
|
||||
return callback();
|
||||
|
||||
showMessage('Buffer disabled in config!');
|
||||
}
|
||||
|
||||
function copy() {
|
||||
var Storage = DOM.Storage,
|
||||
names = getNames(),
|
||||
from = Info.dirPath;
|
||||
|
||||
clear();
|
||||
|
||||
if (names.length)
|
||||
Storage.remove(CUT)
|
||||
.set(COPY, {
|
||||
from : from,
|
||||
names: names
|
||||
});
|
||||
}
|
||||
|
||||
function cut() {
|
||||
var Storage = DOM.Storage,
|
||||
names = getNames(),
|
||||
from = Info.dirPath;
|
||||
|
||||
clear();
|
||||
|
||||
if (names.length) {
|
||||
addCutClass();
|
||||
|
||||
Storage
|
||||
.set(CUT, {
|
||||
from : from,
|
||||
names: names
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function clear() {
|
||||
Storage.remove(COPY)
|
||||
.remove(CUT);
|
||||
|
||||
rmCutClass();
|
||||
}
|
||||
|
||||
function paste() {
|
||||
var copy = Storage.get.bind(Storage, COPY),
|
||||
cut = Storage.get.bind(Storage, CUT);
|
||||
|
||||
Util.exec.parallel([copy, cut], function(error, cp, ct) {
|
||||
var opStr = cp ? 'copy' : 'move',
|
||||
opData = cp || ct,
|
||||
data = {},
|
||||
Operation = CloudCmd.Operation,
|
||||
msg = 'Path is same!',
|
||||
path = Info.dirPath;
|
||||
|
||||
if (!error && !cp && !ct)
|
||||
error = 'Buffer is empty!';
|
||||
|
||||
if (error) {
|
||||
showMessage(error);
|
||||
} else {
|
||||
data = json.parse(opData);
|
||||
data.to = path;
|
||||
|
||||
if (data.from === path) {
|
||||
showMessage(msg);
|
||||
} else {
|
||||
Operation.show(opStr, data);
|
||||
clear();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Buffer;
|
||||
function showMessage(msg) {
|
||||
Dialog.alert(TITLE, msg);
|
||||
}
|
||||
})(Util, DOM, CloudCmd);
|
||||
|
||||
function getNames() {
|
||||
var files = DOM.getActiveFiles(),
|
||||
names = DOM.getFilenames(files);
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
function addCutClass() {
|
||||
var files = DOM.getActiveFiles();
|
||||
|
||||
files.forEach(function(element) {
|
||||
element.classList.add(CLASS);
|
||||
});
|
||||
}
|
||||
|
||||
function rmCutClass() {
|
||||
var files = DOM.getByClassAll(CLASS);
|
||||
|
||||
[]
|
||||
.slice.call(files)
|
||||
.forEach(function(element) {
|
||||
element.classList.remove(CLASS);
|
||||
});
|
||||
}
|
||||
|
||||
function callIfEnabled(callback) {
|
||||
var is = CloudCmd.config('buffer');
|
||||
|
||||
if (is)
|
||||
return callback();
|
||||
|
||||
showMessage('Buffer disabled in config!');
|
||||
}
|
||||
|
||||
function copy() {
|
||||
const names = getNames();
|
||||
const from = Info.dirPath;
|
||||
|
||||
clear();
|
||||
|
||||
if (!names.length)
|
||||
return;
|
||||
|
||||
Storage.remove(CUT)
|
||||
.set(COPY, {
|
||||
from : from,
|
||||
names: names
|
||||
});
|
||||
}
|
||||
|
||||
function cut() {
|
||||
const names = getNames();
|
||||
const from = Info.dirPath;
|
||||
|
||||
clear();
|
||||
|
||||
if (!names.length)
|
||||
return;
|
||||
|
||||
addCutClass();
|
||||
|
||||
Storage
|
||||
.set(CUT, {
|
||||
from : from,
|
||||
names: names
|
||||
});
|
||||
}
|
||||
|
||||
function clear() {
|
||||
Storage.remove(COPY)
|
||||
.remove(CUT);
|
||||
|
||||
rmCutClass();
|
||||
}
|
||||
|
||||
function paste() {
|
||||
const copy = Storage.get.bind(Storage, COPY);
|
||||
const cut = Storage.get.bind(Storage, CUT);
|
||||
|
||||
exec.parallel([copy, cut], function(error, cp, ct) {
|
||||
var opStr = cp ? 'copy' : 'move',
|
||||
opData = cp || ct,
|
||||
data = {},
|
||||
Operation = CloudCmd.Operation,
|
||||
msg = 'Path is same!',
|
||||
path = Info.dirPath;
|
||||
|
||||
if (!error && !cp && !ct)
|
||||
error = 'Buffer is empty!';
|
||||
|
||||
if (error)
|
||||
return showMessage(error);
|
||||
|
||||
data = jonny.parse(opData);
|
||||
data.to = path;
|
||||
|
||||
if (data.from === path) {
|
||||
showMessage(msg);
|
||||
} else {
|
||||
Operation.show(opStr, data);
|
||||
clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Buffer;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue