Convert all CoffeeScript to Javascript. See:

http://meta.discourse.org/t/is-it-better-for-discourse-to-use-javascript-or-coffeescript/3153
This commit is contained in:
Robin Ward
2013-02-20 13:15:50 -05:00
parent 68ad545f0f
commit f661fa609e
407 changed files with 13226 additions and 8953 deletions
@@ -0,0 +1,188 @@
(function() {
window.Discourse.AdminUser = Discourse.Model.extend({
deleteAllPosts: function() {
this.set('can_delete_all_posts', false);
return jQuery.ajax("/admin/users/" + (this.get('id')) + "/delete_all_posts", {
type: 'PUT'
});
},
/* Revoke the user's admin access
*/
revokeAdmin: function() {
this.set('admin', false);
this.set('can_grant_admin', true);
this.set('can_revoke_admin', false);
return jQuery.ajax("/admin/users/" + (this.get('id')) + "/revoke_admin", {
type: 'PUT'
});
},
grantAdmin: function() {
this.set('admin', true);
this.set('can_grant_admin', false);
this.set('can_revoke_admin', true);
return jQuery.ajax("/admin/users/" + (this.get('id')) + "/grant_admin", {
type: 'PUT'
});
},
/* Revoke the user's moderation access
*/
revokeModeration: function() {
this.set('moderator', false);
this.set('can_grant_moderation', true);
this.set('can_revoke_moderation', false);
return jQuery.ajax("/admin/users/" + (this.get('id')) + "/revoke_moderation", {
type: 'PUT'
});
},
grantModeration: function() {
this.set('moderator', true);
this.set('can_grant_moderation', false);
this.set('can_revoke_moderation', true);
return jQuery.ajax("/admin/users/" + (this.get('id')) + "/grant_moderation", {
type: 'PUT'
});
},
refreshBrowsers: function() {
jQuery.ajax("/admin/users/" + (this.get('id')) + "/refresh_browsers", {
type: 'POST'
});
return bootbox.alert("Message sent to all clients!");
},
approve: function() {
this.set('can_approve', false);
this.set('approved', true);
this.set('approved_by', Discourse.get('currentUser'));
return jQuery.ajax("/admin/users/" + (this.get('id')) + "/approve", {
type: 'PUT'
});
},
username_lower: (function() {
return this.get('username').toLowerCase();
}).property('username'),
trustLevel: (function() {
return Discourse.get('site.trust_levels').findProperty('id', this.get('trust_level'));
}).property('trust_level'),
canBan: (function() {
return !this.admin && !this.moderator;
}).property('admin', 'moderator'),
banDuration: (function() {
var banned_at, banned_till;
banned_at = Date.create(this.banned_at);
banned_till = Date.create(this.banned_till);
return "" + (banned_at.short()) + " - " + (banned_till.short());
}).property('banned_till', 'banned_at'),
ban: function() {
var duration,
_this = this;
if (duration = parseInt(window.prompt(Em.String.i18n('admin.user.ban_duration')), 10)) {
if (duration > 0) {
return jQuery.ajax("/admin/users/" + this.id + "/ban", {
type: 'PUT',
data: {
duration: duration
},
success: function() {
window.location.reload();
},
error: function(e) {
var error;
error = Em.String.i18n('admin.user.ban_failed', {
error: "http: " + e.status + " - " + e.body
});
bootbox.alert(error);
}
});
}
}
},
unban: function() {
var _this = this;
return jQuery.ajax("/admin/users/" + this.id + "/unban", {
type: 'PUT',
success: function() {
window.location.reload();
},
error: function(e) {
var error;
error = Em.String.i18n('admin.user.unban_failed', {
error: "http: " + e.status + " - " + e.body
});
bootbox.alert(error);
}
});
},
impersonate: function() {
var _this = this;
return jQuery.ajax("/admin/impersonate", {
type: 'POST',
data: {
username_or_email: this.get('username')
},
success: function() {
document.location = "/";
},
error: function(e) {
_this.set('loading', false);
if (e.status === 404) {
return bootbox.alert(Em.String.i18n('admin.impersonate.not_found'));
} else {
return bootbox.alert(Em.String.i18n('admin.impersonate.invalid'));
}
}
});
}
});
window.Discourse.AdminUser.reopenClass({
create: function(result) {
result = this._super(result);
return result;
},
bulkApprove: function(users) {
users.each(function(user) {
user.set('approved', true);
user.set('can_approve', false);
return user.set('selected', false);
});
return jQuery.ajax("/admin/users/approve-bulk", {
type: 'PUT',
data: {
users: users.map(function(u) {
return u.id;
})
}
});
},
find: function(username) {
var promise;
promise = new RSVP.Promise();
jQuery.ajax({
url: "/admin/users/" + username,
success: function(result) {
return promise.resolve(Discourse.AdminUser.create(result));
}
});
return promise;
},
findAll: function(query, filter) {
var result;
result = Em.A();
jQuery.ajax({
url: "/admin/users/list/" + query + ".json",
data: {
filter: filter
},
success: function(users) {
return users.each(function(u) {
return result.pushObject(Discourse.AdminUser.create(u));
});
}
});
return result;
}
});
}).call(this);
@@ -1,137 +0,0 @@
window.Discourse.AdminUser = Discourse.Model.extend
deleteAllPosts: ->
@set('can_delete_all_posts', false)
$.ajax "/admin/users/#{@get('id')}/delete_all_posts", type: 'PUT'
# Revoke the user's admin access
revokeAdmin: ->
@set('admin',false)
@set('can_grant_admin',true)
@set('can_revoke_admin',false)
$.ajax "/admin/users/#{@get('id')}/revoke_admin", type: 'PUT'
grantAdmin: ->
@set('admin',true)
@set('can_grant_admin',false)
@set('can_revoke_admin',true)
$.ajax "/admin/users/#{@get('id')}/grant_admin", type: 'PUT'
# Revoke the user's moderation access
revokeModeration: ->
@set('moderator',false)
@set('can_grant_moderation',true)
@set('can_revoke_moderation',false)
$.ajax "/admin/users/#{@get('id')}/revoke_moderation", type: 'PUT'
grantModeration: ->
@set('moderator',true)
@set('can_grant_moderation',false)
@set('can_revoke_moderation',true)
$.ajax "/admin/users/#{@get('id')}/grant_moderation", type: 'PUT'
refreshBrowsers: ->
$.ajax "/admin/users/#{@get('id')}/refresh_browsers",
type: 'POST'
bootbox.alert("Message sent to all clients!")
approve: ->
@set('can_approve', false)
@set('approved', true)
@set('approved_by', Discourse.get('currentUser'))
$.ajax "/admin/users/#{@get('id')}/approve", type: 'PUT'
username_lower:(->
@get('username').toLowerCase()
).property('username')
trustLevel: (->
Discourse.get('site.trust_levels').findProperty('id', @get('trust_level'))
).property('trust_level')
canBan: ( ->
!@admin && !@moderator
).property('admin','moderator')
banDuration: (->
banned_at = Date.create(@banned_at)
banned_till = Date.create(@banned_till)
"#{banned_at.short()} - #{banned_till.short()}"
).property('banned_till', 'banned_at')
ban: ->
debugger
if duration = parseInt(window.prompt(Em.String.i18n('admin.user.ban_duration')))
if duration > 0
$.ajax "/admin/users/#{@id}/ban",
type: 'PUT'
data:
duration: duration
success: ->
window.location.reload()
return
error: (e) =>
error = Em.String.i18n('admin.user.ban_failed', error: "http: #{e.status} - #{e.body}")
bootbox.alert error
return
unban: ->
$.ajax "/admin/users/#{@id}/unban",
type: 'PUT'
success: ->
window.location.reload()
return
error: (e) =>
error = Em.String.i18n('admin.user.unban_failed', error: "http: #{e.status} - #{e.body}")
bootbox.alert error
return
impersonate: ->
$.ajax "/admin/impersonate"
type: 'POST'
data:
username_or_email: @get('username')
success: ->
document.location = "/"
error: (e) =>
@set('loading', false)
if e.status == 404
bootbox.alert Em.String.i18n('admin.impersonate.not_found')
else
bootbox.alert Em.String.i18n('admin.impersonate.invalid')
window.Discourse.AdminUser.reopenClass
create: (result) ->
result = @_super(result)
result
bulkApprove: (users) ->
users.each (user) ->
user.set('approved', true)
user.set('can_approve', false)
user.set('selected', false)
$.ajax "/admin/users/approve-bulk",
type: 'PUT'
data: {users: users.map (u) -> u.id}
find: (username)->
promise = new RSVP.Promise()
$.ajax
url: "/admin/users/#{username}"
success: (result) -> promise.resolve(Discourse.AdminUser.create(result))
promise
findAll: (query, filter)->
result = Em.A()
$.ajax
url: "/admin/users/list/#{query}.json"
data: {filter: filter}
success: (users) ->
users.each (u) -> result.pushObject(Discourse.AdminUser.create(u))
result
@@ -0,0 +1,30 @@
(function() {
window.Discourse.EmailLog = Discourse.Model.extend({});
window.Discourse.EmailLog.reopenClass({
create: function(attrs) {
if (attrs.user) {
attrs.user = Discourse.AdminUser.create(attrs.user);
}
return this._super(attrs);
},
findAll: function(filter) {
var result;
result = Em.A();
jQuery.ajax({
url: "/admin/email_logs.json",
data: {
filter: filter
},
success: function(logs) {
return logs.each(function(log) {
return result.pushObject(Discourse.EmailLog.create(log));
});
}
});
return result;
}
});
}).call(this);
@@ -1,17 +0,0 @@
window.Discourse.EmailLog = Discourse.Model.extend({})
window.Discourse.EmailLog.reopenClass
create: (attrs) ->
attrs.user = Discourse.AdminUser.create(attrs.user) if attrs.user
@_super(attrs)
findAll: (filter)->
result = Em.A()
$.ajax
url: "/admin/email_logs.json"
data: {filter: filter}
success: (logs) ->
logs.each (log) -> result.pushObject(Discourse.EmailLog.create(log))
result
@@ -0,0 +1,109 @@
(function() {
window.Discourse.FlaggedPost = Discourse.Post.extend({
flaggers: (function() {
var r,
_this = this;
r = [];
this.post_actions.each(function(a) {
return r.push(_this.userLookup[a.user_id]);
});
return r;
}).property(),
messages: (function() {
var r,
_this = this;
r = [];
this.post_actions.each(function(a) {
if (a.message) {
return r.push({
user: _this.userLookup[a.user_id],
message: a.message
});
}
});
return r;
}).property(),
lastFlagged: (function() {
return this.post_actions[0].created_at;
}).property(),
user: (function() {
return this.userLookup[this.user_id];
}).property(),
topicHidden: (function() {
return this.get('topic_visible') === 'f';
}).property('topic_hidden'),
deletePost: function() {
var promise;
promise = new RSVP.Promise();
if (this.get('post_number') === "1") {
return jQuery.ajax("/t/" + this.topic_id, {
type: 'DELETE',
cache: false,
success: function() {
return promise.resolve();
},
error: function(e) {
return promise.reject();
}
});
} else {
return jQuery.ajax("/posts/" + this.id, {
type: 'DELETE',
cache: false,
success: function() {
return promise.resolve();
},
error: function(e) {
return promise.reject();
}
});
}
},
clearFlags: function() {
var promise;
promise = new RSVP.Promise();
jQuery.ajax("/admin/flags/clear/" + this.id, {
type: 'POST',
cache: false,
success: function() {
return promise.resolve();
},
error: function(e) {
return promise.reject();
}
});
return promise;
},
hiddenClass: (function() {
if (this.get('hidden') === "t") {
return "hidden-post";
}
}).property()
});
window.Discourse.FlaggedPost.reopenClass({
findAll: function(filter) {
var result;
result = Em.A();
jQuery.ajax({
url: "/admin/flags/" + filter + ".json",
success: function(data) {
var userLookup;
userLookup = {};
data.users.each(function(u) {
userLookup[u.id] = Discourse.User.create(u);
});
return data.posts.each(function(p) {
var f;
f = Discourse.FlaggedPost.create(p);
f.userLookup = userLookup;
return result.pushObject(f);
});
}
});
return result;
}
});
}).call(this);
@@ -1,81 +0,0 @@
window.Discourse.FlaggedPost = Discourse.Post.extend
flaggers: (->
r = []
@post_actions.each (a)=>
r.push(@userLookup[a.user_id])
r
).property()
messages: (->
r = []
@post_actions.each (a)=>
if a.message
r.push
user: @userLookup[a.user_id]
message: a.message
r
).property()
lastFlagged: (->
@post_actions[0].created_at
).property()
user: (->
@userLookup[@user_id]
).property()
topicHidden: (->
@get('topic_visible') == 'f'
).property('topic_hidden')
deletePost: ->
promise = new RSVP.Promise()
if @get('post_number') == "1"
$.ajax "/t/#{@topic_id}",
type: 'DELETE'
cache: false
success: ->
promise.resolve()
error: (e)->
promise.reject()
else
$.ajax "/posts/#{@id}",
type: 'DELETE'
cache: false
success: ->
promise.resolve()
error: (e)->
promise.reject()
clearFlags: ->
promise = new RSVP.Promise()
$.ajax "/admin/flags/clear/#{@id}",
type: 'POST'
cache: false
success: ->
promise.resolve()
error: (e)->
promise.reject()
promise
hiddenClass: (->
"hidden-post" if @get('hidden') == "t"
).property()
window.Discourse.FlaggedPost.reopenClass
findAll: (filter) ->
result = Em.A()
$.ajax
url: "/admin/flags/#{filter}.json"
success: (data) ->
userLookup = {}
data.users.each (u) -> userLookup[u.id] = Discourse.User.create(u)
data.posts.each (p) ->
f = Discourse.FlaggedPost.create(p)
f.userLookup = userLookup
result.pushObject(f)
result
@@ -0,0 +1,101 @@
(function() {
var SiteCustomizations;
window.Discourse.SiteCustomization = Discourse.Model.extend({
init: function() {
this._super();
return this.startTrackingChanges();
},
trackedProperties: ['enabled', 'name', 'stylesheet', 'header', 'override_default_style'],
description: (function() {
return "" + this.name + (this.enabled ? ' (*)' : '');
}).property('selected', 'name'),
changed: (function() {
var _this = this;
if (!this.originals) {
return false;
}
return this.trackedProperties.any(function(p) {
return _this.originals[p] !== _this.get(p);
});
}).property('override_default_style', 'enabled', 'name', 'stylesheet', 'header', 'originals'),
startTrackingChanges: function() {
var _this = this;
this.set('originals', {});
return this.trackedProperties.each(function(p) {
_this.originals[p] = _this.get(p);
return true;
});
},
previewUrl: (function() {
return "/?preview-style=" + (this.get('key'));
}).property('key'),
disableSave: (function() {
return !this.get('changed');
}).property('changed'),
save: function() {
var data;
this.startTrackingChanges();
data = {
name: this.name,
enabled: this.enabled,
stylesheet: this.stylesheet,
header: this.header,
override_default_style: this.override_default_style
};
return jQuery.ajax({
url: "/admin/site_customizations" + (this.id ? '/' + this.id : ''),
data: {
site_customization: data
},
type: this.id ? 'PUT' : 'POST'
});
},
"delete": function() {
if (!this.id) {
return;
}
return jQuery.ajax({
url: "/admin/site_customizations/" + this.id,
type: 'DELETE'
});
}
});
SiteCustomizations = Ember.ArrayProxy.extend({
selectedItemChanged: (function() {
var selected;
selected = this.get('selectedItem');
return this.get('content').each(function(i) {
return i.set('selected', selected === i);
});
}).observes('selectedItem')
});
Discourse.SiteCustomization.reopenClass({
findAll: function() {
var content,
_this = this;
content = SiteCustomizations.create({
content: [],
loading: true
});
jQuery.ajax({
url: "/admin/site_customizations",
dataType: "json",
success: function(data) {
if (data) {
data.site_customizations.each(function(c) {
var item;
item = Discourse.SiteCustomization.create(c);
return content.pushObject(item);
});
}
return content.set('loading', false);
}
});
return content;
}
});
}).call(this);
@@ -1,78 +0,0 @@
window.Discourse.SiteCustomization = Discourse.Model.extend
init: ->
@_super()
@startTrackingChanges()
trackedProperties: ['enabled','name', 'stylesheet', 'header', 'override_default_style']
description: (->
"#{@name}#{if @enabled then ' (*)' else ''}"
).property('selected', 'name')
changed: (->
return false unless @originals
@trackedProperties.any (p)=>
@originals[p] != @get(p)
).property('override_default_style','enabled','name', 'stylesheet', 'header', 'originals') # TODO figure out how to call with apply
startTrackingChanges: ->
@set('originals',{})
@trackedProperties.each (p)=>
@originals[p] = @get(p)
true
previewUrl: (->
"/?preview-style=#{@get('key')}"
).property('key')
disableSave:(->
!@get('changed')
).property('changed')
save: ->
@startTrackingChanges()
data =
name: @name
enabled: @enabled
stylesheet: @stylesheet
header: @header
override_default_style: @override_default_style
$.ajax
url: "/admin/site_customizations#{if @id then '/' + @id else ''}"
data:
site_customization: data
type: if @id then 'PUT' else 'POST'
delete: ->
return unless @id
$.ajax
url: "/admin/site_customizations/#{ @id }"
type: 'DELETE'
SiteCustomizations = Ember.ArrayProxy.extend
selectedItemChanged: (->
selected = @get('selectedItem')
@get('content').each (i)->
i.set('selected', selected == i)
).observes('selectedItem')
Discourse.SiteCustomization.reopenClass
findAll: ->
content = SiteCustomizations.create
content: []
loading: true
$.ajax
url: "/admin/site_customizations"
dataType: "json"
success: (data)=>
data?.site_customizations.each (c)->
item = Discourse.SiteCustomization.create(c)
content.pushObject(item)
content.set('loading',false)
content
@@ -0,0 +1,62 @@
(function() {
window.Discourse.SiteSetting = Discourse.Model.extend(Discourse.Presence, {
/* Whether a property is short.
*/
short: (function() {
if (this.blank('value')) {
return true;
}
return this.get('value').toString().length < 80;
}).property('value'),
/* Whether the site setting has changed
*/
dirty: (function() {
return this.get('originalValue') !== this.get('value');
}).property('originalValue', 'value'),
overridden: (function() {
var defaultVal, val;
val = this.get('value');
defaultVal = this.get('default');
if (val && defaultVal) {
return val.toString() !== defaultVal.toString();
}
return val !== defaultVal;
}).property('value'),
resetValue: function() {
return this.set('value', this.get('originalValue'));
},
save: function() {
/* Update the setting
*/
var _this = this;
return jQuery.ajax("/admin/site_settings/" + (this.get('setting')), {
data: {
value: this.get('value')
},
type: 'PUT',
success: function() {
return _this.set('originalValue', _this.get('value'));
}
});
}
});
window.Discourse.SiteSetting.reopenClass({
findAll: function() {
var result;
result = Em.A();
jQuery.get("/admin/site_settings", function(settings) {
return settings.each(function(s) {
s.originalValue = s.value;
return result.pushObject(Discourse.SiteSetting.create(s));
});
});
return result;
}
});
}).call(this);
@@ -1,42 +0,0 @@
window.Discourse.SiteSetting = Discourse.Model.extend Discourse.Presence,
# Whether a property is short.
short: (->
return true if @blank('value')
return @get('value').toString().length < 80
).property('value')
# Whether the site setting has changed
dirty: (->
@get('originalValue') != @get('value')
).property('originalValue', 'value')
overridden: (->
val = @get('value')
defaultVal = @get('default')
return val.toString() != defaultVal.toString() if (val and defaultVal)
return val != defaultVal
).property('value')
resetValue: ->
@set('value', @get('originalValue'))
save: ->
# Update the setting
$.ajax "/admin/site_settings/#{@get('setting')}",
data:
value: @get('value')
type: 'PUT'
success: => @set('originalValue', @get('value'))
window.Discourse.SiteSetting.reopenClass
findAll: ->
result = Em.A()
$.get "/admin/site_settings", (settings) ->
settings.each (s) ->
s.originalValue = s.value
result.pushObject(Discourse.SiteSetting.create(s))
result
@@ -0,0 +1,18 @@
(function() {
window.Discourse.VersionCheck = Discourse.Model.extend({});
Discourse.VersionCheck.reopenClass({
find: function() {
var _this = this;
return jQuery.ajax({
url: '/admin/version_check',
dataType: 'json',
success: function(json) {
return Discourse.VersionCheck.create(json);
}
});
}
});
}).call(this);
@@ -1,9 +0,0 @@
window.Discourse.VersionCheck = Discourse.Model.extend({})
Discourse.VersionCheck.reopenClass
find: ->
$.ajax
url: '/admin/version_check'
dataType: 'json'
success: (json) =>
Discourse.VersionCheck.create(json)