Working fairly well now
This commit is contained in:
parent
840be343e9
commit
69661ae6b1
|
@ -0,0 +1,7 @@
|
||||||
|
{{#if currentUser.staff}}
|
||||||
|
{{#if staffNotesCount}}
|
||||||
|
<li><a {{action "showStaffNotes"}} href class="btn">{{fa-icon "pencil"}}{{i18n 'staff_notes.show' count=staffNotesCount}}</a></li>
|
||||||
|
{{else}}
|
||||||
|
<li><a {{action "showStaffNotes"}} href class="btn">{{fa-icon "pencil"}}{{i18n 'staff_notes.title'}}</a></li>
|
||||||
|
{{/if}}
|
||||||
|
{{/if}}
|
|
@ -4,10 +4,11 @@ import { popupAjaxError } from 'discourse/lib/ajax-error';
|
||||||
const StaffNotesController = Ember.Controller.extend({
|
const StaffNotesController = Ember.Controller.extend({
|
||||||
newNote: null,
|
newNote: null,
|
||||||
saving: false,
|
saving: false,
|
||||||
|
user: null,
|
||||||
|
|
||||||
@on('init')
|
@on('init')
|
||||||
reset() {
|
reset() {
|
||||||
this.setProperties({ newNote: null, saving: false });
|
this.setProperties({ newNote: null, saving: false, callback: null });
|
||||||
},
|
},
|
||||||
|
|
||||||
@computed('newNote', 'saving')
|
@computed('newNote', 'saving')
|
||||||
|
@ -15,17 +16,26 @@ const StaffNotesController = Ember.Controller.extend({
|
||||||
return saving || !newNote || (newNote.length === 0);
|
return saving || !newNote || (newNote.length === 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
_refreshCount() {
|
||||||
|
const callback = this.get('callback');
|
||||||
|
if (callback) {
|
||||||
|
callback(this.get('model.length'));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
attachNote() {
|
attachNote() {
|
||||||
const note = this.store.createRecord('staff-note');
|
const note = this.store.createRecord('staff-note');
|
||||||
const userId = parseInt(this.get('userId'));
|
const userId = parseInt(this.get('userId'));
|
||||||
|
|
||||||
|
const noteCount = StaffNotesController.noteCount;
|
||||||
this.set('saving', true);
|
this.set('saving', true);
|
||||||
note.save({ raw: this.get('newNote'), user_id: userId }).then(() => {
|
note.save({ raw: this.get('newNote'), user_id: userId }).then(() => {
|
||||||
this.set('newNote', '');
|
this.set('newNote', '');
|
||||||
this.get('model').pushObject(note);
|
this.get('model').pushObject(note);
|
||||||
StaffNotesController.noteCount[userId] = (StaffNotesController.noteCount[userId] || 0) + 1;
|
noteCount[userId] = this.get('model.length');
|
||||||
this.appEvents.trigger('post-stream:refresh', { force: true });
|
|
||||||
|
this._refreshCount();
|
||||||
}).catch(popupAjaxError).finally(() => this.set('saving', false));
|
}).catch(popupAjaxError).finally(() => this.set('saving', false));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -34,7 +44,7 @@ const StaffNotesController = Ember.Controller.extend({
|
||||||
const notes = this.get('model');
|
const notes = this.get('model');
|
||||||
notes.removeObject(note);
|
notes.removeObject(note);
|
||||||
StaffNotesController.noteCount[parseInt(note.get('user_id'))] = notes.get('length');
|
StaffNotesController.noteCount[parseInt(note.get('user_id'))] = notes.get('length');
|
||||||
this.appEvents.trigger('post-stream:refresh', { force: true });
|
this._refreshCount();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,16 +7,42 @@ export default {
|
||||||
const siteSettings = container.lookup('site-settings:main');
|
const siteSettings = container.lookup('site-settings:main');
|
||||||
if (!siteSettings.staff_notes_enabled) { return; }
|
if (!siteSettings.staff_notes_enabled) { return; }
|
||||||
|
|
||||||
|
const store = container.lookup('store:main');
|
||||||
|
|
||||||
withPluginApi('0.2', api => {
|
withPluginApi('0.2', api => {
|
||||||
function showStaffNotes() {
|
function showStaffNotes(userId, callback) {
|
||||||
const userId = this.attrs.user_id;
|
return store.find('staff-note', { user_id: userId }).then(model => {
|
||||||
return this.store.find('staff-note', { user_id: userId }).then(model => {
|
|
||||||
const controller = showModal('staff-notes', { model, title: 'staff_notes.title' });
|
const controller = showModal('staff-notes', { model, title: 'staff_notes.title' });
|
||||||
controller.reset();
|
controller.reset();
|
||||||
controller.set('userId', userId);
|
controller.set('userId', userId);
|
||||||
|
controller.set('callback', callback);
|
||||||
|
return controller;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function widgetShowStaffNotes() {
|
||||||
|
showStaffNotes(this.attrs.user_id, count => {
|
||||||
|
this.scheduleRerender();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const UserController = container.lookupFactory('controller:user');
|
||||||
|
UserController.reopen({
|
||||||
|
staffNotesCount: null,
|
||||||
|
|
||||||
|
_modelChanged: function() {
|
||||||
|
this.set('staffNotesCount', this.get('model.custom_fields.staff_notes_count') || 0);
|
||||||
|
}.observes('model').on('init'),
|
||||||
|
|
||||||
|
actions: {
|
||||||
|
showStaffNotes() {
|
||||||
|
const user = this.get('model');
|
||||||
|
showStaffNotes(user.get('id'), count => this.set('staffNotesCount', count));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const StaffNotesController = container.lookupFactory('controller:staff-notes');
|
const StaffNotesController = container.lookupFactory('controller:staff-notes');
|
||||||
const noteCount = StaffNotesController.noteCount;
|
const noteCount = StaffNotesController.noteCount;
|
||||||
|
|
||||||
|
@ -24,12 +50,12 @@ export default {
|
||||||
const cfs = dec.attrs.userCustomFields || {};
|
const cfs = dec.attrs.userCustomFields || {};
|
||||||
|
|
||||||
// If we know the count, use it
|
// If we know the count, use it
|
||||||
const c = noteCount[dec.attrs.user_id];
|
let c = noteCount[dec.attrs.user_id];
|
||||||
if (c !== undefined) {
|
if (c === undefined && cfs.staff_notes_count) {
|
||||||
if (c > 0) {
|
c = cfs.staff_notes_count;
|
||||||
return dec.attach('staff-notes-icon');
|
|
||||||
}
|
}
|
||||||
} else if (cfs.has_staff_notes) {
|
|
||||||
|
if (c > 0) {
|
||||||
return dec.attach('staff-notes-icon');
|
return dec.attach('staff-notes-icon');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -42,11 +68,11 @@ export default {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
api.attachWidgetAction('post-admin-menu', 'showStaffNotes', showStaffNotes);
|
api.attachWidgetAction('post-admin-menu', 'showStaffNotes', widgetShowStaffNotes);
|
||||||
|
|
||||||
api.createWidget('staff-notes-icon', {
|
api.createWidget('staff-notes-icon', {
|
||||||
tagName: 'span.staff-notes-icon',
|
tagName: 'span.staff-notes-icon',
|
||||||
click: showStaffNotes,
|
click: widgetShowStaffNotes,
|
||||||
|
|
||||||
html() {
|
html() {
|
||||||
return this.attach('emoji', { name: 'pencil' });
|
return this.attach('emoji', { name: 'pencil' });
|
||||||
|
|
|
@ -4,3 +4,4 @@ en:
|
||||||
title: "Staff Notes"
|
title: "Staff Notes"
|
||||||
attach: "Attach Staff Note"
|
attach: "Attach Staff Note"
|
||||||
remove: "Remove Note"
|
remove: "Remove Note"
|
||||||
|
show: "Staff Notes ({{count}})"
|
||||||
|
|
10
plugin.rb
10
plugin.rb
|
@ -7,7 +7,7 @@ enabled_site_setting :staff_notes_enabled
|
||||||
|
|
||||||
register_asset 'stylesheets/staff_notes.scss'
|
register_asset 'stylesheets/staff_notes.scss'
|
||||||
|
|
||||||
STAFF_NOTES_FIELD = "has_staff_notes"
|
STAFF_NOTE_COUNT_FIELD = "staff_notes_count"
|
||||||
|
|
||||||
after_initialize do
|
after_initialize do
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ after_initialize do
|
||||||
notes << record
|
notes << record
|
||||||
::PluginStore.set("staff_notes", key_for(user.id), notes)
|
::PluginStore.set("staff_notes", key_for(user.id), notes)
|
||||||
|
|
||||||
user.custom_fields[STAFF_NOTES_FIELD] = true
|
user.custom_fields[STAFF_NOTE_COUNT_FIELD] = notes.size
|
||||||
user.save_custom_fields
|
user.save_custom_fields
|
||||||
|
|
||||||
record
|
record
|
||||||
|
@ -47,9 +47,9 @@ after_initialize do
|
||||||
::PluginStore.set("staff_notes", key_for(user.id), notes)
|
::PluginStore.set("staff_notes", key_for(user.id), notes)
|
||||||
else
|
else
|
||||||
::PluginStore.remove("staff_notes", key_for(user.id))
|
::PluginStore.remove("staff_notes", key_for(user.id))
|
||||||
user.custom_fields.delete(STAFF_NOTES_FIELD)
|
|
||||||
user.save_custom_fields
|
|
||||||
end
|
end
|
||||||
|
user.custom_fields[STAFF_NOTE_COUNT_FIELD] = notes.size
|
||||||
|
user.save_custom_fields
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -116,7 +116,7 @@ after_initialize do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
whitelist_staff_user_custom_field(STAFF_NOTES_FIELD)
|
whitelist_staff_user_custom_field(STAFF_NOTE_COUNT_FIELD)
|
||||||
|
|
||||||
DiscourseStaffNotes::Engine.routes.draw do
|
DiscourseStaffNotes::Engine.routes.draw do
|
||||||
get '/' => 'staff_notes#index'
|
get '/' => 'staff_notes#index'
|
||||||
|
|
Loading…
Reference in New Issue