2015-10-28 14:36:45 +03:00
|
|
|
/* global key */
|
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2015-08-28 19:35:06 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
import {request as ajax} from 'ic-ajax';
|
|
|
|
|
2015-11-18 14:58:26 +03:00
|
|
|
const {$, Component, RSVP, computed, inject, observer} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
const {filterBy} = computed;
|
|
|
|
|
|
|
|
export default Component.extend({
|
2015-08-28 19:35:06 +03:00
|
|
|
|
|
|
|
selection: null,
|
|
|
|
content: [],
|
|
|
|
isLoading: false,
|
2015-09-02 18:32:31 +03:00
|
|
|
contentExpiry: 10 * 1000,
|
2015-08-28 19:35:06 +03:00
|
|
|
contentExpiresAt: false,
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
posts: filterBy('content', 'category', 'Posts'),
|
|
|
|
pages: filterBy('content', 'category', 'Pages'),
|
|
|
|
users: filterBy('content', 'category', 'Users'),
|
|
|
|
tags: filterBy('content', 'category', 'Tags'),
|
|
|
|
|
|
|
|
_store: inject.service('store'),
|
|
|
|
_routing: inject.service('-routing'),
|
2015-08-28 19:35:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_selectize: computed(function () {
|
2015-08-28 19:35:06 +03:00
|
|
|
return this.$('select')[0].selectize;
|
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
refreshContent() {
|
|
|
|
let promises = [];
|
|
|
|
let now = new Date();
|
|
|
|
let contentExpiry = this.get('contentExpiry');
|
|
|
|
let contentExpiresAt = this.get('contentExpiresAt');
|
2015-08-28 19:35:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (this.get('isLoading') || contentExpiresAt > now) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-28 19:35:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
this.set('isLoading', true);
|
2015-08-28 19:35:06 +03:00
|
|
|
promises.pushObject(this._loadPosts());
|
|
|
|
promises.pushObject(this._loadUsers());
|
2015-10-22 18:03:52 +03:00
|
|
|
promises.pushObject(this._loadTags());
|
2015-08-28 19:35:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
RSVP.all(promises).then(() => { }).finally(() => {
|
|
|
|
this.set('isLoading', false);
|
|
|
|
this.set('contentExpiresAt', new Date(now.getTime() + contentExpiry));
|
2015-08-28 19:35:06 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_loadPosts() {
|
|
|
|
let store = this.get('_store');
|
|
|
|
let postsUrl = `${store.adapterFor('post').urlForQuery({}, 'post')}/`;
|
|
|
|
let postsQuery = {fields: 'id,title,page', limit: 'all', status: 'all', staticPages: 'all'};
|
|
|
|
let content = this.get('content');
|
|
|
|
|
|
|
|
return ajax(postsUrl, {data: postsQuery}).then((posts) => {
|
|
|
|
content.removeObjects(this.get('posts'));
|
|
|
|
content.removeObjects(this.get('pages'));
|
|
|
|
content.pushObjects(posts.posts.map((post) => {
|
2015-08-28 19:35:06 +03:00
|
|
|
return {
|
2015-10-22 18:03:52 +03:00
|
|
|
id: `post.${post.id}`,
|
2015-08-28 19:35:06 +03:00
|
|
|
title: post.title,
|
|
|
|
category: post.page ? 'Pages' : 'Posts'
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_loadUsers() {
|
|
|
|
let store = this.get('_store');
|
|
|
|
let usersUrl = `${store.adapterFor('user').urlForQuery({}, 'user')}/`;
|
|
|
|
let usersQuery = {fields: 'name,slug', limit: 'all'};
|
|
|
|
let content = this.get('content');
|
2015-08-28 19:35:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return ajax(usersUrl, {data: usersQuery}).then((users) => {
|
|
|
|
content.removeObjects(this.get('users'));
|
|
|
|
content.pushObjects(users.users.map((user) => {
|
2015-08-28 19:35:06 +03:00
|
|
|
return {
|
2015-10-22 18:03:52 +03:00
|
|
|
id: `user.${user.slug}`,
|
2015-08-28 19:35:06 +03:00
|
|
|
title: user.name,
|
|
|
|
category: 'Users'
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_loadTags() {
|
|
|
|
let store = this.get('_store');
|
|
|
|
let tagsUrl = `${store.adapterFor('tag').urlForQuery({}, 'tag')}/`;
|
|
|
|
let tagsQuery = {fields: 'name,slug', limit: 'all'};
|
|
|
|
let content = this.get('content');
|
2015-10-22 18:03:52 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return ajax(tagsUrl, {data: tagsQuery}).then((tags) => {
|
|
|
|
content.removeObjects(this.get('tags'));
|
|
|
|
content.pushObjects(tags.tags.map((tag) => {
|
2015-10-22 18:03:52 +03:00
|
|
|
return {
|
|
|
|
id: `tag.${tag.slug}`,
|
|
|
|
title: tag.name,
|
|
|
|
category: 'Tags'
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_keepSelectionClear: observer('selection', function () {
|
2015-09-02 21:34:54 +03:00
|
|
|
if (this.get('selection') !== null) {
|
|
|
|
this.set('selection', null);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_setKeymasterScope() {
|
2015-08-28 19:35:06 +03:00
|
|
|
key.setScope('search-input');
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_resetKeymasterScope() {
|
2015-08-28 19:35:06 +03:00
|
|
|
key.setScope('default');
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
willDestroy() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
2015-08-28 19:35:06 +03:00
|
|
|
this._resetKeymasterScope();
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
openSelected(selected) {
|
|
|
|
let transition = null;
|
2015-08-28 19:35:06 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!selected) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-28 19:35:06 +03:00
|
|
|
|
|
|
|
if (selected.category === 'Posts' || selected.category === 'Pages') {
|
2015-10-22 18:03:52 +03:00
|
|
|
let id = selected.id.replace('post.', '');
|
2015-10-28 14:36:45 +03:00
|
|
|
transition = this.get('_routing.router').transitionTo('editor.edit', id);
|
2015-08-28 19:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selected.category === 'Users') {
|
2015-10-22 18:03:52 +03:00
|
|
|
let id = selected.id.replace('user.', '');
|
2015-10-28 14:36:45 +03:00
|
|
|
transition = this.get('_routing.router').transitionTo('team.user', id);
|
2015-10-22 18:03:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (selected.category === 'Tags') {
|
|
|
|
let id = selected.id.replace('tag.', '');
|
2015-10-28 14:36:45 +03:00
|
|
|
transition = this.get('_routing.router').transitionTo('settings.tags.tag', id);
|
2015-08-28 19:35:06 +03:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
transition.then(() => {
|
|
|
|
if (this.get('_selectize').$control_input.is(':focus')) {
|
|
|
|
this._setKeymasterScope();
|
2015-08-28 19:35:06 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
focusInput() {
|
2015-08-28 19:35:06 +03:00
|
|
|
this.get('_selectize').focus();
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
onInit() {
|
|
|
|
let selectize = this.get('_selectize');
|
|
|
|
let html = '<div class="dropdown-empty-message">Nothing found…</div>';
|
2015-09-02 11:23:20 +03:00
|
|
|
|
|
|
|
selectize.$empty_results_container = $(html);
|
|
|
|
selectize.$empty_results_container.hide();
|
|
|
|
selectize.$dropdown.append(selectize.$empty_results_container);
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
onFocus() {
|
2015-08-28 19:35:06 +03:00
|
|
|
this._setKeymasterScope();
|
|
|
|
this.refreshContent();
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
onBlur() {
|
|
|
|
let selectize = this.get('_selectize');
|
2015-09-02 11:23:20 +03:00
|
|
|
|
2015-08-28 19:35:06 +03:00
|
|
|
this._resetKeymasterScope();
|
2015-09-02 11:23:20 +03:00
|
|
|
selectize.$empty_results_container.hide();
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
onType() {
|
|
|
|
let selectize = this.get('_selectize');
|
2015-09-02 11:23:20 +03:00
|
|
|
|
|
|
|
if (!selectize.hasOptions) {
|
|
|
|
selectize.open();
|
|
|
|
selectize.$empty_results_container.show();
|
|
|
|
} else {
|
|
|
|
selectize.$empty_results_container.hide();
|
|
|
|
}
|
2015-08-28 19:35:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|