From 0418a775f50923bca8aae266376f91f01f13e499 Mon Sep 17 00:00:00 2001 From: Wildhoney Date: Thu, 15 Oct 2015 14:41:56 +0100 Subject: [PATCH] Added Droplet.METHOD object --- README.md | 2 +- components/Droplet.js | 14 +++++++++++++- dist/ember-droplet.js | 14 +++++++++++++- dist/ember-droplet.min.js | 2 +- tests/DropletOptions.test.js | 4 ++-- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8371f76..46ea919 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ options object: ```javascript App.XDropletComponent = Ember.Component.extend(Droplet, { options: { - requestMethod: "PATCH" + requestMethod: Droplet.METHOD.PATCH // ... } }); diff --git a/components/Droplet.js b/components/Droplet.js index 912c837..6efe7a9 100644 --- a/components/Droplet.js +++ b/components/Droplet.js @@ -11,6 +11,12 @@ */ const STATUS_TYPES = { NONE: 0, VALID: 1, INVALID: 2, DELETED: 4, UPLOADED: 8, FAILED: 16 }; + /** + * @constant REQUEST_METHODS + * @type {{PATCH: string, POST: string, PUT: string}} + */ + const REQUEST_METHODS = { PATCH: 'PATCH', POST: 'POST', PUT: 'PUT' }; + /** * @constant EVENT_NAME * @type {String} @@ -142,7 +148,7 @@ * @property requestMethod * @type {String} */ - requestMethod: 'POST', + requestMethod: REQUEST_METHODS.POST, /** * @property maximumSize @@ -904,4 +910,10 @@ multiple: false }); + /** + * @constant $window.Droplet.METHOD + * @type {Object} + */ + $window.Droplet.METHOD = REQUEST_METHODS; + })(window, window.Ember, window.FileReader); diff --git a/dist/ember-droplet.js b/dist/ember-droplet.js index c348954..44f4e5a 100644 --- a/dist/ember-droplet.js +++ b/dist/ember-droplet.js @@ -23,6 +23,12 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr */ var STATUS_TYPES = { NONE: 0, VALID: 1, INVALID: 2, DELETED: 4, UPLOADED: 8, FAILED: 16 }; + /** + * @constant REQUEST_METHODS + * @type {{PATCH: string, POST: string, PUT: string}} + */ + var REQUEST_METHODS = { PATCH: 'PATCH', POST: 'POST', PUT: 'PUT' }; + /** * @constant EVENT_NAME * @type {String} @@ -155,7 +161,7 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr * @property requestMethod * @type {String} */ - requestMethod: 'POST', + requestMethod: REQUEST_METHODS.POST, /** * @property maximumSize @@ -975,4 +981,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr $window.Droplet.SingleInput = Mixin.create($window.Droplet.MultipleInput, { multiple: false }); + + /** + * @constant $window.Droplet.METHOD + * @type {Object} + */ + $window.Droplet.METHOD = REQUEST_METHODS; })(window, window.Ember, window.FileReader); \ No newline at end of file diff --git a/dist/ember-droplet.min.js b/dist/ember-droplet.min.js index 3763cae..ab570c6 100644 --- a/dist/ember-droplet.min.js +++ b/dist/ember-droplet.min.js @@ -1 +1 @@ -"use strict";function _toConsumableArray(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);ti;i++)r[i]=arguments[i];e.send.apply(e,["prepareFiles"].concat(r))}),this._super()},willDestroy:function(){this._super();var e=this.get("lastRequest");e&&(delete e.upload.onprogress,delete e.upload.onload,delete e.upload.onerror,this.send("abortUpload"))},invokeHook:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),i=1;t>i;i++)r[i-1]=arguments[i];var n=c(this,"hooks")[e]||function(){},s=this;Ember.run(function(){n.apply(s,r)})},uploadStatus:p(function(){return{uploading:!1,percentComplete:0,error:!1}}),validFiles:(i=p(function(){return this.getFiles(h.VALID)})).property.apply(i,_toConsumableArray(D)),invalidFiles:(n=p(function(){return this.getFiles(h.INVALID)})).property.apply(n,_toConsumableArray(D)),uploadedFiles:(s=p(function(){return this.getFiles(h.UPLOADED)})).property.apply(s,_toConsumableArray(D)),deletedFiles:(a=p(function(){return this.getFiles(h.DELETED)})).property.apply(a,_toConsumableArray(D)),requestSize:(o=p(function(){return c(this,"validFiles").reduce(function(e,t){return e+t.getFileSize()},0)})).property.apply(o,_toConsumableArray(D)),getFiles:function(e){return e?this.files.filter(function(t){return t.statusType&e}):this.files},isValid:function S(e){var r=this;if(!(e instanceof t.Object))return!1;var i=function(e){return function(){var t=r.get("options.mimeTypes").some(function(e){return e instanceof RegExp}),i=c(r,"options.mimeTypes");return t?i.some(function(t){var r=t===e,i=!!e.match(t);return r||i}):!!~i.indexOf(e)}},n=function(e){return function(){return e<=Number(c(r,"options.maximumSize"))}},s=function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];return function(e){return t.reverse().every(function(t){return t(e)})}},S=s(i(e.getMIMEType()),n(e.getFileSize()));return S(e)},getFormData:function(){var t=new e.FormData,r=this.get("options.useArray")?"file[]":"file",i=this.get("options.requestPostData"),n=c(this,"validFiles").map(function(e){return e.file});return n.forEach(function(e){t.append(r,e)}),Object.keys(i).forEach(function(e){t.append(e,i[e])}),t},addProgressListener:function(e){var t=this;e.addEventListener("progress",function(e){if(e.lengthComputable){var r=e.loaded/c(t,"requestSize")*100;d(t,"uploadStatus.percentComplete",Math.round(r))}})},getRequest:function(){var e=this,r=function(e){return"function"==typeof e},i=r(c(this,"url"))?c(this,"url").apply(this):c(this,"url"),n=c(this,"options.requestMethod")||"POST",s=this.getFormData(),a=t.merge({},this.get("options.requestHeaders"));c(this,"options.includeXFileSize")&&(a["X-File-Size"]=this.get("requestSize"));var o=t.$.ajax({url:i,method:n,headers:a,data:s,processData:!1,contentType:!1,xhr:function u(){var u=t.$.ajaxSettings.xhr();return e.addProgressListener(u.upload),d(e,"lastRequest",u),u}});return d(this,"lastResolver",o),o},actions:{uploadFiles:function(){var e=this,r=c(this,"files").filter(function(e){return e.statusType&h.VALID}),i=this.getRequest();d(this,"abortedUpload",!1),d(this,"uploadStatus.percentComplete",0),d(this,"uploadStatus.uploading",!0),d(this,"uploadStatus.error",!1);var n=function(t,n){e.invokeHook("promiseResolver",t,n,r),i.done(t).fail(n)},s=function(t){e.invokeHook("didUpload",t),r.map(function(e){return e.setStatusType(h.UPLOADED)})},a=function(t){var r=t.request,i=t.textStatus,n=t.errorThrown;c(e,"abortedUpload")!==!0&&d(e,"uploadStatus.error",{request:r,textStatus:i,errorThrown:n})},o=function(){d(e,"uploadStatus.uploading",!1),e.invokeHook("didComplete")};return new t.RSVP.Promise(n).then(s,a)["finally"](o)},abortUpload:function(){var e=c(this,"lastResolver");e&&c(this,"uploadStatus.uploading")&&(d(this,"abortedUpload",!0),e.abort(),d(this,"uploadStatus.uploading",!1))},mimeTypes:function(e){var t=arguments.length<=1||void 0===arguments[1]?b.PUSH:arguments[1];t===b.SET&&d(this,"options.mimeTypes",[]),e=Array.isArray(e)?e:[e];var r=[].concat(_toConsumableArray(c(this,"options.mimeTypes")),_toConsumableArray(e));d(this,"options.mimeTypes",r)},addFiles:function(){for(var e=this,r=arguments.length,i=Array(r),n=0;r>n;n++)i[n]=arguments[n];var s=i.map(function(r){var i=e.get("validFiles.length")===e.get("options.maximumValidFiles");if(r instanceof t.Object){var n=function(){var t=e.isValid(r)&&!i?h.VALID:h.INVALID;return f(function(){return r.setStatusType(t)}),c(e,"files").pushObject(r),{v:r}}();if("object"==typeof n)return n.v}}).filter(function(e){return"undefined"!=typeof e});s.length&&this.invokeHook.apply(this,["didAdd"].concat(_toConsumableArray(s))),this.get("options.uploadImmediately")&&this.send.apply(this,["uploadFiles"].concat(_toConsumableArray(s)))},prepareFiles:function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];t=g(t);var i=t.reduce(function(e,t){var r=v.create({file:t});return e.push(r),e},[]);return this.send.apply(this,["addFiles"].concat(_toConsumableArray(i))),i},deleteFiles:function(){for(var e=this,t=arguments.length,r=Array(t),i=0;t>i;i++)r[i]=arguments[i];var n=r.map(function(t){var r=!!~c(e,"files").indexOf(t);return r?(t.setStatusType(h.DELETED),t):void 0}).filter(function(e){return"undefined"!=typeof e});n.length&&this.invokeHook.apply(this,["didDelete"].concat(_toConsumableArray(n)))},clearFiles:function(){var e=this,t=[].concat(_toConsumableArray(this.get("validFiles")),_toConsumableArray(this.get("invalidFiles")));t.forEach(function(t){return e.send("deleteFiles",t)})}}});var F=function(e){e.preventDefault(),e.stopPropagation()};e.Droplet.Area=u.create({classNames:["droppable"],drop:function(e){return F(e),this.handleFiles(e.dataTransfer.files)},handleFiles:function(e){var t;return this.DropletEventBus&&(t=this.DropletEventBus).publish.apply(t,[m].concat(_toConsumableArray(g(e)))),e},dragEnter:F,dragOver:F,dragLeave:F}),e.Droplet.Preview=u.create({tagName:"img",attributeBindings:["src"],reader:r,image:{file:{type:""}},isImage:function(e){return!!e.type.match(/^image\//i)},didInsertElement:function(){var e=this,t=this.get("reader"),r=new t,i=c(this,"image.file");return this.isImage(i)?(r.addEventListener("load",f.bind(this,function(t){e.get("isDestroyed")!==!0&&d(e,"src",t.target.result)})),void r.readAsDataURL(i)):void this.destroy()}}),e.Droplet.MultipleInput=u.create({tagName:"input",classNames:"files",attributeBindings:["disabled","name","type","multiple"],type:"file",multiple:"multiple",change:function(){var e=this.get("element").files;this.handleFiles(e)},handleFiles:function(e){var t;this.DropletEventBus&&(t=this.DropletEventBus).publish.apply(t,[m].concat(_toConsumableArray(g(e))))}}),e.Droplet.SingleInput=u.create(e.Droplet.MultipleInput,{multiple:!1})}(window,window.Ember,window.FileReader); \ No newline at end of file +"use strict";function _toConsumableArray(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);ti;i++)r[i]=arguments[i];e.send.apply(e,["prepareFiles"].concat(r))}),this._super()},willDestroy:function(){this._super();var e=this.get("lastRequest");e&&(delete e.upload.onprogress,delete e.upload.onload,delete e.upload.onerror,this.send("abortUpload"))},invokeHook:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),i=1;t>i;i++)r[i-1]=arguments[i];var n=c(this,"hooks")[e]||function(){},s=this;Ember.run(function(){n.apply(s,r)})},uploadStatus:p(function(){return{uploading:!1,percentComplete:0,error:!1}}),validFiles:(i=p(function(){return this.getFiles(h.VALID)})).property.apply(i,_toConsumableArray(E)),invalidFiles:(n=p(function(){return this.getFiles(h.INVALID)})).property.apply(n,_toConsumableArray(E)),uploadedFiles:(s=p(function(){return this.getFiles(h.UPLOADED)})).property.apply(s,_toConsumableArray(E)),deletedFiles:(a=p(function(){return this.getFiles(h.DELETED)})).property.apply(a,_toConsumableArray(E)),requestSize:(o=p(function(){return c(this,"validFiles").reduce(function(e,t){return e+t.getFileSize()},0)})).property.apply(o,_toConsumableArray(E)),getFiles:function(e){return e?this.files.filter(function(t){return t.statusType&e}):this.files},isValid:function T(e){var r=this;if(!(e instanceof t.Object))return!1;var i=function(e){return function(){var t=r.get("options.mimeTypes").some(function(e){return e instanceof RegExp}),i=c(r,"options.mimeTypes");return t?i.some(function(t){var r=t===e,i=!!e.match(t);return r||i}):!!~i.indexOf(e)}},n=function(e){return function(){return e<=Number(c(r,"options.maximumSize"))}},s=function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];return function(e){return t.reverse().every(function(t){return t(e)})}},T=s(i(e.getMIMEType()),n(e.getFileSize()));return T(e)},getFormData:function(){var t=new e.FormData,r=this.get("options.useArray")?"file[]":"file",i=this.get("options.requestPostData"),n=c(this,"validFiles").map(function(e){return e.file});return n.forEach(function(e){t.append(r,e)}),Object.keys(i).forEach(function(e){t.append(e,i[e])}),t},addProgressListener:function(e){var t=this;e.addEventListener("progress",function(e){if(e.lengthComputable){var r=e.loaded/c(t,"requestSize")*100;d(t,"uploadStatus.percentComplete",Math.round(r))}})},getRequest:function(){var e=this,r=function(e){return"function"==typeof e},i=r(c(this,"url"))?c(this,"url").apply(this):c(this,"url"),n=c(this,"options.requestMethod")||"POST",s=this.getFormData(),a=t.merge({},this.get("options.requestHeaders"));c(this,"options.includeXFileSize")&&(a["X-File-Size"]=this.get("requestSize"));var o=t.$.ajax({url:i,method:n,headers:a,data:s,processData:!1,contentType:!1,xhr:function u(){var u=t.$.ajaxSettings.xhr();return e.addProgressListener(u.upload),d(e,"lastRequest",u),u}});return d(this,"lastResolver",o),o},actions:{uploadFiles:function(){var e=this,r=c(this,"files").filter(function(e){return e.statusType&h.VALID}),i=this.getRequest();d(this,"abortedUpload",!1),d(this,"uploadStatus.percentComplete",0),d(this,"uploadStatus.uploading",!0),d(this,"uploadStatus.error",!1);var n=function(t,n){e.invokeHook("promiseResolver",t,n,r),i.done(t).fail(n)},s=function(t){e.invokeHook("didUpload",t),r.map(function(e){return e.setStatusType(h.UPLOADED)})},a=function(t){var r=t.request,i=t.textStatus,n=t.errorThrown;c(e,"abortedUpload")!==!0&&d(e,"uploadStatus.error",{request:r,textStatus:i,errorThrown:n})},o=function(){d(e,"uploadStatus.uploading",!1),e.invokeHook("didComplete")};return new t.RSVP.Promise(n).then(s,a)["finally"](o)},abortUpload:function(){var e=c(this,"lastResolver");e&&c(this,"uploadStatus.uploading")&&(d(this,"abortedUpload",!0),e.abort(),d(this,"uploadStatus.uploading",!1))},mimeTypes:function(e){var t=arguments.length<=1||void 0===arguments[1]?A.PUSH:arguments[1];t===A.SET&&d(this,"options.mimeTypes",[]),e=Array.isArray(e)?e:[e];var r=[].concat(_toConsumableArray(c(this,"options.mimeTypes")),_toConsumableArray(e));d(this,"options.mimeTypes",r)},addFiles:function(){for(var e=this,r=arguments.length,i=Array(r),n=0;r>n;n++)i[n]=arguments[n];var s=i.map(function(r){var i=e.get("validFiles.length")===e.get("options.maximumValidFiles");if(r instanceof t.Object){var n=function(){var t=e.isValid(r)&&!i?h.VALID:h.INVALID;return f(function(){return r.setStatusType(t)}),c(e,"files").pushObject(r),{v:r}}();if("object"==typeof n)return n.v}}).filter(function(e){return"undefined"!=typeof e});s.length&&this.invokeHook.apply(this,["didAdd"].concat(_toConsumableArray(s))),this.get("options.uploadImmediately")&&this.send.apply(this,["uploadFiles"].concat(_toConsumableArray(s)))},prepareFiles:function(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];t=y(t);var i=t.reduce(function(e,t){var r=b.create({file:t});return e.push(r),e},[]);return this.send.apply(this,["addFiles"].concat(_toConsumableArray(i))),i},deleteFiles:function(){for(var e=this,t=arguments.length,r=Array(t),i=0;t>i;i++)r[i]=arguments[i];var n=r.map(function(t){var r=!!~c(e,"files").indexOf(t);return r?(t.setStatusType(h.DELETED),t):void 0}).filter(function(e){return"undefined"!=typeof e});n.length&&this.invokeHook.apply(this,["didDelete"].concat(_toConsumableArray(n)))},clearFiles:function(){var e=this,t=[].concat(_toConsumableArray(this.get("validFiles")),_toConsumableArray(this.get("invalidFiles")));t.forEach(function(t){return e.send("deleteFiles",t)})}}});var S=function(e){e.preventDefault(),e.stopPropagation()};e.Droplet.Area=u.create({classNames:["droppable"],drop:function(e){return S(e),this.handleFiles(e.dataTransfer.files)},handleFiles:function(e){var t;return this.DropletEventBus&&(t=this.DropletEventBus).publish.apply(t,[g].concat(_toConsumableArray(y(e)))),e},dragEnter:S,dragOver:S,dragLeave:S}),e.Droplet.Preview=u.create({tagName:"img",attributeBindings:["src"],reader:r,image:{file:{type:""}},isImage:function(e){return!!e.type.match(/^image\//i)},didInsertElement:function(){var e=this,t=this.get("reader"),r=new t,i=c(this,"image.file");return this.isImage(i)?(r.addEventListener("load",f.bind(this,function(t){e.get("isDestroyed")!==!0&&d(e,"src",t.target.result)})),void r.readAsDataURL(i)):void this.destroy()}}),e.Droplet.MultipleInput=u.create({tagName:"input",classNames:"files",attributeBindings:["disabled","name","type","multiple"],type:"file",multiple:"multiple",change:function(){var e=this.get("element").files;this.handleFiles(e)},handleFiles:function(e){var t;this.DropletEventBus&&(t=this.DropletEventBus).publish.apply(t,[g].concat(_toConsumableArray(y(e))))}}),e.Droplet.SingleInput=u.create(e.Droplet.MultipleInput,{multiple:!1}),e.Droplet.METHOD=m}(window,window.Ember,window.FileReader); \ No newline at end of file diff --git a/tests/DropletOptions.test.js b/tests/DropletOptions.test.js index cc1915e..05dba21 100644 --- a/tests/DropletOptions.test.js +++ b/tests/DropletOptions.test.js @@ -19,8 +19,8 @@ describe('Ember Droplet: Options', () => { expect(component.get('options.mimeTypes.length')).toEqual(defaultMimeTypesLength + 1); expect(component.get('options.mimeTypes')[defaultMimeTypesLength]).toEqual('application/pdf'); - component.set('options.requestMethod', 'GET'); - expect(component.get('options.requestMethod')).toEqual('GET'); + component.set('options.requestMethod', Droplet.METHOD.PATCH); + expect(component.get('options.requestMethod')).toEqual('PATCH'); component.set('options.maximumSize', 4000); expect(component.get('options.maximumSize')).toEqual(4000);