main.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* This file is loaded when Organizr is loaded */
  2. // Load once Organizr loads
  3. $('body').arrive('#activeInfo', {onceOnly: true}, function() {
  4. plexLibrariesPluginLaunch();
  5. });
  6. // FUNCTIONS
  7. function plexLibrariesPluginLaunch(){
  8. organizrAPI2('GET','api/v2/plugins/plexlibraries/launch').success(function(data) {
  9. try {
  10. var menuList = `<li><a href="javascript:void(0)" onclick="togglePlexLibrariesPlugin();"><i class="fa fa-tv fa-fw"></i> <span lang="en">Plex Libraries</span></a></li>`;
  11. $('.append-menu').after(menuList);
  12. }catch(e) {
  13. organizrCatchError(e,data);
  14. }
  15. }).fail(function(xhr) {
  16. OrganizrApiError(xhr);
  17. });
  18. }
  19. function togglePlexLibrariesPlugin(){
  20. let div = `
  21. <div class="panel bg-org panel-info" id="plexLibraries-area">
  22. <div class="panel-heading">
  23. <span lang="en">Customise Plex Libraries</span>
  24. </div>
  25. <div class="panel-body">
  26. <div id="plexLibrariesTable">
  27. <div class="white-box m-b-0">
  28. <h2 class="text-center loadingPlexLibraries" lang="en"><i class="fa fa-spin fa-spinner"></i></h2>
  29. <div class="row">
  30. <div class="col-lg-12">
  31. <select class="form-control" name="plexUsers" id="plexUsers" style="display:none">
  32. <option value="">Choose a User</option>
  33. </select><br>
  34. </div>
  35. </div>
  36. <div class="table-responsive plexLibrariesTableList hidden" id="plexLibrariesTableList">
  37. <table class="table color-bordered-table purple-bordered-table text-left">
  38. <thead>
  39. <tr>
  40. <th width="20">Type</th>
  41. <th>Name</th>
  42. <th width="20">Action</th>
  43. </tr>
  44. </thead>
  45. <tbody id="plexLibraries"></tbody>
  46. </table>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. `;
  53. swal({
  54. content: createElementFromHTML(div),
  55. button: false,
  56. className: 'orgAlertTransparent',
  57. });
  58. plexLibrariesPluginLoadShares();
  59. }
  60. function plexLibrariesPluginLoadShares(){
  61. organizrAPI2('GET','api/v2/plugins/plexlibraries/shares').success(function(data) {
  62. $('.loadingPlexLibraries').remove();
  63. try {
  64. if (data.response.data.plexAdmin == false) {
  65. $.each(data.response.data.libraryData, function(_, sharedServer) {
  66. $.each(sharedServer.libraries, function(_, obj) {
  67. var userId = sharedServer.id;
  68. plexLibrariesPluginLoadSharesItem(obj,"",userId);
  69. if($('.plexLibrariesTableList').hasClass('hidden')){
  70. $('.plexLibrariesTableList').removeClass('hidden');
  71. }
  72. });
  73. });
  74. } else {
  75. // Plex Admin response contains all users shares, mark all toggles as disabled whilst this is a work in progress.
  76. $.each(data.response.data.libraryData, function(_, sharedServer) {
  77. const thtml = $("#plexUsers ");
  78. var dropdown = document.getElementById('plexUsers');
  79. dropdown.style.display = "block";
  80. var username = sharedServer.username;
  81. var userId = sharedServer.id;
  82. thtml.append('<option value="'+username+'">'+username+'</option>');
  83. $.each(sharedServer.libraries, function(_, obj) {
  84. plexLibrariesPluginLoadSharesItem(obj,username,userId);
  85. });
  86. });
  87. }
  88. const thtml = $("#plexLibraries ");
  89. thtml.append('<script>plexLibrariesPluginOnToggle();</script>');
  90. thtml.append('<script>plexLibrariesPluginOnSelect();</script>');
  91. }catch(e) {
  92. organizrCatchError(e,data);
  93. }
  94. }).fail(function(xhr) {
  95. $('.loadingPlexLibraries').remove();
  96. OrganizrApiError(xhr);
  97. });
  98. }
  99. function plexLibrariesPluginLoadSharesItem(obj,username,userId){
  100. const thtml = $("#plexLibraries ");
  101. var mediaType = obj.type
  102. var mediaShared = obj.shared
  103. var mediaIcon = "0"
  104. var checked = "";
  105. switch(mediaType) {
  106. case 'movie':
  107. var mediaIcon = "video-clapper"
  108. var mediaIconColour = "purple"
  109. break;
  110. case 'show':
  111. var mediaIcon = "video-camera"
  112. var mediaIconColour = "warning"
  113. break;
  114. case 'artist':
  115. var mediaIcon = "music-alt"
  116. var mediaIconColour = "info"
  117. break;
  118. case 'photo':
  119. var mediaIcon = "camera"
  120. var mediaIconColour = "danger"
  121. break;
  122. }
  123. if (mediaShared == 1) {
  124. var checked = "checked";
  125. }
  126. if (username === "") {
  127. var username = "none"
  128. }
  129. let libItem = `
  130. <tr class="plexUser ${username}">
  131. <td><p class="text-${mediaIconColour}"><i class="ti-${mediaIcon} fa-2x" style="vertical-align: text-top;"></i></p></td>
  132. <td>${obj.title}</td>
  133. <td><input type="checkbox" class="js-switch plexLibraries" data-size="small" data-color="#99d683" data-secondary-color="#f96262" data-user-id="${userId}" value="${obj.id}" ${checked}></td>
  134. </tr>
  135. `;
  136. thtml.append(libItem);
  137. }
  138. function plexLibrariesPluginOnToggle() {
  139. $('.plexLibraries').change(function () {
  140. let userId = $(this).attr('data-user-id');
  141. if (this.checked) {
  142. plexLibrariesPluginUpdateShare(userId,"share", this.value);
  143. } else {
  144. plexLibrariesPluginUpdateShare(userId,"unshare", this.value);
  145. }
  146. });
  147. }
  148. function plexLibrariesPluginOnSelect() {
  149. $('#plexUsers').change(function () {
  150. Array.from(document.getElementsByClassName('plexUser')).forEach(
  151. function(element, index, array) {
  152. element.style.display = "none";
  153. }
  154. );
  155. Array.from(document.getElementsByClassName(this.value)).forEach(
  156. function(element, index, array) {
  157. element.style.display = "table-row";
  158. }
  159. );
  160. if($('.plexLibrariesTableList').hasClass('hidden')){
  161. $('.plexLibrariesTableList').removeClass('hidden');
  162. }
  163. });
  164. }
  165. function plexLibrariesPluginUpdateShare(userId, action, shareId) {
  166. $('#plexLibrariesTable').block({
  167. message: '<h4><img src="plugins/images/busy.gif" width="50px" /> Just a moment...</h4>',
  168. css: {
  169. border: '1px solid #000',
  170. color: '#fff',
  171. background: '#1b1b1b'
  172. }
  173. });
  174. organizrAPI2('POST','api/v2/plugins/plexlibraries/shares/' + userId + '/' + action + '/' + shareId, {}).success(function(data) {
  175. try {
  176. let response = data.response;
  177. $('#plexLibrariesTable').unblock();
  178. message('Plex Share',response.message,activeInfo.settings.notifications.position,"#FFF","success","5000");
  179. }catch(e) {
  180. organizrCatchError(e,data);
  181. }
  182. }).fail(function(xhr) {
  183. message('Plex Share',xhr.responseJSON.response.message,activeInfo.settings.notifications.position,"#FFF","error","5000");
  184. $('#plexLibrariesTable').unblock();
  185. OrganizrApiError(xhr);
  186. });
  187. }
  188. // EVENTS and LISTENERS