All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
6.1 KiB

  1. class OwnershipViewer {
  2. // Creates and applies the OwnershipViewer div to each document in a directory - called when each directory renders.
  3. static directoryRendered(obj, html, data) {
  4. // If user isn't a GM, don't continue
  5. if (!game.user.isGM) return;
  6. // Get the current directory's right-click context options, then tore the ownership config option
  7. const contextOptions = obj._getEntryContextOptions();
  8. const ownershipOption = contextOptions.find(e => e.name === 'OWNERSHIP.Configure')
  9. // Gather all documents in the current directory
  10. let collection = obj.constructor.collection;
  11. // Interate through each directory list item.
  12. for (let li of html.find("li.directory-item.document")) {
  13. // Match it to the corresponding document
  14. li = $(li)
  15. let document = collection.get(li.attr("data-document-id"))
  16. let users = []
  17. // Iterate through each ownership definition on the document
  18. for (let id in document.ownership) {
  19. let ownership = document.ownership[id]
  20. // If the ownership definition isn't 'None'...
  21. if (ownership >= CONST.DOCUMENT_OWNERSHIP_LEVELS.LIMITED) {
  22. let bg_color = "transparent"
  23. // And if the ownership definition isn't 'All Players' (default) or a GM, set 'bg_color' to the user's color
  24. if (id != "default") {
  25. const user = game.users.get(id)
  26. if (user) {
  27. if (user.isGM) continue;
  28. bg_color = user.color;
  29. } else {
  30. continue;
  31. }
  32. }
  33. // Create the div for this ownership definition, with the appropriate class based on the ownership level
  34. let user_div = $('<div></div>')
  35. user_div.attr("data-user-id", id)
  36. if (ownership === CONST.DOCUMENT_OWNERSHIP_LEVELS.LIMITED) {
  37. user_div.addClass("ownership-viewer-limited")
  38. } else if (ownership === CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER) {
  39. user_div.addClass("ownership-viewer-observer")
  40. } else if (ownership === CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER) {
  41. user_div.addClass("ownership-viewer-owner")
  42. }
  43. if (id == "default") {
  44. user_div.addClass("ownership-viewer-all")
  45. } else {
  46. user_div.addClass("ownership-viewer-user")
  47. }
  48. user_div.css({'background-color': bg_color})
  49. // Store the resulting div and keep iterating through the other ownership definitions on the document
  50. users.push(user_div)
  51. }
  52. }
  53. let div = $('<div class="ownership-viewer"></div>')
  54. // Append the collection of divs to the document's list item, or add the 'none set' icon if empty
  55. if (ownershipOption) {
  56. if (users.length === 0)
  57. users.push($('<div><i class="fas fa-share-alt" style="color: white;"/></div>'))
  58. let a = $(`<a href="#"></a>`)
  59. div.append(a)
  60. a.append(...users)
  61. } else {
  62. div.append(...users)
  63. }
  64. li.append(div)
  65. }
  66. // Ensure any clicks on the OwnershipViewer div open the ownership config for that document
  67. if (ownershipOption)
  68. html.find(".ownership-viewer").click(event => {
  69. event.preventDefault();
  70. event.stopPropagation();
  71. let li = $(event.currentTarget).closest("li")
  72. if (li)
  73. ownershipOption.callback(li)
  74. })
  75. }
  76. // Update the user color in OwnershipViewer divs if the user is edited
  77. static userUpdated(user) {
  78. for (let user_div of $(".ownership-viewer-user")) {
  79. let id = $(user_div).attr("data-user-id")
  80. if (id == user.id) {
  81. $(user_div).css('background-color', user.color)
  82. }
  83. }
  84. }
  85. // Makes the color assigned to each player clearer in the player list if they are inactive.
  86. static playerListRendered(){
  87. console.log("Ownership Viewer | Player List");
  88. let pvUsers = game.users.contents;
  89. let pvIdColor = [];
  90. for (let x of pvUsers){
  91. let pvMyObject = {};
  92. pvMyObject.id = x.id;
  93. pvMyObject.color = x.color;
  94. pvIdColor.push(pvMyObject);
  95. }
  96. let pvToReplace = "border: 1px solid #000000";
  97. let pvReplacee = "border: 1px solid";
  98. for (let i = 0; i < document.getElementById("player-list").children.length ; i++ ){
  99. let pvString = document.getElementById("player-list").children[i].innerHTML;
  100. if (pvString.toString().search("inactive") > 0){
  101. pvString = pvString.replace(pvToReplace, (pvReplacee + pvIdColor[i].color));
  102. document.getElementById("player-list").children[i].innerHTML = pvString;
  103. }
  104. }
  105. }
  106. }
  107. Hooks.on('renderJournalDirectory', OwnershipViewer.directoryRendered)
  108. Hooks.on('renderSceneDirectory', OwnershipViewer.directoryRendered)
  109. Hooks.on('renderActorDirectory', OwnershipViewer.directoryRendered)
  110. Hooks.on('renderItemDirectory', OwnershipViewer.directoryRendered)
  111. Hooks.on('renderMacroDirectory', OwnershipViewer.directoryRendered)
  112. Hooks.on('renderRollTableDirectory', OwnershipViewer.directoryRendered)
  113. Hooks.on('renderCardsDirectory', OwnershipViewer.directoryRendered)
  114. Hooks.on('updateUser', OwnershipViewer.userUpdated)
  115. Hooks.on('renderPlayerList', OwnershipViewer.playerListRendered)