discourse/app/assets/javascripts/discourse/controllers/history.js.es6

197 lines
7.2 KiB
Plaintext
Raw Normal View History

2014-08-13 07:04:36 +08:00
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { categoryBadgeHTML } from 'discourse/helpers/category-link';
2015-08-14 01:56:46 +08:00
import computed from 'ember-addons/ember-computed-decorators';
2015-08-20 03:10:12 +08:00
import { propertyGreaterThan, propertyLessThan } from 'discourse/lib/computed';
2014-08-13 07:04:36 +08:00
function customTagArray(fieldName) {
return function() {
var val = this.get(fieldName);
if (!val) { return val; }
if (!Array.isArray(val)) { val = [val]; }
return val;
}.property(fieldName);
}
// This controller handles displaying of history
export default Ember.Controller.extend(ModalFunctionality, {
loading: true,
2014-10-28 05:18:10 +08:00
viewMode: "side_by_side",
2014-10-28 05:18:10 +08:00
_changeViewModeOnMobile: function() {
2016-02-19 00:53:25 +08:00
if (this.site.mobileView) { this.set("viewMode", "inline"); }
2014-10-28 05:18:10 +08:00
}.on("init"),
previousFeaturedLink: Em.computed.alias('model.featured_link_changes.previous'),
currentFeaturedLink: Em.computed.alias('model.featured_link_changes.current'),
previousTagChanges: customTagArray('model.tags_changes.previous'),
currentTagChanges: customTagArray('model.tags_changes.current'),
@computed('previousVersion', 'model.current_version', 'model.version_count')
revisionsText(previous, current, total) {
return I18n.t("post.revisions.controls.comparing_previous_to_current_out_of_total", {
previous, current, total
});
},
2015-08-14 01:56:46 +08:00
refresh(postId, postVersion) {
2014-03-12 01:51:26 +08:00
this.set("loading", true);
2015-08-20 03:10:12 +08:00
Discourse.Post.loadRevision(postId, postVersion).then(result => {
this.setProperties({ loading: false, model: result });
2013-12-12 10:41:34 +08:00
});
},
2015-08-14 01:56:46 +08:00
hide(postId, postVersion) {
2015-08-20 03:10:12 +08:00
Discourse.Post.hideRevision(postId, postVersion).then(() => this.refresh(postId, postVersion));
},
2015-08-14 01:56:46 +08:00
show(postId, postVersion) {
2015-08-20 03:10:12 +08:00
Discourse.Post.showRevision(postId, postVersion).then(() => this.refresh(postId, postVersion));
},
revert(post, postVersion) {
post.revertToRevision(postVersion).then((result) => {
this.refresh(post.get('id'), postVersion);
if (result.topic) {
post.set('topic.slug', result.topic.slug);
post.set('topic.title', result.topic.title);
post.set('topic.fancy_title', result.topic.fancy_title);
}
if (result.category_id) {
post.set('topic.category', Discourse.Category.findById(result.category_id));
}
this.send("closeModal");
}).catch(function(e) {
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors && e.jqXHR.responseJSON.errors[0]) {
bootbox.alert(e.jqXHR.responseJSON.errors[0]);
}
});
},
2015-08-20 03:10:12 +08:00
@computed('model.created_at')
createdAtDate(createdAt) {
return moment(createdAt).format("LLLL");
},
2015-08-14 01:56:46 +08:00
@computed('model.current_version')
2015-08-20 03:10:12 +08:00
previousVersion(current) {
return current - 1;
},
2015-08-14 01:56:46 +08:00
@computed('model.current_revision', 'model.previous_revision')
displayGoToPrevious(current, prev) {
return prev && current > prev;
},
displayRevisions: Ember.computed.gt("model.version_count", 2),
2015-08-20 03:10:12 +08:00
displayGoToFirst: propertyGreaterThan("model.current_revision", "model.first_revision"),
displayGoToNext: propertyLessThan("model.current_revision", "model.next_revision"),
displayGoToLast: propertyLessThan("model.current_revision", "model.next_revision"),
hideGoToFirst: Ember.computed.not("displayGoToFirst"),
hideGoToPrevious: Ember.computed.not("displayGoToPrevious"),
hideGoToNext: Ember.computed.not("displayGoToNext"),
hideGoToLast: Ember.computed.not("displayGoToLast"),
loadFirstDisabled: Ember.computed.or("loading", "hideGoToFirst"),
loadPreviousDisabled: Ember.computed.or("loading", "hideGoToPrevious"),
loadNextDisabled: Ember.computed.or("loading", "hideGoToNext"),
loadLastDisabled: Ember.computed.or("loading", "hideGoToLast"),
@computed('model.previous_hidden')
displayShow(prevHidden) {
2015-08-24 01:56:03 +08:00
return prevHidden && this.currentUser && this.currentUser.get('staff');
2015-08-14 01:56:46 +08:00
},
2015-08-20 03:10:12 +08:00
@computed('model.previous_hidden')
displayHide(prevHidden) {
2015-08-24 01:56:03 +08:00
return !prevHidden && this.currentUser && this.currentUser.get('staff');
2015-08-14 01:56:46 +08:00
},
@computed()
displayRevert() {
return this.currentUser && this.currentUser.get('staff');
},
2015-08-14 01:56:46 +08:00
isEitherRevisionHidden: Ember.computed.or("model.previous_hidden", "model.current_hidden"),
2015-08-14 01:56:46 +08:00
@computed('model.previous_hidden', 'model.current_hidden', 'displayingInline')
hiddenClasses(prevHidden, currentHidden, displayingInline) {
if (displayingInline) {
return this.get("isEitherRevisionHidden") ? "hidden-revision-either" : null;
} else {
var result = [];
2015-08-14 01:56:46 +08:00
if (prevHidden) { result.push("hidden-revision-previous"); }
if (currentHidden) { result.push("hidden-revision-current"); }
return result.join(" ");
}
2015-08-14 01:56:46 +08:00
},
2013-12-12 10:41:34 +08:00
displayingInline: Em.computed.equal("viewMode", "inline"),
displayingSideBySide: Em.computed.equal("viewMode", "side_by_side"),
displayingSideBySideMarkdown: Em.computed.equal("viewMode", "side_by_side_markdown"),
2015-08-20 03:10:12 +08:00
@computed("displayingInline")
inlineClass(displayingInline) { return displayingInline ? "btn-primary" : ""; },
@computed("displayingSideBySide")
sideBySideClass(displayingSideBySide) { return displayingSideBySide ? "btn-primary" : ""; },
@computed("displayingSideBySideMarkdown")
sideBySideMarkdownClass(displayingSideBySideMarkdown) { return displayingSideBySideMarkdown ? "btn-primary" : ""; },
2015-08-14 01:56:46 +08:00
@computed('model.category_id_changes')
previousCategory(changes) {
if (changes) {
var category = Discourse.Category.findById(changes["previous"]);
return categoryBadgeHTML(category, { allowUncategorized: true });
}
2015-08-14 01:56:46 +08:00
},
2015-08-14 01:56:46 +08:00
@computed('model.category_id_changes')
currentCategory(changes) {
if (changes) {
var category = Discourse.Category.findById(changes["current"]);
return categoryBadgeHTML(category, { allowUncategorized: true });
}
2015-08-14 01:56:46 +08:00
},
2014-05-13 20:53:11 +08:00
2015-08-14 01:56:46 +08:00
@computed('model.wiki_changes')
wikiDisabled(changes) {
return changes && !changes['current'];
2015-08-14 01:56:46 +08:00
},
2015-08-14 01:56:46 +08:00
@computed('model.post_type_changes')
postTypeDisabled(changes) {
return (changes && changes['current'] !== this.site.get('post_types.moderator_action'));
2015-08-14 01:56:46 +08:00
},
2015-08-14 01:56:46 +08:00
@computed('viewMode', 'model.title_changes')
titleDiff(viewMode) {
if (viewMode === "side_by_side_markdown") { viewMode = "side_by_side"; }
2015-08-14 01:56:46 +08:00
return this.get("model.title_changes." + viewMode);
},
2015-08-14 01:56:46 +08:00
@computed('viewMode', 'model.body_changes')
bodyDiff(viewMode) {
return this.get("model.body_changes." + viewMode);
},
2013-12-12 10:41:34 +08:00
actions: {
2015-08-20 03:10:12 +08:00
loadFirstVersion() { this.refresh(this.get("model.post_id"), this.get("model.first_revision")); },
loadPreviousVersion() { this.refresh(this.get("model.post_id"), this.get("model.previous_revision")); },
loadNextVersion() { this.refresh(this.get("model.post_id"), this.get("model.next_revision")); },
loadLastVersion() { this.refresh(this.get("model.post_id"), this.get("model.last_revision")); },
2015-08-20 03:10:12 +08:00
hideVersion() { this.hide(this.get("model.post_id"), this.get("model.current_revision")); },
showVersion() { this.show(this.get("model.post_id"), this.get("model.current_revision")); },
revertToVersion() { this.revert(this.get("post"), this.get("model.current_revision")); },
2015-08-20 03:10:12 +08:00
displayInline() { this.set("viewMode", "inline"); },
displaySideBySide() { this.set("viewMode", "side_by_side"); },
displaySideBySideMarkdown() { this.set("viewMode", "side_by_side_markdown"); }
}
});