2014-11-20 01:27:45 +01:00
|
|
|
import PaginationMixin from 'ghost/mixins/pagination-controller';
|
2014-12-01 23:45:45 +01:00
|
|
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
2014-11-20 01:27:45 +01:00
|
|
|
|
|
|
|
var TagsController = Ember.ArrayController.extend(PaginationMixin, {
|
|
|
|
tags: Ember.computed.alias('model'),
|
2014-12-01 23:45:45 +01:00
|
|
|
|
2014-11-20 01:27:45 +01:00
|
|
|
activeTag: null,
|
2014-12-01 23:45:45 +01:00
|
|
|
activeTagNameScratch: boundOneWay('activeTag.name'),
|
|
|
|
activeTagSlugScratch: boundOneWay('activeTag.slug'),
|
|
|
|
activeTagDescriptionScratch: boundOneWay('activeTag.description'),
|
|
|
|
|
2014-12-09 15:25:20 +01:00
|
|
|
// Tag properties that should not be set to the empty string
|
|
|
|
requiredTagProperties: ['name', 'slug'],
|
|
|
|
|
2014-11-20 01:27:45 +01:00
|
|
|
init: function (options) {
|
|
|
|
options = options || {};
|
|
|
|
options.modelType = 'tag';
|
|
|
|
this._super(options);
|
|
|
|
},
|
2014-12-01 23:45:45 +01:00
|
|
|
|
2014-12-09 15:25:20 +01:00
|
|
|
saveActiveTagProperty: function (propKey, newValue) {
|
2014-12-01 23:45:45 +01:00
|
|
|
var activeTag = this.get('activeTag'),
|
2014-12-09 15:25:20 +01:00
|
|
|
currentValue = activeTag.get(propKey),
|
|
|
|
requiredTagProps = this.get('requiredTagProperties'),
|
|
|
|
self = this,
|
|
|
|
tagName;
|
|
|
|
|
|
|
|
newValue = newValue.trim();
|
|
|
|
// Quit if value is empty for a required property
|
|
|
|
if (!newValue && requiredTagProps.contains(propKey)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Quit if there was no change
|
|
|
|
if (newValue === currentValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
activeTag.set(propKey, newValue);
|
|
|
|
|
|
|
|
tagName = activeTag.get('name');
|
|
|
|
// don't save a new tag until it has a name
|
|
|
|
if (!tagName) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-01 23:45:45 +01:00
|
|
|
|
|
|
|
activeTag.save().then(function () {
|
2014-12-09 15:25:20 +01:00
|
|
|
self.notifications.showSuccess('Saved ' + tagName);
|
2014-12-01 23:45:45 +01:00
|
|
|
}).catch(function (error) {
|
|
|
|
self.notifications.showAPIError(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-11-20 01:27:45 +01:00
|
|
|
actions: {
|
|
|
|
newTag: function () {
|
|
|
|
this.set('activeTag', this.store.createRecord('tag'));
|
|
|
|
this.send('openSettingsMenu');
|
|
|
|
},
|
2014-12-01 23:45:45 +01:00
|
|
|
|
2014-11-20 01:27:45 +01:00
|
|
|
editTag: function (tag) {
|
|
|
|
this.set('activeTag', tag);
|
|
|
|
this.send('openSettingsMenu');
|
|
|
|
},
|
2014-12-01 23:45:45 +01:00
|
|
|
|
2014-11-20 01:27:45 +01:00
|
|
|
deleteTag: function (tag) {
|
|
|
|
var name = tag.get('name'),
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
this.send('closeSettingsMenu');
|
|
|
|
|
|
|
|
tag.destroyRecord().then(function () {
|
|
|
|
self.notifications.showSuccess('Deleted ' + name);
|
|
|
|
}).catch(function (error) {
|
|
|
|
self.notifications.showAPIError(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-12-01 23:45:45 +01:00
|
|
|
saveActiveTagName: function (name) {
|
2014-12-09 15:25:20 +01:00
|
|
|
this.saveActiveTagProperty('name', name);
|
2014-12-01 23:45:45 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
saveActiveTagSlug: function (slug) {
|
2014-12-09 15:25:20 +01:00
|
|
|
this.saveActiveTagProperty('slug', slug);
|
2014-12-01 23:45:45 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
saveActiveTagDescription: function (description) {
|
2014-12-09 15:25:20 +01:00
|
|
|
this.saveActiveTagProperty('description', description);
|
2014-11-20 01:27:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default TagsController;
|