discourse/app/assets/javascripts/admin/models/site_customization.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
Our data model for interacting with site customizations.
@class SiteCustomization
@extends Discourse.Model
@namespace Discourse
@module Discourse
**/
Discourse.SiteCustomization = Discourse.Model.extend({
trackedProperties: [
'enabled', 'name',
'stylesheet', 'header', 'footer',
'mobile_stylesheet', 'mobile_header', 'mobile_footer',
'head_tag', 'body_tag'
],
description: function() {
return "" + this.name + (this.enabled ? ' (*)' : '');
}.property('selected', 'name', 'enabled'),
changed: function() {
2014-11-10 15:51:55 -05:00
var self = this;
2014-11-10 15:51:55 -05:00
if (!this.originals) { return false; }
2014-11-10 15:51:55 -05:00
var changed = _.some(this.trackedProperties, function (p) {
return self.originals[p] !== self.get(p);
});
2014-11-10 15:51:55 -05:00
if (changed) { this.set('savingStatus', ''); }
return changed;
}.property('enabled', 'name', 'originals',
'stylesheet', 'header', 'footer',
'mobile_stylesheet', 'mobile_header', 'mobile_footer',
'head_tag', 'body_tag'),
startTrackingChanges: function() {
2014-11-10 15:51:55 -05:00
var self = this;
var originals = {};
2014-11-10 15:51:55 -05:00
_.each(this.trackedProperties, function (prop) {
originals[prop] = self.get(prop);
});
this.set('originals', originals);
}.on('init'),
2014-11-10 15:51:55 -05:00
previewUrl: function() { return "/?preview-style=" + this.get('key'); }.property('key'),
disableSave: function() { return !this.get('changed') || this.get('saving'); }.property('changed'),
save: function() {
this.set('savingStatus', I18n.t('saving'));
this.set('saving',true);
var data = {
name: this.name,
enabled: this.enabled,
stylesheet: this.stylesheet,
header: this.header,
2014-11-10 15:51:55 -05:00
footer: this.footer,
mobile_stylesheet: this.mobile_stylesheet,
mobile_header: this.mobile_header,
mobile_footer: this.mobile_footer,
head_tag: this.head_tag,
body_tag: this.body_tag
};
var siteCustomization = this;
return Discourse.ajax("/admin/site_customizations" + (this.id ? '/' + this.id : ''), {
data: { site_customization: data },
type: this.id ? 'PUT' : 'POST'
}).then(function (result) {
2013-06-19 01:18:22 -04:00
if (!siteCustomization.id) {
siteCustomization.set('id', result.id);
siteCustomization.set('key', result.key);
}
siteCustomization.set('savingStatus', I18n.t('saved'));
siteCustomization.set('saving',false);
siteCustomization.startTrackingChanges();
});
},
destroy: function() {
2014-11-10 15:51:55 -05:00
if (!this.id) return;
return Discourse.ajax("/admin/site_customizations/" + this.id, { type: 'DELETE' });
}
});
var SiteCustomizations = Ember.ArrayProxy.extend({
selectedItemChanged: function() {
var selected = this.get('selectedItem');
2014-11-10 15:51:55 -05:00
_.each(this.get('content'), function (i) {
i.set('selected', selected === i);
});
}.observes('selectedItem')
});
Discourse.SiteCustomization.reopenClass({
findAll: function() {
return Discourse.ajax("/admin/site_customizations").then(function (data) {
var content = [];
if (data) {
content = data.site_customizations.map(function(c) {
return Discourse.SiteCustomization.create(c);
});
}
return SiteCustomizations.create({ content: content });
});
}
});