From 44531adfc6ead3fd7ddbcac37e5f4bd1df07a596 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 16 Jul 2015 08:24:11 -0400 Subject: [PATCH] feature(directory) add ability to upload directories via drag n drop in Chrome --- bower.json | 3 +- lib/client/cloudcmd.js | 3 +- lib/client/directory.js | 116 +++++++++++++++++++++++ lib/client/listeners.js | 9 +- modules/emitify/.bower.json | 35 +++++++ modules/emitify/ChangeLog | 47 ++++++++++ modules/emitify/LICENSE | 21 +++++ modules/emitify/README.md | 63 +++++++++++++ modules/emitify/bower.json | 26 ++++++ modules/emitify/lib/emitify.js | 82 ++++++++++++++++ modules/emitify/package.json | 23 +++++ modules/findit/.bower.json | 38 ++++++++ modules/findit/ChangeLog | 39 ++++++++ modules/findit/LICENSE | 21 +++++ modules/findit/README.md | 55 +++++++++++ modules/findit/bower.json | 28 ++++++ modules/findit/example/index.html | 39 ++++++++ modules/findit/lib/findit.js | 60 ++++++++++++ modules/findit/package.json | 27 ++++++ modules/philip/.bower.json | 38 ++++++++ modules/philip/ChangeLog | 11 +++ modules/philip/LICENSE | 21 +++++ modules/philip/README.md | 77 +++++++++++++++ modules/philip/bower.json | 28 ++++++ modules/philip/example/index.html | 55 +++++++++++ modules/philip/lib/philip.js | 149 ++++++++++++++++++++++++++++++ modules/philip/package.json | 27 ++++++ 27 files changed, 1137 insertions(+), 4 deletions(-) create mode 100644 lib/client/directory.js create mode 100644 modules/emitify/.bower.json create mode 100644 modules/emitify/ChangeLog create mode 100644 modules/emitify/LICENSE create mode 100644 modules/emitify/README.md create mode 100644 modules/emitify/bower.json create mode 100644 modules/emitify/lib/emitify.js create mode 100644 modules/emitify/package.json create mode 100644 modules/findit/.bower.json create mode 100644 modules/findit/ChangeLog create mode 100644 modules/findit/LICENSE create mode 100644 modules/findit/README.md create mode 100644 modules/findit/bower.json create mode 100644 modules/findit/example/index.html create mode 100644 modules/findit/lib/findit.js create mode 100644 modules/findit/package.json create mode 100644 modules/philip/.bower.json create mode 100644 modules/philip/ChangeLog create mode 100644 modules/philip/LICENSE create mode 100644 modules/philip/README.md create mode 100644 modules/philip/bower.json create mode 100644 modules/philip/example/index.html create mode 100644 modules/philip/lib/philip.js create mode 100644 modules/philip/package.json diff --git a/bower.json b/bower.json index 347fbc13..b45e2efc 100644 --- a/bower.json +++ b/bower.json @@ -29,6 +29,7 @@ "github": "~0.10.6", "vk-openapi": "~0.0.1", "domtokenlist-shim": "~1.1.0", - "promise-polyfill": "~2.1.0" + "promise-polyfill": "~2.1.0", + "philip": "~1.1.1" } } diff --git a/lib/client/cloudcmd.js b/lib/client/cloudcmd.js index 52e766d2..5fd745ba 100644 --- a/lib/client/cloudcmd.js +++ b/lib/client/cloudcmd.js @@ -23,7 +23,8 @@ var CloudCmd; 'client', client + 'buffer', client + 'listeners', - client + 'key' + client + 'key', + client + 'directory' ].map(function(name) { return lib + name; }), diff --git a/lib/client/directory.js b/lib/client/directory.js new file mode 100644 index 00000000..4af2aad9 --- /dev/null +++ b/lib/client/directory.js @@ -0,0 +1,116 @@ +/* global CloudCmd */ +/* global DOM */ +/* global CloudFunc */ + +(function() { + 'use strict'; + + if (typeof module !== 'undefined' && module.exports) + module.exports = uploadDirectory; + else + DOM.uploadDirectory = uploadDirectory; + + function uploadDirectory(items) { + var entries, + Images = DOM.Images, + Info = DOM.CurrentInfo, + load = DOM.load, + url = '', + array = [ + 'findit', + 'philip' + ]; + + Images.show('top'); + + entries = [].map.call(items, function(item) { + return item.webkitGetAsEntry(); + }); + + if (!window.Emitify) + array.unshift('emitify'); + + array = array.map(function(name) { + var result = [ + '/modules/' + name, + '/lib/' + name, + '.js' + ].join(''); + + return result; + }); + + if (!window.fetch) + array.unshift('/modules/fetch/fetch.js'); + + url = CloudCmd.join(array); + + load.js(url, function() { + var path = Info.dirPath + .replace(/\/$/, ''), + + uploader; + + uploader = window.philip(entries, function(type, name, data, callback) { + var full = path + name; + switch(type) { + case 'file': + uploadFile(full, data, callback); + break; + + case 'directory': + uploadDir(full, callback); + break; + } + }); + + uploader.on('error', function(error) { + alert(error); + uploader.abort(); + }); + + uploader.on('progress', function(count) { + Images.setProgress(count); + Images.show('top'); + }); + + uploader.on('end', function() { + CloudCmd.refresh(); + }); + }); + } + + function uploadFile(url, data, callback) { + upload(url, data, callback); + } + + function uploadDir(url, callback) { + upload(url + '?dir', null, callback); + } + + function upload(url, body, callback) { + var prefix = CloudCmd.PREFIX, + apiURL = prefix + CloudFunc.apiURL, + api = apiURL + '/fs', + xhr = new XMLHttpRequest(); + + url = encodeURI(url); + url = url.replace('#', '%23'); + + xhr.open('put', api + url, true); + xhr.onreadystatechange = function() { + var error, + over = xhr.readyState === 4, + OK = 200; + + if (over) { + if (xhr.status !== OK) + error = Error(xhr.responseText); + + callback(error); + } + }; + xhr.send(body); + } + +})(); diff --git a/lib/client/listeners.js b/lib/client/listeners.js index 89c15384..b6adae8f 100644 --- a/lib/client/listeners.js +++ b/lib/client/listeners.js @@ -265,10 +265,15 @@ var Util, DOM, CloudFunc, CloudCmd; }); }, onDrop = function(event) { - var files = event.dataTransfer.files; + var files = event.dataTransfer.files, + items = event.dataTransfer.items; event.preventDefault(); - DOM.uploadFiles(files); + + if (items && items[0].webkitGetAsEntry) + DOM.uploadDirectory(items); + else + DOM.uploadFiles(files); }, /** * In Mac OS Chrome dropEffect = 'none' diff --git a/modules/emitify/.bower.json b/modules/emitify/.bower.json new file mode 100644 index 00000000..19eeaf10 --- /dev/null +++ b/modules/emitify/.bower.json @@ -0,0 +1,35 @@ +{ + "name": "emitify", + "version": "1.2.0", + "homepage": "https://github.com/coderaiser/emitify", + "authors": [ + "coderaiser " + ], + "description": "Dead simple event emitter", + "main": "lib/emitify.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "event", + "emitter" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "_release": "1.2.0", + "_resolution": { + "type": "version", + "tag": "v1.2.0", + "commit": "5dd1685840ba4905c7fcbd7b00202bed4baeb1ad" + }, + "_source": "git://github.com/coderaiser/emitify.git", + "_target": "~1.2.0", + "_originalSource": "emitify" +} \ No newline at end of file diff --git a/modules/emitify/ChangeLog b/modules/emitify/ChangeLog new file mode 100644 index 00000000..babfb9e0 --- /dev/null +++ b/modules/emitify/ChangeLog @@ -0,0 +1,47 @@ +2015.06.16, v1.2.0 + +feature: +- (emitify) add instanceof check + + +2015.02.24, v1.1.2 + +fix: +- (emitify) once: do not remove callback after emit + + +2015.02.11, v1.1.1 + +fix: +- (emitify) emit throw: data -> args[0] + +feature: +- (package) v1.1.0 + + +2015.02.11, v1.1.0 + +feature: +- (emitify) emit: add ability to use more then two arguments + + +2015.02.10, v1.0.3 + +fix: +- (emitify) "event should be function" + + +2015.02.10, v1.0.2 + +feature: +- (emitify) add arguments check + + +2015.02.10, v1.0.1 + +fix: +- (emitify) off: index do not changed + +feature: +- (bower) add + diff --git a/modules/emitify/LICENSE b/modules/emitify/LICENSE new file mode 100644 index 00000000..a103b669 --- /dev/null +++ b/modules/emitify/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 coderaiser + +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. diff --git a/modules/emitify/README.md b/modules/emitify/README.md new file mode 100644 index 00000000..109f3983 --- /dev/null +++ b/modules/emitify/README.md @@ -0,0 +1,63 @@ +# Emitify + +Dead simple event emitter. + +## Install + +``` +npm i emitify --save +bower i emitify --save +``` + +## API + +What you should do first is create new instance of `emitify` with + +```js +var emitify = Emitify(); +``` + +Than you could just use API as it is. + +### emitter.on(event, callback) + +Add `callback` listener to `event`. + +### emitter.off(event, callback) + +Remove `callback` listener from `event`. + +### emitter.emit(event [, data1, data2, ..., dataN]) + +Emit `event` with (or without) data. + +### emitter.addListener(event, callback) + +Alias to `emitter.on`. + +### emitter.removeListener(event, callback) + +Alias to `emitter.off`. + +## How to use? + +```js +var Emitify = require('emitify'), + emitter = new Emitify(), + log = function(data) { + console.log(data); + }); + +emitter.on('data', log); + +emitter.emit('data', 'hello'); +// result +'hello' + +emitter.off('data', log); + +``` + +## License + +MIT diff --git a/modules/emitify/bower.json b/modules/emitify/bower.json new file mode 100644 index 00000000..f7b05532 --- /dev/null +++ b/modules/emitify/bower.json @@ -0,0 +1,26 @@ +{ + "name": "emitify", + "version": "1.2.0", + "homepage": "https://github.com/coderaiser/emitify", + "authors": [ + "coderaiser " + ], + "description": "Dead simple event emitter", + "main": "lib/emitify.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "event", + "emitter" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/modules/emitify/lib/emitify.js b/modules/emitify/lib/emitify.js new file mode 100644 index 00000000..edf9b895 --- /dev/null +++ b/modules/emitify/lib/emitify.js @@ -0,0 +1,82 @@ +(function(global) { + 'use strict'; + + if (typeof module !== 'undefined' && module.exports) + module.exports = Emitify; + else + global.Emitify = Emitify; + + function Emitify() { + if (this instanceof Emitify) + this._all = {}; + else + return new Emitify(); + } + + Emitify.prototype._check = function(event, callback) { + var isTwo = arguments.length === 2; + + if (typeof event !== 'string') + throw(Error('event should be string!')); + + if (isTwo && typeof callback !== 'function') + throw(Error('callback should be function!')); + }; + + Emitify.prototype.on = function(event, callback) { + var funcs = this._all[event]; + + this._check(event, callback); + + if (funcs) + funcs.push(callback); + else + this._all[event] = [callback]; + + return this; + }; + + Emitify.prototype.addListener = + Emitify.prototype.on; + + Emitify.prototype.once = function(event, callback) { + var self = this; + + self._check(event, callback); + + self.on(event, function fn() { + callback(); + self.off(event, fn); + }); + }; + + Emitify.prototype.off = function(event, callback) { + var events = this._all[event] || [], + index = events.indexOf(callback); + + this._check(event, callback); + + while (~index) { + events.splice(index, 1); + index = events.indexOf(callback); + } + }; + + Emitify.prototype.removeListener = + Emitify.prototype.off; + + Emitify.prototype.emit = function(event) { + var args = [].slice.call(arguments, 1), + funcs = this._all[event]; + + this._check(event); + + if (funcs) + funcs.forEach(function(fn) { + fn.apply(null, args); + }); + else if (event === 'error') + throw args[0]; + }; + +})(this); diff --git a/modules/emitify/package.json b/modules/emitify/package.json new file mode 100644 index 00000000..e1eb9cd1 --- /dev/null +++ b/modules/emitify/package.json @@ -0,0 +1,23 @@ +{ + "name": "emitify", + "version": "1.2.0", + "description": "dead simple event emitter", + "main": "lib/emitify.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git://github.com/coderaiser/emitify.git" + }, + "keywords": [ + "event", + "emitter" + ], + "author": "coderaiser (http://coderaiser.github.io/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/coderaiser/emitify/issues" + }, + "homepage": "https://github.com/coderaiser/emitify" +} diff --git a/modules/findit/.bower.json b/modules/findit/.bower.json new file mode 100644 index 00000000..24094552 --- /dev/null +++ b/modules/findit/.bower.json @@ -0,0 +1,38 @@ +{ + "name": "findit", + "homepage": "https://github.com/coderaiser/domfs-findit", + "authors": [ + "coderaiser " + ], + "description": "Walk a directory tree in DOM File System", + "main": "lib/findit.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "findit", + "DOM", + "File System" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "modules" + ], + "dependencies": { + "emitify": "~1.2.0" + }, + "version": "1.1.2", + "_release": "1.1.2", + "_resolution": { + "type": "version", + "tag": "v1.1.2", + "commit": "c311a97563fccb55df3129a3f93f40c0ba08aaf5" + }, + "_source": "git://github.com/coderaiser/domfs-findit.git", + "_target": "~1.1.2", + "_originalSource": "findit", + "_direct": true +} \ No newline at end of file diff --git a/modules/findit/ChangeLog b/modules/findit/ChangeLog new file mode 100644 index 00000000..1ec13ac5 --- /dev/null +++ b/modules/findit/ChangeLog @@ -0,0 +1,39 @@ +2015.07.16, v1.1.2 + +fix: +- (findit) find -> self._find + + +2015.07.15, v1.1.1 + +fix: +- (dindit) enry -> entry + +feature: +- (package) main: domfs-findit -> findit + + +2015.07.14, v1.1.0 + +fix: +- (findit) dirs, first could be overwriting + + +2015.07.14, v1.0.3 + +fix: +- (findit) no end when entry file + + +2015.07.14, v1.0.2 + +fix: +- (findit) dirs could be more then 0 + + +2015.07.13, v1.0.1 + +fix: +- (findit) return +- (example) add () + diff --git a/modules/findit/LICENSE b/modules/findit/LICENSE new file mode 100644 index 00000000..a103b669 --- /dev/null +++ b/modules/findit/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 coderaiser + +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. diff --git a/modules/findit/README.md b/modules/findit/README.md new file mode 100644 index 00000000..88adde9e --- /dev/null +++ b/modules/findit/README.md @@ -0,0 +1,55 @@ +# DOM File System Findit + +Similar to [node-findit](https://github.com/substack/node-findit "Node Findit") but for [Dom File System](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem "Dom File System"). + +## Install + +``` +bower i findit --save +``` + +## How to use? + +Add `findit.js` and [emitify](https://github.com/coderaiser/emitify "Emitify"). +Or any other node-compitable [EventEmitter](https://iojs.org/api/events.html "Events") (set `window.Emitify = your_emitter` before using `findit`). + +```html + + +``` + +```js +var node = window; + +node.addEventListener('drop', function (e) { + var entry, + finder, + item = e.dataTransfer.items[0]; + + e.preventDefault(); + + entry = item.webkitGetAsEntry(); + + finder = findit(entry); + + finder.on('file', function(file, entry) { + console.log('file: ', file, entry); + }); + + finder.on('directory', function(file, entry) { + console.log('directory: ', file, entry); + }) + + finder.on('end', function() { + console.log('done'); + }) +}); + +node.addEventListener('dragover', function (e) { + e.preventDefault(); +}); +``` + +## License + +MIT diff --git a/modules/findit/bower.json b/modules/findit/bower.json new file mode 100644 index 00000000..8f980256 --- /dev/null +++ b/modules/findit/bower.json @@ -0,0 +1,28 @@ +{ + "name": "findit", + "homepage": "https://github.com/coderaiser/domfs-findit", + "authors": [ + "coderaiser " + ], + "description": "Walk a directory tree in DOM File System", + "main": "lib/findit.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "findit", + "DOM", + "File System" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "modules" + ], + "dependencies": { + "emitify": "~1.2.0" + }, + "version": "1.1.2" +} diff --git a/modules/findit/example/index.html b/modules/findit/example/index.html new file mode 100644 index 00000000..458b14ae --- /dev/null +++ b/modules/findit/example/index.html @@ -0,0 +1,39 @@ + + + + + diff --git a/modules/findit/lib/findit.js b/modules/findit/lib/findit.js new file mode 100644 index 00000000..7ea26ad4 --- /dev/null +++ b/modules/findit/lib/findit.js @@ -0,0 +1,60 @@ +/* global Emitify */ + +(function(global) { + 'use strict'; + + if (typeof module !== 'undefined' && module.exports) + module.exports = findit; + else + global.findit = findit; + + function findit(entry) { + var emitter = Emitify(); + + setTimeout(function() { + FindIt(emitter, entry); + }, 0); + + return emitter; + } + + function FindIt(emitter, entry) { + if (!(this instanceof FindIt)) + return new FindIt(emitter, entry); + + this._dirs = 0; + this._first = true; + + this._find(emitter, entry); + } + + FindIt.prototype._find = function(emitter, entry) { + var self = this; + + if (entry.isFile) { + emitter.emit('file', entry.fullPath, entry); + + if (self._first) + emitter.emit('end'); + } else { + if (self._first) + self._first = false; + + emitter.emit('directory', entry.fullPath, entry); + + ++self._dirs; + + entry.createReader() + .readEntries(function(entries) { + [].filter.call(entries, function(entry) { + self._find(emitter, entry); + }); + + --self._dirs; + + if (!self._dirs) + emitter.emit('end'); + }); + } + }; +})(this); diff --git a/modules/findit/package.json b/modules/findit/package.json new file mode 100644 index 00000000..2979a1fc --- /dev/null +++ b/modules/findit/package.json @@ -0,0 +1,27 @@ +{ + "name": "findit", + "private": true, + "version": "1.1.2", + "description": "Walk a directory tree in DOM File System", + "main": "lib/findit.js", + "dependencies": {}, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git://github.com/coderaiser/domfs-findit.git" + }, + "keywords": [ + "findit", + "DOM", + "File System" + ], + "author": "coderaiser (http://coderaiser.github.io/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/coderaiser/domfs-findit/issues" + }, + "homepage": "https://github.com/coderaiser/domfs-findit" +} diff --git a/modules/philip/.bower.json b/modules/philip/.bower.json new file mode 100644 index 00000000..4bfec476 --- /dev/null +++ b/modules/philip/.bower.json @@ -0,0 +1,38 @@ +{ + "name": "philip", + "version": "1.1.1", + "homepage": "https://github.com/coderaiser/domfs-philip", + "authors": [ + "coderaiser " + ], + "description": "Walk a directory tree in DOM File System", + "main": "lib/philip.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "DOM", + "File System" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "modules" + ], + "dependencies": { + "emitify": "~1.2.0", + "findit": "~1.1.0" + }, + "_release": "1.1.1", + "_resolution": { + "type": "version", + "tag": "v1.1.1", + "commit": "8d6fdd3fe7dece2c0760a6beada90ff4e4ff4064" + }, + "_source": "git://github.com/coderaiser/domfs-philip.git", + "_target": "~1.1.1", + "_originalSource": "philip", + "_direct": true +} \ No newline at end of file diff --git a/modules/philip/ChangeLog b/modules/philip/ChangeLog new file mode 100644 index 00000000..985d9c7b --- /dev/null +++ b/modules/philip/ChangeLog @@ -0,0 +1,11 @@ +2015.07.16, v1.1.1 + +feature: +- (philip) inherit from Emitify + + +2015.07.16, v1.1.0 + +feature: +- (philip) add abort, pause, continue + diff --git a/modules/philip/LICENSE b/modules/philip/LICENSE new file mode 100644 index 00000000..a103b669 --- /dev/null +++ b/modules/philip/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 coderaiser + +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. diff --git a/modules/philip/README.md b/modules/philip/README.md new file mode 100644 index 00000000..566d7e8c --- /dev/null +++ b/modules/philip/README.md @@ -0,0 +1,77 @@ +# Philip + +[Dom File System](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem "Dom File System") processing library + +## Install + +``` +bower i philip --save +``` + +## How to use? + +Add `philip.js` [findit](https://github.com/coderaiser/domfs-findit "Find It") and [emitify](https://github.com/coderaiser/emitify "Emitify"). + +Or any other node-compitable [EventEmitter](https://iojs.org/api/events.html "Events") (set `window.Emitify = your_emitter` before using `findit`). + +```html + + + +``` + +```js +(function() { + 'use strict'; + + var node = window; + + node.addEventListener('drop', function (e) { + var upload, + entry, + finder, + item = e.dataTransfer.items[0]; + + e.preventDefault(); + + entry = item.webkitGetAsEntry(); + + upload = philip(entry, function(type, name, data, callback) { + var error = null; + + switch(type) { + case 'file': + console.log('file', name, data); + break; + + case 'directory': + console.log('directory', name); + break; + } + + callback(error); + }); + + upload.on('error', function(error) { + upload.abort(); + console.error(error); + }); + + upload.on('progress', function(count) { + console.log(count); + }); + + upload.on('end', function() { + console.log('done'); + }); + }); + + node.addEventListener('dragover', function (e) { + e.preventDefault(); + }); +})(); +``` + +## License + +MIT diff --git a/modules/philip/bower.json b/modules/philip/bower.json new file mode 100644 index 00000000..6f13a110 --- /dev/null +++ b/modules/philip/bower.json @@ -0,0 +1,28 @@ +{ + "name": "philip", + "version": "1.1.1", + "homepage": "https://github.com/coderaiser/domfs-philip", + "authors": [ + "coderaiser " + ], + "description": "Walk a directory tree in DOM File System", + "main": "lib/philip.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "DOM", + "File System" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "modules" + ], + "dependencies": { + "emitify": "~1.2.0", + "findit": "~1.1.0" + } +} diff --git a/modules/philip/example/index.html b/modules/philip/example/index.html new file mode 100644 index 00000000..956e7730 --- /dev/null +++ b/modules/philip/example/index.html @@ -0,0 +1,55 @@ + + + + + + diff --git a/modules/philip/lib/philip.js b/modules/philip/lib/philip.js new file mode 100644 index 00000000..94895a1a --- /dev/null +++ b/modules/philip/lib/philip.js @@ -0,0 +1,149 @@ +/* global Emitify */ +/* global findit */ + +(function(global) { + 'use strict'; + + if (typeof module !== 'undefined' && module.exports) + module.exports = Philip; + else + global.philip = Philip; + + Philip.prototype = Object.create(Emitify.prototype); + + function Philip(entries, processingFn) { + var array, + self; + + if (!(this instanceof Philip)) + return new Philip(entries, processingFn); + + if (typeof processingFn !== 'function') + throw Error('processingFn should be function!'); + + Emitify.call(this); + + if (Array.isArray(entries)) + array = entries; + else + array = [entries]; + + self = this; + + this._i = 0; + this._n = 0; + this._processingFn = processingFn; + this._pause = false; + + this._find(array, function(files, dirs) { + self._files = files; + self._dirs = dirs; + self._n = files.length + dirs.length; + self._data = {}; + + self._getFiles(files, self._data, function() { + self._process(); + }); + }); + } + + Philip.prototype._process = function() { + var el, + data, + self = this, + name = self._dirs.shift(), + type = 'directory'; + + if (!name) { + type = 'file'; + el = self._files.shift(); + + if (el) { + name = el.fullPath; + data = self._data[name]; + } + } + + if (!name) { + self.emit('end'); + } else if (!this._pause) { + self._processingFn(type, name, data, function(error) { + ++self._i; + + if (error) { + self.emit('error', error); + self.pause(); + } + + self._process(); + self._progress(); + }); + } + }; + + Philip.prototype.pause = function() { + this._pause = true; + }; + + Philip.prototype.continue = function() { + if (this._pause) { + this._pause = false; + this._process(); + } + }; + + Philip.prototype.abort = function() { + this._files = []; + this._dirs = []; + + this._process(); + }; + + Philip.prototype._progress = function() { + var value = Math.round(this._i * 100 / this._n); + + this.emit('progress', value); + }; + + Philip.prototype._getFiles = function(files, obj, callback) { + var current, + self = this; + + files = files.slice(); + current = files.shift(); + + if (!obj) + obj = {}; + + if (!current) + callback(null, obj); + else + current.file(function(file) { + var name = current.fullPath; + + obj[name] = file; + + self._getFiles(files, obj, callback); + }); + }; + + Philip.prototype._find = function(entries, fn) { + [].forEach.call(entries, function(entry) { + var files = [], + dirs = [], + finder = findit(entry); + + finder.on('directory', function(name) { + dirs.push(name); + }); + + finder.on('file', function(name, current) { + files.push(current); + }); + + finder.on('end', function() { + fn(files, dirs); + }); + }); + }; +})(this); diff --git a/modules/philip/package.json b/modules/philip/package.json new file mode 100644 index 00000000..57de952c --- /dev/null +++ b/modules/philip/package.json @@ -0,0 +1,27 @@ +{ + "name": "philip", + "private": true, + "version": "1.1.1", + "description": "Process files and directories in DOM File System", + "main": "lib/philip.js", + "dependencies": {}, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git://github.com/coderaiser/domfs-philip.git" + }, + "keywords": [ + "philip", + "DOM", + "File System" + ], + "author": "coderaiser (http://coderaiser.github.io/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/coderaiser/domfs-philip/issues" + }, + "homepage": "https://github.com/coderaiser/domfs-philip" +}