2018-01-12 21:09:10 +03:00
|
|
|
/* global key */
|
2017-08-02 10:05:59 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import ShortcutsMixin from 'ghost-admin/mixins/shortcuts';
|
|
|
|
import {bind} from '@ember/runloop';
|
|
|
|
import {or} from '@ember/object/computed';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-08-02 10:05:59 +03:00
|
|
|
|
|
|
|
const ONE_COLUMN_WIDTH = 540;
|
|
|
|
const TWO_COLUMN_WIDTH = 940;
|
|
|
|
|
|
|
|
export default Component.extend(ShortcutsMixin, {
|
2017-10-30 12:38:01 +03:00
|
|
|
resizeDetector: service(),
|
|
|
|
unsplash: service(),
|
|
|
|
ui: service(),
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
shortcuts: null,
|
2017-08-02 10:05:59 +03:00
|
|
|
tagName: '',
|
|
|
|
zoomedPhoto: null,
|
2018-08-09 19:55:11 +03:00
|
|
|
searchTerm: null,
|
2017-08-02 10:05:59 +03:00
|
|
|
|
|
|
|
// closure actions
|
|
|
|
close() {},
|
2018-08-09 19:55:11 +03:00
|
|
|
select() {},
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2019-01-10 18:00:07 +03:00
|
|
|
sideNavHidden: or('ui.{isFullScreen,showMobileMenu}'),
|
2017-08-02 10:05:59 +03:00
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.shortcuts = {
|
2018-01-12 21:09:10 +03:00
|
|
|
escape: {action: 'handleEscape', scope: 'all'}
|
2017-11-24 21:53:19 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-08-09 19:55:11 +03:00
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (this.searchTerm !== this._searchTerm) {
|
|
|
|
this.unsplash.updateSearch(this.searchTerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._searchTerm = this.searchTerm;
|
|
|
|
},
|
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._resizeCallback = bind(this, this._handleResize);
|
2019-03-06 16:53:54 +03:00
|
|
|
this.resizeDetector.setup('[data-unsplash]', this._resizeCallback);
|
2017-08-02 10:05:59 +03:00
|
|
|
this.registerShortcuts();
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.resizeDetector.teardown('[data-unsplash]', this._resizeCallback);
|
2017-08-02 10:05:59 +03:00
|
|
|
this.removeShortcuts();
|
2018-01-12 21:09:10 +03:00
|
|
|
this.send('resetKeyScope');
|
|
|
|
this._super(...arguments);
|
2017-08-02 10:05:59 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
loadNextPage() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.unsplash.loadNextPage();
|
2017-08-02 10:05:59 +03:00
|
|
|
},
|
|
|
|
|
2018-08-09 16:15:47 +03:00
|
|
|
search(term) {
|
|
|
|
this.unsplash.updateSearch(term);
|
|
|
|
this.send('closeZoom');
|
|
|
|
},
|
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
zoomPhoto(photo) {
|
|
|
|
this.set('zoomedPhoto', photo);
|
|
|
|
},
|
|
|
|
|
|
|
|
closeZoom() {
|
|
|
|
this.set('zoomedPhoto', null);
|
|
|
|
},
|
|
|
|
|
2018-08-09 19:55:11 +03:00
|
|
|
select(photo) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.unsplash.triggerDownload(photo);
|
2018-08-09 19:55:11 +03:00
|
|
|
|
|
|
|
let selectParams = {
|
2019-08-29 11:41:43 +03:00
|
|
|
src: photo.urls.regular.replace(/&w=1080/, '&w=2000'),
|
2020-06-15 15:16:10 +03:00
|
|
|
width: photo.width,
|
|
|
|
height: photo.height,
|
2018-08-09 19:55:11 +03:00
|
|
|
alt: photo.description || '',
|
|
|
|
caption: `Photo by <a href="${photo.user.links.html}?utm_source=ghost&utm_medium=referral&utm_campaign=api-credit">${photo.user.name}</a> / <a href="https://unsplash.com/?utm_source=ghost&utm_medium=referral&utm_campaign=api-credit">Unsplash</a>`
|
|
|
|
};
|
|
|
|
this.select(selectParams);
|
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
this.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
retry() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.unsplash.retryLastRequest();
|
2017-08-02 10:05:59 +03:00
|
|
|
},
|
|
|
|
|
2018-01-12 21:09:10 +03:00
|
|
|
setKeyScope() {
|
|
|
|
key.setScope('unsplash');
|
|
|
|
},
|
|
|
|
|
|
|
|
resetKeyScope() {
|
|
|
|
key.setScope('default');
|
|
|
|
},
|
|
|
|
|
2017-08-02 10:05:59 +03:00
|
|
|
handleEscape() {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.zoomedPhoto) {
|
2018-08-09 16:15:47 +03:00
|
|
|
return this.send('closeZoom');
|
2017-08-02 10:05:59 +03:00
|
|
|
}
|
2018-08-09 16:15:47 +03:00
|
|
|
|
|
|
|
this.close();
|
2017-08-02 10:05:59 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleResize(element) {
|
|
|
|
let width = element.clientWidth;
|
|
|
|
let columns = 3;
|
|
|
|
|
|
|
|
if (width <= ONE_COLUMN_WIDTH) {
|
|
|
|
columns = 1;
|
|
|
|
} else if (width <= TWO_COLUMN_WIDTH) {
|
|
|
|
columns = 2;
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.unsplash.changeColumnCount(columns);
|
2017-08-02 10:05:59 +03:00
|
|
|
}
|
|
|
|
});
|