index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2017 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import ReactDOM from 'react-dom'
  20. import MoveItemTray from './MoveItemTray'
  21. const ROOT_ID = 'move_item_tray'
  22. export function renderTray (props, rootContainer = document.body) {
  23. let root = document.getElementById(ROOT_ID)
  24. if (!root) {
  25. root = document.createElement('div')
  26. root.setAttribute('id', ROOT_ID)
  27. rootContainer.appendChild(root)
  28. }
  29. ReactDOM.render(<MoveItemTray {...props} ref={(tray) => tray && tray.open()} />, root)
  30. }
  31. export const backbone = {
  32. collectionToItems (collection) {
  33. return collection.models.map(item => ({
  34. id: item.attributes.id,
  35. title: item.attributes.name || item.attributes.title,
  36. }))
  37. },
  38. collectionToGroups (collection, getItems, filter = () => true) {
  39. return collection.models.filter(filter).map((item) => ({
  40. id: item.attributes.id,
  41. title: item.attributes.name || item.attributes.title,
  42. items: this.collectionToItems(getItems(item)),
  43. }))
  44. },
  45. reorderInCollection (order, model, collection = model.collection) {
  46. order.forEach((id, index) => {
  47. const item = collection.get(id)
  48. if (item) item.set('position', index + 1)
  49. })
  50. // call reset to trigger a re-render
  51. collection.sort()
  52. collection.reset(collection.models)
  53. },
  54. reorderAcrossCollections (order, collId, model, keys) {
  55. let newColl = model.collection.view.parentCollection.get(collId).get(keys.model)
  56. // if item moved across collections
  57. if (newColl && newColl !== model.collection) {
  58. model.collection.remove(model)
  59. newColl.add(model)
  60. if (keys.parent) {
  61. model.set(keys.parent, collId)
  62. }
  63. } else {
  64. newColl = model.collection
  65. }
  66. this.reorderInCollection(order, model, newColl)
  67. }
  68. }
  69. export function reorderElements (order, container, idToItemSelector = id => `#${id}`) {
  70. const itemMap = order.reduce((items, id) => {
  71. const item = container.querySelector(idToItemSelector(id))
  72. items[id] = item
  73. container.removeChild(item)
  74. return items
  75. }, {})
  76. order.forEach(id => container.appendChild(itemMap[id]))
  77. }