application/services/copyNote-service.js
import alt from './../alt';
import BaseService from './base-service';
let NoteStack = [];
let LastSelectedNote = false;
/**
* Copy notes live in the editor's metdata and so
* are only tracked via an internal, read-only, stack here.
*
* You shouldn't need to interact with this outside the NoteViewer component.
*/
class CopyNoteService extends BaseService {
updateNotes(notes) {
return notes;
}
updateSelection(id) {
return id;
}
setLastSelectedNote(id) {
LastSelectedNote = id;
return (dispatch) => {
setTimeout(() => {
this.updateSelection(id);
}, 1);
};
}
reset() {
return (dispatch) => {
setTimeout(() => {
NoteStack = [];
this.updateNotes(NoteStack);
}, 1);
}
}
add(uid, id, val, user, icon, email, version, created_at) {
NoteStack.push({uid: uid, id: id, contents: val, user: user, icon: icon, email: email, version: version, created_at: created_at});
return (dispatch) => {
setTimeout(() => {
this.updateNotes(NoteStack);
}, 1);
};
}
update(uid, id, val) {
for (let i in NoteStack) {
if (NoteStack[i].uid === uid) {
NoteStack[i].contents = val;
}
}
return (dispatch) => {
setTimeout(() => {
this.updateNotes(NoteStack);
}, 1);
};
}
remove(uid) {
let newNotes = [];
for (let i in NoteStack) {
if (NoteStack[i].uid === uid) {
continue;
}
newNotes.push(NoteStack[i]);
}
NoteStack = newNotes;
return (dispatch) => {
setTimeout(() => {
this.updateNotes(NoteStack);
}, 1);
}
}
}
const service = alt.createActions(CopyNoteService);
class CopyNotesStore {
constructor() {
this.state = {
'notes': [],
'selection': false
};
this.bindListeners({
'handleUpdateNotes': service.UPDATE_NOTES,
'handleUpdateSelection': service.UPDATE_SELECTION
});
}
handleUpdateNotes(notes) {
this.setState({'notes': notes});
}
handleUpdateSelection(selection) {
this.setState({'selection': selection});
if (window && window.jQuery) {
// force color change on selection
// can't use application state as the
// state updates cause the editor to crash
jQuery('.copyNoteMarker').css({'background-color': ''});
jQuery('.note-' + selection).css({'background-color': '#ffe92c'});
}
}
}
const store = alt.createStore(CopyNotesStore, 'CopyNotesStore');
export {service as CopyNoteService, store as CopyNoteStore};