From b8334d346332f78402a7a54fc767fc6c7d2c4fba Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 22 Mar 2017 15:10:08 +0200 Subject: [PATCH] feature(entity) add from cloudfunc --- server/cloudfunc.js | 469 ++++++++++++++++++++------------------------ server/entity.js | 32 +++ 2 files changed, 247 insertions(+), 254 deletions(-) create mode 100644 server/entity.js diff --git a/server/cloudfunc.js b/server/cloudfunc.js index c29fc853..5e062e0e 100644 --- a/server/cloudfunc.js +++ b/server/cloudfunc.js @@ -1,279 +1,240 @@ 'use strict'; const rendy = require('rendy'); +const Entity = require('./entity'); -module.exports = new CloudFuncProto(); +/* КОНСТАНТЫ (общие для клиента и сервера)*/ -function CloudFuncProto() { - const CloudFunc = this; - const Entity = new entityProto(); - var FS; +/* название программы */ +const NAME = 'Cloud Commander'; +const FS = '/fs'; + +let Path; + +module.exports.FS = FS; +module.exports.apiURL = '/api/v1'; +module.exports.MAX_FILE_SIZE = 500 * 1024; +module.exports.Entity = Entity; + +module.exports.formatMsg = (msg, name, status = 'ok') => { + if (name) + name = '("' + name + '")'; + else + name = ''; - /* КОНСТАНТЫ (общие для клиента и сервера)*/ + return msg + ': ' + status + name; +}; + +/** + * Функция возвращает заголовок веб страницы + * @path + */ +module.exports.getTitle = (path) => { + if (!Path) + Path = '/'; - /* название программы */ - this.NAME = 'Cloud Commander'; + return NAME + ' - ' + (path || Path); +}; + +/** Функция получает адреса каждого каталога в пути + * возвращаеться массив каталогов + * @param url - адрес каталога + */ +function getPathLink(url, prefix, template) { + var namesRaw, names, length, + pathHTML = '', + path = '/'; - /* если в ссылке будет эта строка - в браузере js отключен */ - this.FS = FS = '/fs'; + if (!url) + throw Error('url could not be empty!'); - this.apiURL = '/api/v1'; - this.MAX_FILE_SIZE = 500 * 1024; - this.Entity = Entity; + if (!template) + throw Error('template could not be empty!'); - function entityProto() { - var Entities = { - ' ': ' ', - '<' : '<', - '>' : '>' - }; - - var keys = Object.keys(Entities); - - this.encode = function(str) { - keys.forEach(function(code) { - var char = Entities[code]; - var reg = RegExp(char, 'g'); - - str = str.replace(reg, code); - }); - - return str; - }; - - this.decode = function(str) { - keys.forEach(function(code) { - var char = Entities[code]; - var reg = RegExp(code, 'g'); - - str = str.replace(reg, char); - }); - - return str; - }; - } + namesRaw = url.split('/') + .slice(1, -1), - this.formatMsg = function(msg, name, status) { - if (!status) - status = 'ok'; - - if (name) - name = '("' + name + '")'; - else - name = ''; - - msg = msg + ': ' + status + name; - - return msg; - }; + names = [].concat('/', namesRaw), - /** Функция возвращает заголовок веб страницы - * @pPath - */ - this.getTitle = function(path) { - if (!CloudFunc.Path) - CloudFunc.Path = '/'; - - return CloudFunc.NAME + ' - ' + (path || CloudFunc.Path); - }; + length = names.length - 1; - /** Функция получает адреса каждого каталога в пути - * возвращаеться массив каталогов - * @param url - адрес каталога - */ - function getPathLink(url, prefix, template) { - var namesRaw, names, length, - pathHTML = '', - path = '/'; + names.forEach((name, index) => { + var slash = '', + isLast = index === length; - if (!url) - throw Error('url could not be empty!'); + if (index) + path += name + '/'; - if (!template) - throw Error('template could not be empty!'); - - namesRaw = url.split('/') - .slice(1, -1), - - names = [].concat('/', namesRaw), - - length = names.length - 1; - - names.forEach(function(name, index) { - var slash = '', - isLast = index === length; - + if (index && isLast) { + pathHTML += name + '/'; + } else { if (index) - path += name + '/'; + slash = '/'; - if (index && isLast) { - pathHTML += name + '/'; - } else { - if (index) - slash = '/'; - - pathHTML += rendy(template, { - path: path, - name: name, - slash: slash, - prefix: prefix - }); - } - }); - - return pathHTML; - } - - /** - * Функция строит таблицу файлв из JSON-информации о файлах - * @param params - информация о файлах - * - */ - this.buildFromJSON = function(params) { - var attribute, - dotDot, link, dataName, - linkResult, - prefix = params.prefix, - template = params.template, - templateFile = template.file, - templateLink = template.link, - json = params.data, - files = json.files, - path = json.path, - - sort = params.sort || 'name', - order = params.order || 'asc', - - /* - * Строим путь каталога в котором мы находимся - * со всеми подкаталогами - */ - htmlPath = getPathLink(path, prefix, template.pathLink); - - var fileTable = rendy(template.path, { - link : prefix + FS + path, - fullPath : path, - path : htmlPath - }); - - var name = 'name'; - var size = 'size'; - var date = 'date'; - var arrow = order === 'asc' ? '↑' : '↓'; - - if (sort === 'name' && order !== 'asc') - name += arrow; - else if (sort === 'size') - size += arrow; - else if (sort === 'date') - date += arrow; - - var header = rendy(templateFile, { - tag : 'div', - attribute : 'data-name="js-fm-header" ', - className : 'fm-header', - type : '', - name : name, - size : size, - date : date, - owner : 'owner', - mode : 'mode' - }); - - /* сохраняем путь */ - CloudFunc.Path = path; - - fileTable += header + ''; - - return fileTable; - }; + }); - function getType(size) { - if (size === 'dir') - return 'directory'; + return pathHTML; +} + +/** + * Функция строит таблицу файлв из JSON-информации о файлах + * @param params - информация о файлах + * + */ +module.exports.buildFromJSON = (params) => { + var attribute, + dotDot, link, dataName, + linkResult, + prefix = params.prefix, + template = params.template, + templateFile = template.file, + templateLink = template.link, + json = params.data, + files = json.files, + path = json.path, - return 'text-file'; + sort = params.sort || 'name', + order = params.order || 'asc', + + /* + * Строим путь каталога в котором мы находимся + * со всеми подкаталогами + */ + htmlPath = getPathLink(path, prefix, template.pathLink); + + let fileTable = rendy(template.path, { + link : prefix + FS + path, + fullPath : path, + path : htmlPath + }); + + let name = 'name'; + let size = 'size'; + let date = 'date'; + const owner = 'owner'; + const mode = 'mode'; + const arrow = order === 'asc' ? '↑' : '↓'; + + if (sort === 'name' && order !== 'asc') + name += arrow; + else if (sort === 'size') + size += arrow; + else if (sort === 'date') + date += arrow; + + const header = rendy(templateFile, { + tag : 'div', + attribute : 'data-name="js-fm-header" ', + className : 'fm-header', + type : '', + name, + size, + date, + owner, + mode, + }); + + /* сохраняем путь */ + Path = path; + + fileTable += header + ''; + + return fileTable; +}; + +function getType(size) { + if (size === 'dir') + return 'directory'; + + return 'text-file'; +} + +function getAttribute(size) { + if (size === 'dir') + return ''; + + return 'target="_blank" '; +} + +function getSize(size) { + if (size === 'dir') + return '<dir>'; + + return size; +} + diff --git a/server/entity.js b/server/entity.js new file mode 100644 index 00000000..34bcd966 --- /dev/null +++ b/server/entity.js @@ -0,0 +1,32 @@ +'use strict'; + +const Entities = { + ' ': ' ', + '<' : '<', + '>' : '>' +}; + +const keys = Object.keys(Entities); + +module.exports.encode = (str) => { + keys.forEach((code) => { + const char = Entities[code]; + const reg = RegExp(char, 'g'); + + str = str.replace(reg, code); + }); + + return str; +}; + +module.exports.decode = (str) => { + keys.forEach((code) => { + const char = Entities[code]; + const reg = RegExp(code, 'g'); + + str = str.replace(reg, char); + }); + + return str; +}; +