Home Reference Source

application/services/copyNote-service.js

  1. import alt from './../alt';
  2. import BaseService from './base-service';
  3.  
  4. let NoteStack = [];
  5. let LastSelectedNote = false;
  6.  
  7. /**
  8. * Copy notes live in the editor's metdata and so
  9. * are only tracked via an internal, read-only, stack here.
  10. *
  11. * You shouldn't need to interact with this outside the NoteViewer component.
  12. */
  13. class CopyNoteService extends BaseService {
  14.  
  15. updateNotes(notes) {
  16. return notes;
  17. }
  18.  
  19. updateSelection(id) {
  20. return id;
  21. }
  22.  
  23. setLastSelectedNote(id) {
  24. LastSelectedNote = id;
  25.  
  26. return (dispatch) => {
  27. setTimeout(() => {
  28. this.updateSelection(id);
  29. }, 1);
  30. };
  31. }
  32.  
  33. reset() {
  34. return (dispatch) => {
  35. setTimeout(() => {
  36. NoteStack = [];
  37. this.updateNotes(NoteStack);
  38. }, 1);
  39. }
  40. }
  41.  
  42. add(uid, id, val, user, icon, email, version, created_at) {
  43. NoteStack.push({uid: uid, id: id, contents: val, user: user, icon: icon, email: email, version: version, created_at: created_at});
  44.  
  45. return (dispatch) => {
  46. setTimeout(() => {
  47. this.updateNotes(NoteStack);
  48. }, 1);
  49. };
  50. }
  51.  
  52. update(uid, id, val) {
  53. for (let i in NoteStack) {
  54. if (NoteStack[i].uid === uid) {
  55. NoteStack[i].contents = val;
  56. }
  57. }
  58.  
  59. return (dispatch) => {
  60. setTimeout(() => {
  61. this.updateNotes(NoteStack);
  62. }, 1);
  63. };
  64. }
  65.  
  66. remove(uid) {
  67. let newNotes = [];
  68.  
  69. for (let i in NoteStack) {
  70. if (NoteStack[i].uid === uid) {
  71. continue;
  72. }
  73.  
  74. newNotes.push(NoteStack[i]);
  75. }
  76.  
  77. NoteStack = newNotes;
  78.  
  79. return (dispatch) => {
  80. setTimeout(() => {
  81. this.updateNotes(NoteStack);
  82. }, 1);
  83. }
  84. }
  85. }
  86.  
  87. const service = alt.createActions(CopyNoteService);
  88.  
  89. class CopyNotesStore {
  90. constructor() {
  91. this.state = {
  92. 'notes': [],
  93. 'selection': false
  94. };
  95.  
  96. this.bindListeners({
  97. 'handleUpdateNotes': service.UPDATE_NOTES,
  98. 'handleUpdateSelection': service.UPDATE_SELECTION
  99. });
  100. }
  101.  
  102. handleUpdateNotes(notes) {
  103. this.setState({'notes': notes});
  104. }
  105.  
  106. handleUpdateSelection(selection) {
  107. this.setState({'selection': selection});
  108. if (window && window.jQuery) {
  109. // force color change on selection
  110. // can't use application state as the
  111. // state updates cause the editor to crash
  112. jQuery('.copyNoteMarker').css({'background-color': ''});
  113. jQuery('.note-' + selection).css({'background-color': '#ffe92c'});
  114. }
  115. }
  116. }
  117.  
  118. const store = alt.createStore(CopyNotesStore, 'CopyNotesStore');
  119.  
  120. export {service as CopyNoteService, store as CopyNoteStore};