From b6ee9b3995457e3b89a062df474808a6977a749a Mon Sep 17 00:00:00 2001 From: Adrian Tello Date: Tue, 19 Jan 2016 15:24:05 +0100 Subject: [PATCH 1/4] Remove old grunt plugins and tasks --- Gruntfile.js | 58 ---------------------------------------------------- package.json | 4 +--- 2 files changed, 1 insertion(+), 61 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 7c957e2..e51ba78 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -3,63 +3,5 @@ module.exports = function (grunt) { // Project configuration. grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), - uglify: { - dist: { - options: { - banner: '/*' + - ' * MIT Licensed' + - ' * http://github.com/flowjs/fusty-flow.js' + - ' * Aidas Klimas' + - ' */', - compress: { - sequences: true, // join consecutive statemets with the “comma operator” - properties: true, // optimize property access: a["foo"] → a.foo - dead_code: true, // discard unreachable code - drop_debugger: true, // discard “debugger” statements - unsafe: false, // some unsafe optimizations (see below) - conditionals: true, // optimize if-s and conditional expressions - comparisons: true, // optimize comparisons - evaluate: true, // evaluate constant expressions - booleans: true, // optimize boolean expressions - loops: true, // optimize loops - unused: true, // drop unused variables/functions - hoist_funs: true, // hoist function declarations - hoist_vars: false, // hoist variable declarations - if_return: true, // optimize if-s followed by return/continue - join_vars: true, // join var declarations - cascade: true, // try to cascade `right` into `left` in sequences - side_effects: true, // drop side-effect-free statements - warnings: true, // warn about potentially dangerous optimizations/code - global_defs: {} // global definitions - } - }, - files: { - 'build/fusty-flow.min.js': [ - 'bower_components/flow.js/src/flow.js', - 'src/fusty-flow-factory.js', - 'src/fusty-flow.js' - ] - } - } - }, - concat: { - flow: { - files: { - 'build/fusty-flow.js': [ - 'bower_components/flow.js/src/flow.js', - 'src/fusty-flow-factory.js', - 'src/fusty-flow.js' - ] - } - } - } }); - - // Load the plugin that provides the "uglify" task. - grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-contrib-concat'); - - // Default task(s). - grunt.registerTask('build', ['uglify', 'concat']); - }; \ No newline at end of file diff --git a/package.json b/package.json index 76195d0..96ff4dd 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,6 @@ "license": "MIT", "readmeFilename": "README.md", "devDependencies": { - "grunt": "~0.4.1", - "grunt-contrib-uglify": "~0.11.0", - "grunt-contrib-concat": "~0.5.1" + "grunt": "~0.4.1" } } From 050e507a41d201dd069a7c8b968652df88219724 Mon Sep 17 00:00:00 2001 From: Adrian Tello Date: Tue, 19 Jan 2016 15:31:33 +0100 Subject: [PATCH 2/4] Convert the code to CommonJS --- package.json | 5 +- src/fusty-flow-factory.js | 20 +- src/fusty-flow.js | 770 +++++++++++++++++++------------------- src/index.js | 7 + 4 files changed, 399 insertions(+), 403 deletions(-) create mode 100644 src/index.js diff --git a/package.json b/package.json index 96ff4dd..4a298a3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "fusty-flow.js", "version": "1.2.0", "description": "Flow.js html5 file upload support for older browsers based on iframe upload.", - "main": "src/fusty-flow-factory.js", + "main": "src/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -23,5 +23,8 @@ "readmeFilename": "README.md", "devDependencies": { "grunt": "~0.4.1" + }, + "dependencies": { + "flow.js": "git@github.com:flowjs/flow.js.git#v2.9.0" } } diff --git a/src/fusty-flow-factory.js b/src/fusty-flow-factory.js index b81092e..b55c59c 100644 --- a/src/fusty-flow-factory.js +++ b/src/fusty-flow-factory.js @@ -1,14 +1,12 @@ -(function (Flow, FustyFlow, window) { - 'use strict'; +var Flow = require('flow.js'); +var FustyFlow = require('./fusty-flow'); - var fustyFlowFactory = function (opts) { - var flow = new Flow(opts); - if (flow.support) { - return flow; - } - return new FustyFlow(opts); +var fustyFlowFactory = function (opts) { + var flow = new Flow(opts); + if (flow.support) { + return flow; } + return new FustyFlow(opts); +} - window.fustyFlowFactory = fustyFlowFactory; - -})(window.Flow, window.FustyFlow, window); +module.exports = fustyFlowFactory; \ No newline at end of file diff --git a/src/fusty-flow.js b/src/fusty-flow.js index 0f9a3d4..65b765b 100644 --- a/src/fusty-flow.js +++ b/src/fusty-flow.js @@ -1,429 +1,417 @@ -(function (Flow, window, document, undefined) { - 'use strict'; +var Flow = require('flow.js'); - var extend = Flow.extend; - var each = Flow.each; +var extend = Flow.extend; +var each = Flow.each; - function addEvent(element, type, handler) { - if (element.addEventListener) { - element.addEventListener(type, handler, false); - } else if (element.attachEvent) { - element.attachEvent("on" + type, handler); - } else { - element["on" + type] = handler; - } +function addEvent(element, type, handler) { + if (element.addEventListener) { + element.addEventListener(type, handler, false); + } else if (element.attachEvent) { + element.attachEvent("on" + type, handler); + } else { + element["on" + type] = handler; } +} - function removeEvent(element, type, handler) { - if (element.removeEventListener) { - element.removeEventListener(type, handler, false); - } else if (element.detachEvent) { - element.detachEvent("on" + type, handler); - } else { - element["on" + type] = null; - } +function removeEvent(element, type, handler) { + if (element.removeEventListener) { + element.removeEventListener(type, handler, false); + } else if (element.detachEvent) { + element.detachEvent("on" + type, handler); + } else { + element["on" + type] = null; } +} - function removeElement(element) { - element.parentNode.removeChild(element); - } +function removeElement(element) { + element.parentNode.removeChild(element); +} - function isFunction(functionToCheck) { - var getType = {}; - return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; - } +function isFunction(functionToCheck) { + var getType = {}; + return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; +} - /** - * Not resumable file upload library, for IE7-IE9 browsers - * @name FustyFlow - * @param [opts] - * @param {bool} [opts.singleFile] - * @param {string} [opts.fileParameterName] - * @param {Object|Function} [opts.query] - * @param {Object} [opts.headers] - * @param {string} [opts.target] - * @param {Function} [opts.generateUniqueIdentifier] - * @param {bool} [opts.matchJSON] - * @constructor - */ - function FustyFlow(opts) { - // Shortcut of "r instanceof Flow" - this.support = false; +/** + * Not resumable file upload library, for IE7-IE9 browsers + * @name FustyFlow + * @param [opts] + * @param {bool} [opts.singleFile] + * @param {string} [opts.fileParameterName] + * @param {Object|Function} [opts.query] + * @param {Object} [opts.headers] + * @param {string} [opts.target] + * @param {Function} [opts.generateUniqueIdentifier] + * @param {bool} [opts.matchJSON] + * @constructor + */ +function FustyFlow(opts) { + // Shortcut of "r instanceof Flow" + this.support = false; - this.files = []; - this.events = []; - this.defaults = { - simultaneousUploads: 3, - fileParameterName: 'file', - query: {}, - target: '/', - generateUniqueIdentifier: null, - matchJSON: false - }; + this.files = []; + this.events = []; + this.defaults = { + simultaneousUploads: 3, + fileParameterName: 'file', + query: {}, + target: '/', + generateUniqueIdentifier: null, + matchJSON: false + }; - var $ = this; + var $ = this; - this.inputChangeEvent = function (event) { - var input = event.target || event.srcElement; - removeEvent(input, 'change', $.inputChangeEvent); - var newClone = input.cloneNode(false); - // change current input with new one - input.parentNode.replaceChild(newClone, input); - // old input will be attached to hidden form - $.addFile(input, event); - // reset new input - newClone.value = ''; - addEvent(newClone, 'change', $.inputChangeEvent); - }; + this.inputChangeEvent = function (event) { + var input = event.target || event.srcElement; + removeEvent(input, 'change', $.inputChangeEvent); + var newClone = input.cloneNode(false); + // change current input with new one + input.parentNode.replaceChild(newClone, input); + // old input will be attached to hidden form + $.addFile(input, event); + // reset new input + newClone.value = ''; + addEvent(newClone, 'change', $.inputChangeEvent); + }; - this.opts = Flow.extend({}, this.defaults, opts || {}); - } + this.opts = Flow.extend({}, this.defaults, opts || {}); +} - FustyFlow.prototype = { - on: Flow.prototype.on, - off: Flow.prototype.off, - fire: Flow.prototype.fire, - cancel: Flow.prototype.cancel, - assignBrowse: function (domNodes) { - if (typeof domNodes.length == 'undefined') { - domNodes = [domNodes]; - } - each(domNodes, function (domNode) { - var input; - if (domNode.tagName === 'INPUT' && domNode.type === 'file') { - input = domNode; - } else { - input = document.createElement('input'); - input.setAttribute('type', 'file'); +FustyFlow.prototype = { + on: Flow.prototype.on, + off: Flow.prototype.off, + fire: Flow.prototype.fire, + cancel: Flow.prototype.cancel, + assignBrowse: function (domNodes) { + if (typeof domNodes.length == 'undefined') { + domNodes = [domNodes]; + } + each(domNodes, function (domNode) { + var input; + if (domNode.tagName === 'INPUT' && domNode.type === 'file') { + input = domNode; + } else { + input = document.createElement('input'); + input.setAttribute('type', 'file'); - extend(domNode.style, { - display: 'inline-block', - position: 'relative', - overflow: 'hidden', - verticalAlign: 'top' - }); + extend(domNode.style, { + display: 'inline-block', + position: 'relative', + overflow: 'hidden', + verticalAlign: 'top' + }); - extend(input.style, { - position: 'absolute', - top: 0, - right: 0, - fontFamily: 'Arial', - // 4 persons reported this, the max values that worked for them were 243, 236, 236, 118 - fontSize: '118px', - margin: 0, - padding: 0, - opacity: 0, - filter: 'alpha(opacity=0)', - cursor: 'pointer' - }); + extend(input.style, { + position: 'absolute', + top: 0, + right: 0, + fontFamily: 'Arial', + // 4 persons reported this, the max values that worked for them were 243, 236, 236, 118 + fontSize: '118px', + margin: 0, + padding: 0, + opacity: 0, + filter: 'alpha(opacity=0)', + cursor: 'pointer' + }); - domNode.appendChild(input); - } - // When new files are added, simply append them to the overall list - addEvent(input, 'change', this.inputChangeEvent); - }, this); - }, - assignDrop: function () { - // not supported - }, - unAssignDrop: function () { - // not supported - }, - isUploading: function () { - var uploading = false; - each(this.files, function (file) { - if (file.isUploading()) { - uploading = true; - return false; - } - }); - return uploading; - }, - upload: function () { - // Kick off the queue - var files = 0; - each(this.files, function (file) { - if (file.progress() == 1 || file.isPaused()) { - return; - } - if (file.isUploading()) { - files++; - return; - } - if (files++ >= this.opts.simultaneousUploads) { - return false; - } - if (files == 1) { - this.fire('uploadStart'); - } - file.send(); - }, this); - if (!files) { - this.fire('complete'); + domNode.appendChild(input); } - }, - pause: function () { - each(this.files, function (file) { - file.pause(); - }); - }, - resume: function () { - each(this.files, function (file) { - file.resume(); - }); - }, - progress: function () { - var totalDone = 0; - var totalFiles = 0; - each(this.files, function (file) { - totalDone += file.progress(); - totalFiles++; - }); - return totalFiles > 0 ? totalDone / totalFiles : 0; - }, - addFiles: function (elementsList, event) { - var files = []; - each(elementsList, function (element) { - // is domElement ? - if (element.nodeType === 1 && element.value) { - var f = new FustyFlowFile(this, element); - if (this.fire('fileAdded', f, event)) { - files.push(f); - } - } - }, this); - if (this.fire('filesAdded', files, event)) { - each(files, function (file) { - if (this.opts.singleFile && this.files.length > 0) { - this.removeFile(this.files[0]); - } - this.files.push(file); - }, this); + // When new files are added, simply append them to the overall list + addEvent(input, 'change', this.inputChangeEvent); + }, this); + }, + assignDrop: function () { + // not supported + }, + unAssignDrop: function () { + // not supported + }, + isUploading: function () { + var uploading = false; + each(this.files, function (file) { + if (file.isUploading()) { + uploading = true; + return false; + } + }); + return uploading; + }, + upload: function () { + // Kick off the queue + var files = 0; + each(this.files, function (file) { + if (file.progress() == 1 || file.isPaused()) { + return; } - this.fire('filesSubmitted', files, event); - }, - addFile: function (file, event) { - this.addFiles([file], event); - }, - generateUniqueIdentifier: function (element) { - var custom = this.opts.generateUniqueIdentifier; - if (typeof custom === 'function') { - return custom(element); + if (file.isUploading()) { + files++; + return; + } + if (files++ >= this.opts.simultaneousUploads) { + return false; + } + if (files == 1) { + this.fire('uploadStart'); } - return 'xxxxxxxx-xxxx-yxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { - var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); - return v.toString(16); - }); - }, - getFromUniqueIdentifier: function (uniqueIdentifier) { - var ret = false; - each(this.files, function (f) { - if (f.uniqueIdentifier == uniqueIdentifier) ret = f; - }); - return ret; - }, - removeFile: function (file) { - for (var i = this.files.length - 1; i >= 0; i--) { - if (this.files[i] === file) { - this.files.splice(i, 1); + file.send(); + }, this); + if (!files) { + this.fire('complete'); + } + }, + pause: function () { + each(this.files, function (file) { + file.pause(); + }); + }, + resume: function () { + each(this.files, function (file) { + file.resume(); + }); + }, + progress: function () { + var totalDone = 0; + var totalFiles = 0; + each(this.files, function (file) { + totalDone += file.progress(); + totalFiles++; + }); + return totalFiles > 0 ? totalDone / totalFiles : 0; + }, + addFiles: function (elementsList, event) { + var files = []; + each(elementsList, function (element) { + // is domElement ? + if (element.nodeType === 1 && element.value) { + var f = new FustyFlowFile(this, element); + if (this.fire('fileAdded', f, event)) { + files.push(f); } } - }, - getSize: function () { - // undefined - }, - timeRemaining: function () { - // undefined - }, - sizeUploaded: function () { - // undefined + }, this); + if (this.fire('filesAdded', files, event)) { + each(files, function (file) { + if (this.opts.singleFile && this.files.length > 0) { + this.removeFile(this.files[0]); + } + this.files.push(file); + }, this); } - }; + this.fire('filesSubmitted', files, event); + }, + addFile: function (file, event) { + this.addFiles([file], event); + }, + generateUniqueIdentifier: function (element) { + var custom = this.opts.generateUniqueIdentifier; + if (typeof custom === 'function') { + return custom(element); + } + return 'xxxxxxxx-xxxx-yxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { + var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + }); + }, + getFromUniqueIdentifier: function (uniqueIdentifier) { + var ret = false; + each(this.files, function (f) { + if (f.uniqueIdentifier == uniqueIdentifier) ret = f; + }); + return ret; + }, + removeFile: function (file) { + for (var i = this.files.length - 1; i >= 0; i--) { + if (this.files[i] === file) { + this.files.splice(i, 1); + } + } + }, + getSize: function () { + // undefined + }, + timeRemaining: function () { + // undefined + }, + sizeUploaded: function () { + // undefined + } +}; - function FustyFlowFile(flowObj, element) { - this.flowObj = flowObj; - this.element = element; - this.name = element.value && element.value.replace(/.*(\/|\\)/, ""); - this.relativePath = this.name; - this.uniqueIdentifier = flowObj.generateUniqueIdentifier(element); - this.iFrame = null; +function FustyFlowFile(flowObj, element) { + this.flowObj = flowObj; + this.element = element; + this.name = element.value && element.value.replace(/.*(\/|\\)/, ""); + this.relativePath = this.name; + this.uniqueIdentifier = flowObj.generateUniqueIdentifier(element); + this.iFrame = null; - this.finished = false; - this.error = false; - this.paused = false; + this.finished = false; + this.error = false; + this.paused = false; - var $ = this; - this.iFrameLoaded = function (event) { - // when we remove iframe from dom - // the request stops, but in IE load - // event fires - if (!$.iFrame || !$.iFrame.parentNode) { - return; - } - $.finished = true; - try { - // fixing Opera 10.53 - if ($.iFrame.contentDocument && - $.iFrame.contentDocument.body && - $.iFrame.contentDocument.body.innerHTML == "false") { - // In Opera event is fired second time - // when body.innerHTML changed from false - // to server response approx. after 1 sec - // when we upload file with iframe - return; - } - } catch (error) { - //IE may throw an "access is denied" error when attempting to access contentDocument - $.error = true; - $.abort(); - $.flowObj.fire('fileError', $, error); + var $ = this; + this.iFrameLoaded = function (event) { + // when we remove iframe from dom + // the request stops, but in IE load + // event fires + if (!$.iFrame || !$.iFrame.parentNode) { + return; + } + $.finished = true; + try { + // fixing Opera 10.53 + if ($.iFrame.contentDocument && + $.iFrame.contentDocument.body && + $.iFrame.contentDocument.body.innerHTML == "false") { + // In Opera event is fired second time + // when body.innerHTML changed from false + // to server response approx. after 1 sec + // when we upload file with iframe return; } - // iframe.contentWindow.document - for IE<7 - var doc = $.iFrame.contentDocument || $.iFrame.contentWindow.document; - var innerHtml = doc.body.innerHTML; - if ($.flowObj.opts.matchJSON) { - innerHtml = /(\{.*\})/.exec(innerHtml)[0]; - } - + } catch (error) { + //IE may throw an "access is denied" error when attempting to access contentDocument + $.error = true; $.abort(); - $.flowObj.fire('fileSuccess', $, innerHtml); - $.flowObj.upload(); - }; - this.bootstrap(); - } + $.flowObj.fire('fileError', $, error); + return; + } + // iframe.contentWindow.document - for IE<7 + var doc = $.iFrame.contentDocument || $.iFrame.contentWindow.document; + var innerHtml = doc.body.innerHTML; + if ($.flowObj.opts.matchJSON) { + innerHtml = /(\{.*\})/.exec(innerHtml)[0]; + } - FustyFlowFile.prototype = { - getExtension: Flow.FlowFile.prototype.getExtension, - getType: function () { - // undefined - }, - send: function () { - if (this.finished) { - return; - } - var o = this.flowObj.opts; - var form = this.createForm(); - var params = o.query; - if (isFunction(params)) { - params = params(this); - } - params[o.fileParameterName] = this.element; - params['flowFilename'] = this.name; - params['flowRelativePath'] = this.relativePath; - params['flowIdentifier'] = this.uniqueIdentifier; + $.abort(); + $.flowObj.fire('fileSuccess', $, innerHtml); + $.flowObj.upload(); + }; + this.bootstrap(); +} - this.addFormParams(form, params); - addEvent(this.iFrame, 'load', this.iFrameLoaded); - form.submit(); - removeElement(form); - }, - abort: function (noupload) { - if (this.iFrame) { - this.iFrame.setAttribute('src', 'java' + String.fromCharCode(115) + 'cript:false;'); - removeElement(this.iFrame); - this.iFrame = null; - !noupload && this.flowObj.upload(); - } - }, - cancel: function () { - this.flowObj.removeFile(this); - this.abort(); - }, - retry: function () { - this.bootstrap(); - this.flowObj.upload(); - }, - bootstrap: function () { - this.abort(true); - this.finished = false; - this.error = false; - }, - timeRemaining: function () { - // undefined - }, - sizeUploaded: function () { - // undefined - }, - resume: function () { - this.paused = false; - this.flowObj.upload(); - }, - pause: function () { - this.paused = true; - this.abort(); - }, - isUploading: function () { - return this.iFrame !== null; - }, - isPaused: function () { - return this.paused; - }, - isComplete: function () { - return this.progress() === 1; - }, - progress: function () { - if (this.error) { - return 1; - } - return this.finished ? 1 : 0; - }, +FustyFlowFile.prototype = { + getExtension: Flow.FlowFile.prototype.getExtension, + getType: function () { + // undefined + }, + send: function () { + if (this.finished) { + return; + } + var o = this.flowObj.opts; + var form = this.createForm(); + var params = o.query; + if (isFunction(params)) { + params = params(this); + } + params[o.fileParameterName] = this.element; + params['flowFilename'] = this.name; + params['flowRelativePath'] = this.relativePath; + params['flowIdentifier'] = this.uniqueIdentifier; - createIframe: function () { - var iFrame = (/MSIE (6|7|8)/).test(navigator.userAgent) ? - document.createElement('