Skip to content

Commit

Permalink
corrected issue calling ‘onClose’, cleaned syntax styling
Browse files Browse the repository at this point in the history
  • Loading branch information
mminkoff committed Jun 1, 2017
1 parent dbe6c6b commit 2889d5a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 71 deletions.
86 changes: 52 additions & 34 deletions addon/components/ember-filestack-picker/component.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
})
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
38 changes: 19 additions & 19 deletions tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
28 changes: 14 additions & 14 deletions tests/unit/services/filestack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

0 comments on commit 2889d5a

Please sign in to comment.