diff --git a/common/cloudfunc.js b/common/cloudfunc.js index b58c5720..c29fc853 100644 --- a/common/cloudfunc.js +++ b/common/cloudfunc.js @@ -1,288 +1,279 @@ -(function(global) { - 'use strict'; +'use strict'; + +const rendy = require('rendy'); + +module.exports = new CloudFuncProto(); + +function CloudFuncProto() { + const CloudFunc = this; + const Entity = new entityProto(); + var FS; - var rendy; + /* КОНСТАНТЫ (общие для клиента и сервера)*/ - if (typeof module === 'object' && module.exports) { - rendy = require('rendy'); - module.exports = new CloudFuncProto(); - } else { - rendy = window.rendy; - global.CloudFunc = new CloudFuncProto(); + /* название программы */ + this.NAME = 'Cloud Commander'; + + /* если в ссылке будет эта строка - в браузере js отключен */ + this.FS = FS = '/fs'; + + this.apiURL = '/api/v1'; + this.MAX_FILE_SIZE = 500 * 1024; + this.Entity = Entity; + + 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; + }; } - function CloudFuncProto() { - var CloudFunc = this; - var Entity = new entityProto(); - var FS; + this.formatMsg = function(msg, name, status) { + if (!status) + status = 'ok'; - /* КОНСТАНТЫ (общие для клиента и сервера)*/ + if (name) + name = '("' + name + '")'; + else + name = ''; - /* название программы */ - this.NAME = 'Cloud Commander'; + msg = msg + ': ' + status + name; - /* если в ссылке будет эта строка - в браузере js отключен */ - this.FS = FS = '/fs'; + return msg; + }; + + /** Функция возвращает заголовок веб страницы + * @pPath + */ + this.getTitle = function(path) { + if (!CloudFunc.Path) + CloudFunc.Path = '/'; - this.apiURL = '/api/v1'; - this.MAX_FILE_SIZE = 500 * 1024; - this.Entity = Entity; + return CloudFunc.NAME + ' - ' + (path || CloudFunc.Path); + }; + + /** Функция получает адреса каждого каталога в пути + * возвращаеться массив каталогов + * @param url - адрес каталога + */ + function getPathLink(url, prefix, template) { + var namesRaw, names, length, + pathHTML = '', + path = '/'; - 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; - }; - } + if (!url) + throw Error('url could not be empty!'); - this.formatMsg = function(msg, name, status) { - if (!status) - status = 'ok'; - - if (name) - name = '("' + name + '")'; - else - name = ''; - - msg = msg + ': ' + status + name; - - return msg; - }; + if (!template) + throw Error('template could not be empty!'); - /** Функция возвращает заголовок веб страницы - * @pPath - */ - this.getTitle = function(path) { - if (!CloudFunc.Path) - CloudFunc.Path = '/'; - - return CloudFunc.NAME + ' - ' + (path || CloudFunc.Path); - }; + namesRaw = url.split('/') + .slice(1, -1), - /** Функция получает адреса каждого каталога в пути - * возвращаеться массив каталогов - * @param url - адрес каталога - */ - function getPathLink(url, prefix, template) { - var namesRaw, names, length, - pathHTML = '', - path = '/'; + names = [].concat('/', namesRaw), + + length = names.length - 1; + + names.forEach(function(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 - информация о файлах + * + */ + 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, - return 'text-file'; + 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 'text-file'; + } + + function getAttribute(size) { + if (size === 'dir') + return ''; + + return 'target="_blank" '; + } + + function getSize(size) { + if (size === 'dir') + return '<dir>'; + + return size; + } + }