API Docs for: 0.25.0
Show:

File: addon/mixins/node-actions.js

  1. import Ember from 'ember';
  2.  
  3. /**
  4. * @module ember-osf
  5. * @submodule mixins
  6. */
  7.  
  8. /**
  9. * Controller mixin that implements common actions performed on nodes.
  10. * @class NodeActionsMixin
  11. * @extends Ember.Mixin
  12. */
  13. export default Ember.Mixin.create({
  14. /**
  15. * The node to perform these actions on. If not specified, defaults to the model hook.
  16. * @property node
  17. * @type DS.Model
  18. */
  19. node: null,
  20. model: null,
  21. _node: Ember.computed.or('node', 'model'),
  22. /**
  23. * Helper method that affiliates an institution with a node.
  24. *
  25. * @method _affiliateNode
  26. * @private
  27. * @param {DS.Model} node Node record
  28. * @param {Object} institution Institution record
  29. * @return {Promise} Returns a promise that resolves to the updated node with the newly created institution relationship
  30. */
  31. _affiliateNode(node, institution) {
  32. node.get('affiliatedInstitutions').pushObject(institution);
  33. return node.save();
  34. },
  35. actions: {
  36. /**
  37. * Update a node
  38. *
  39. * @method updateNode
  40. * @param {String} title New title of the node
  41. * @param {String} description New Description of the node
  42. * @param {String} category New node category
  43. * @param {Boolean} isPublic Should this node be publicly-visible?
  44. * @return {Promise} Returns a promise that resolves to the updated node
  45. */
  46. updateNode(title, description, category, isPublic) {
  47. var node = this.get('_node');
  48. if (title) {
  49. node.set('title', title);
  50. }
  51. if (category) {
  52. node.set('category', category);
  53. }
  54. if (description) {
  55. node.set('description', description);
  56. }
  57. if (isPublic !== null) {
  58. node.set('public', isPublic);
  59. }
  60. return node.save();
  61. },
  62. /**
  63. * Delete a node
  64. *
  65. * @method deleteNode
  66. * @return {Promise} Returns a promise that resolves after the deletion of the node.
  67. */
  68. deleteNode() {
  69. var node = this.get('_node');
  70. return node.destroyRecord();
  71. },
  72. /**
  73. * Affiliate a node with an institution
  74. *
  75. * @method affiliateNode
  76. * @param {String} institutionId ID of the institutution to be affiliated
  77. * @return {Promise} Returns a promise that resolves to the updated node
  78. * with the newly affiliated institution relationship
  79. */
  80. affiliateNode(institutionId) {
  81. var node = this.get('_node');
  82. return this.store.findRecord('institution', institutionId)
  83. .then(institution => this._affiliateNode(node, institution));
  84. },
  85. /**
  86. * Unaffiliate a node with an institution
  87. *
  88. * @method unaffiliateNode
  89. * @param {Object} institution Institution relationship to be removed from node
  90. * @return {Promise} Returns a promise that resolves to the updated node
  91. * with the affiliated institution relationship removed.
  92. */
  93. unaffiliateNode(institution) {
  94. var node = this.get('_node');
  95. node.get('affiliatedInstitutions').removeObject(institution);
  96. return node.save();
  97. },
  98. /**
  99. * Add a contributor to a node
  100. *
  101. * @method addContributor
  102. * @param {String} userId ID of user that will be a contributor on the node
  103. * @param {String} permission User permission level. One of "read", "write", or "admin". Default: "write".
  104. * @param {Boolean} isBibliographic Whether user will be included in citations for the node. "default: true"
  105. * @param {Boolean} sendEmail Whether user will receive an email when added. "default: true"
  106. * @return {Promise} Returns a promise that resolves to the newly created contributor object.
  107. */
  108. addContributor() {
  109. return this.get('_node').addContributor(...arguments);
  110. },
  111. /**
  112. * Bulk add contributors to a node
  113. *
  114. * @method addContributors
  115. * @param {Array} contributors Array of objects containing contributor permission, bibliographic, and userId keys
  116. * @param {Boolean} sendEmail Whether user will receive an email when added. "default: true"
  117. * @return {Promise} Returns a promise that resolves to an array of added contributors
  118. */
  119. addContributors() {
  120. return this.get('_node').addContributors(...arguments);
  121. },
  122. /**
  123. * Remove a contributor from a node
  124. *
  125. * @method removeContributor
  126. * @param {Object} contributor Contributor relationship that will be removed from node
  127. * @return {Promise} Returns a promise that will resolve upon contributor deletion.
  128. * User itself will not be removed.
  129. */
  130. removeContributor(contributor) {
  131. var node = this.get('_node');
  132. return node.removeContributor(contributor);
  133. },
  134. /**
  135. * Update contributors of a node. Makes a bulk request to the APIv2.
  136. *
  137. * @method updateContributors
  138. * @param {Contributor[]} contributors Contributor relationships on the node.
  139. * @param {Object} permissionsChanges Dictionary mapping contributor ids to desired permissions.
  140. * @param {Object} bibliographicChanges Dictionary mapping contributor ids to desired bibliographic statuses
  141. * @return {Promise} Returns a promise that resolves to the updated node
  142. * with edited contributor relationships.
  143. */
  144. updateContributors() {
  145. return this.get('_node').updateContributors(...arguments);
  146. },
  147.  
  148. /**
  149. * Update contributors of a node. Makes a bulk request to the APIv2.
  150. *
  151. * @method updateContributor
  152. * @param {Contributor} contributor relationship on the node.
  153. * @param {string} permissions desired permissions.
  154. * @param {boolean} bibliographic desired bibliographic statuses
  155. * @return {Promise} Returns a promise that resolves to the updated node
  156. * with edited contributor relationships.
  157. */
  158. updateContributor() {
  159. return this.get('_node').updateContributor(...arguments);
  160. },
  161. /**
  162. * Reorder contributors on a node, and manually updates store.
  163. *
  164. * @method reorderContributors
  165. * @param {Object} contributor Contributor record to be modified
  166. * @param {Integer} newIndex Contributor's new position in the list
  167. * @param {Array} contributors New contributor list in correct order
  168. * @return {Promise} Returns a promise that resolves to the updated contributor.
  169. */
  170. reorderContributors(contributor, newIndex, contributors) {
  171. contributor.set('index', newIndex);
  172. return contributor.save().then(() => {
  173. contributors.forEach((contrib, index) => {
  174. if (contrib.id !== contributor.id) {
  175. var payload = contrib.serialize();
  176. payload.data.attributes = {
  177. permission: contrib.get('permission'),
  178. bibliographic: contrib.get('bibliographic'),
  179. index: index
  180. };
  181. payload.data.id = contrib.get('id');
  182. this.store.pushPayload(payload);
  183. }
  184. });
  185. });
  186. },
  187. /**
  188. * Add a child (component) to a node.
  189. *
  190. * @method addChild
  191. * @param {String} title Title for the child
  192. * @param {String} description Description for the child
  193. * @param {String} category Category for the child
  194. * @return {Promise} Returns a promise that resolves to the newly created child node.
  195. */
  196. addChild(title, description, category) {
  197. return this.get('_node').addChild(title, description, category);
  198. },
  199. /**
  200. * Adds a relationship to another node, called a linkedNode.
  201. *
  202. * @method addNodeLink
  203. * @param {String} targetNodeId ID of the node for which you wish to create a link
  204. * @return {Promise} Returns a promise that resolves to the newly updated node
  205. */
  206. addNodeLink(targetNodeId) {
  207. var node = this.get('_node');
  208. return this.store.findRecord('node', targetNodeId).then(linkedNode => {
  209. node.get('linkedNodes').pushObject(linkedNode);
  210. return node.save();
  211. });
  212.  
  213. },
  214. /**
  215. * Removes the linkedNode relationship to another node. Does not remove the linked node itself.
  216. *
  217. * @method removeNodeLink
  218. * @param {Object} linkedNode linkedNode relationship to be destroyed.
  219. * @return {Promise} Returns a promise that resolves to the newly updated node
  220. */
  221. removeNodeLink(linkedNode) {
  222. var node = this.get('_node');
  223. node.get('linkedNodes').removeObject(linkedNode);
  224. return node.save();
  225. }
  226.  
  227. }
  228. });
  229.