From 2889d5af7cb7c90ac4ab7c5499fc8c8dddf05de0 Mon Sep 17 00:00:00 2001 From: Max Minkoff Date: Thu, 1 Jun 2017 16:59:21 -0400 Subject: [PATCH] =?UTF-8?q?corrected=20issue=20calling=20=E2=80=98onClose?= =?UTF-8?q?=E2=80=99,=20cleaned=20syntax=20styling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ember-filestack-picker/component.js | 86 +++++++++++-------- package.json | 2 +- tests/dummy/app/controllers/application.js | 38 ++++---- .../ember-filestack-picker/component-test.js | 26 +++++- tests/unit/services/filestack-test.js | 28 +++--- 5 files changed, 109 insertions(+), 71 deletions(-) diff --git a/addon/components/ember-filestack-picker/component.js b/addon/components/ember-filestack-picker/component.js index 8fa90cf..ecf6b68 100644 --- a/addon/components/ember-filestack-picker/component.js +++ b/addon/components/ember-filestack-picker/component.js @@ -1,44 +1,62 @@ import Ember from 'ember'; import layout from './template'; -const { on, run: { scheduleOnce }, inject: { service }} = Ember; +const { + on, + run: { scheduleOnce }, + inject: { service } +} = Ember; export default Ember.Component.extend({ layout, filestack: service(), - actions: { - handleSelection(data) { - if (this.get('onSelection')) { - this.get('onSelection')(data); - } - }, - - handleError(data) { - if (data.code === 101 && this.get('onClose')) { - this.get('onClose')(); - } else if (this.get('onError')) { - this.get('onError')(data); - } - }, - }, - - onSelection: null, - onError: null, - onClose: null, - options: {}, - - openFilepicker: on('didInsertElement', function() { - scheduleOnce('afterRender', this, function() { - this.get('filestack.promise').then( (filestack)=> { - let options = this.get('options'); - filestack.pick(options).then((data)=>{ - this.send('handleSelection',data); - }).catch((data)=>{ - this.send('handleError', data); - }); - }); - }); - }) + actions: { + handleSelection (data) { + if (this.get('onSelection')) { + this.get('onSelection')(data); + } + }, + + handleError (data) { + if (data.code === 101 && this.get('onClose')) { + this.get('onClose')(); + } else if (this.get('onError')) { + this.get('onError')(data); + } + }, + + handleClose () { + const oc = this.get('onClose'); + if (oc) { + oc(); + } + } + }, + + getCallClose () { + return () => { + this.send('handleClose'); + }; + }, + + onSelection: null, + onError: null, + onClose: null, + options: {}, + + openFilepicker: on('didInsertElement', function () { + scheduleOnce('afterRender', this, function () { + this.get('filestack.promise').then((filestack) => { + let options = this.get('options'); + options['onClose'] = options['onClose'] || this.getCallClose(); + filestack.pick(options).then((data) => { + this.send('handleSelection', data); + }).catch((data) => { + this.send('handleError', data); + }); + }); + }); + }) }); diff --git a/package.json b/package.json index 010bd1e..5f4ee40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-filestack", - "version": "0.0.2", + "version": "0.0.3", "description": "Ember Addon for https://filestack.com support", "keywords": [ "ember-addon" diff --git a/tests/dummy/app/controllers/application.js b/tests/dummy/app/controllers/application.js index 98d5ce5..c222ca1 100644 --- a/tests/dummy/app/controllers/application.js +++ b/tests/dummy/app/controllers/application.js @@ -7,23 +7,23 @@ export default Ember.Controller.extend({ accept: 'image/*' }, - actions : { - showPicker: function() { - this.set('showFilestack', true); - }, - hidePicker: function() { - this.set('showFilestack', false); - }, - fileSelected: function(data) { - this.set('pickedUrl',data["filesUploaded"][0].url); - this.send('hidePicker'); - }, - onClose: function() { - this.send('hidePicker'); - }, - onError: function(error) { - this.set('pickerError',error); - this.send('hidePicker'); - } - } + actions: { + showPicker: function () { + this.set('showFilestack', true); + }, + hidePicker: function () { + this.set('showFilestack', false); + }, + fileSelected: function (data) { + this.set('pickedUrl', data['filesUploaded'][0].url); + this.send('hidePicker'); + }, + onClose: function () { + this.send('hidePicker'); + }, + onError: function (error) { + this.set('pickerError', error); + this.send('hidePicker'); + } + } }); diff --git a/tests/integration/components/ember-filestack-picker/component-test.js b/tests/integration/components/ember-filestack-picker/component-test.js index 6e54952..938be21 100644 --- a/tests/integration/components/ember-filestack-picker/component-test.js +++ b/tests/integration/components/ember-filestack-picker/component-test.js @@ -6,15 +6,35 @@ moduleForComponent('ember-filestack-picker', 'Integration | Component | ember fi integration: true }); -test('it renders', function(assert) { - this.set('options',{fromSources: 'local_file_system'}); +test('it renders', function (assert) { + this.set('options', {fromSources: 'local_file_system'}); this.render(hbs`{{ember-filestack-picker options=options}}`); - return wait().then(function() { + return wait().then(function () { assert.equal(window.$('.fsp-picker').length, 1, 'pick modal is open'); // close any open pickers window.$('.fsp-picker__close-button').click(); }); }); + +test('it calls onClose', function (assert) { + this.set('options', {fromSources: 'local_file_system'}); + this.set('onClose', () => { + this.set('closed', true); + }); + + this.render(hbs`{{ember-filestack-picker onClose=onClose options=options}}`); + + return wait().then(() => { + assert.equal(window.$('.fsp-picker').length, 1, 'pick modal is open'); + + // close any open pickers + window.$('.fsp-picker__close-button').click(); + + return wait().then(() => { + assert.equal(this.get('closed'), true); + }); + }); +}); diff --git a/tests/unit/services/filestack-test.js b/tests/unit/services/filestack-test.js index 50c51db..d86aee8 100644 --- a/tests/unit/services/filestack-test.js +++ b/tests/unit/services/filestack-test.js @@ -5,26 +5,26 @@ moduleFor('service:filestack', 'Unit | Service | filestack', { needs: ['config:environment'] }); -test('it exists', function(assert) { +test('it exists', function (assert) { let service = this.subject(); assert.ok(service); }); -test('it resolves the promise ', function(assert) { - var service = this.subject(); - var promise = service.get('promise'); +test('it resolves the promise ', function (assert) { + var service = this.subject(); + var promise = service.get('promise'); - assert.ok(promise instanceof Ember.RSVP.Promise, "promise' value is an intance of proper RSVP.Promise"); + assert.ok(promise instanceof Ember.RSVP.Promise, "promise' value is an intance of proper RSVP.Promise"); - return promise.then(function(filepicker) { - assert.ok(!!filepicker, 'instance exists'); - assert.equal(typeof filepicker.pick, "function", 'The resolved filepicker object has pick method'); - }); + return promise.then(function (filepicker) { + assert.ok(!!filepicker, 'instance exists'); + assert.equal(typeof filepicker.pick, 'function', 'The resolved filepicker object has pick method'); + }); }); -test('it returns a proper filepicker instance', function(assert) { - var service = this.subject(); - return service.get('promise').then(function(filepicker) { - assert.equal(service.get('instance'), filepicker, "'instance' value is the resolved filepicker object"); - }); +test('it returns a proper filepicker instance', function (assert) { + var service = this.subject(); + return service.get('promise').then(function (filepicker) { + assert.equal(service.get('instance'), filepicker, "'instance' value is the resolved filepicker object"); + }); });