Wanikani Open Framework - Progress module

Progress module for Wanikani Open Framework

当前为 2018-03-26 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/38577/261381/Wanikani%20Open%20Framework%20-%20Progress%20module.js

  1. // ==UserScript==
  2. // @name Wanikani Open Framework - Progress module
  3. // @namespace rfindley
  4. // @description Progress module for Wanikani Open Framework
  5. // @version 1.0.4
  6. // @copyright 2018+, Robin Findley
  7. // @license MIT; http://opensource.org/licenses/MIT
  8. // ==/UserScript==
  9.  
  10. (function(global) {
  11.  
  12. //########################################################################
  13. //------------------------------
  14. // Published interface
  15. //------------------------------
  16. global.wkof.Progress = {
  17. update: update_progress
  18. }
  19. //########################################################################
  20.  
  21. var popup_delay = 1000; // Delay before popup will open (in milliseconds).
  22. var popup_delay_started = false, popup_delay_expired = false, popup_timer;
  23. var externals_requested = false, externals_loaded = false;
  24. var progress_bars = {};
  25. var user_closed = false;
  26. var dialog_visible = false, dialog;
  27.  
  28. //------------------------------
  29. // Update the progress bar.
  30. //------------------------------
  31. function update_progress(data) {
  32. if (data) update_data(data);
  33.  
  34. if (!dialog_visible && !have_pending()) return shutdown();
  35.  
  36. // We have something pending, but don't show dialog until popup_delay has passed.
  37. if (!popup_delay_started) return start_popup_delay();
  38.  
  39. // Popup delay has passed. Show progress.
  40. if (!popup_delay_expired) return;
  41. update_dialog();
  42. }
  43.  
  44. //------------------------------
  45. // Update our stored progress bar status
  46. //------------------------------
  47. function update_data(data) {
  48. var bar = progress_bars[data.name];
  49. if (!bar) progress_bars[data.name] = bar = {label: data.label};
  50. bar.is_updated = true;
  51. bar.value = data.value;
  52. bar.max = data.max;
  53. if (bar.max === 0) {
  54. bar.value = 1;
  55. bar.max = 1;
  56. }
  57. // Don't retain items that complete before the dialog pops up.
  58. if (!popup_delay_expired && (bar.value >= bar.max))
  59. delete progress_bars[data.name];
  60. }
  61.  
  62. //------------------------------
  63. // Check if some progress is still pending.
  64. //------------------------------
  65. function have_pending() {
  66. var all_done = true;
  67. for (name in progress_bars) {
  68. var progress_bar = progress_bars[name];
  69. if (progress_bar.value < progress_bar.max) all_done = false;
  70. }
  71. return !all_done;
  72. }
  73.  
  74. //------------------------------
  75. // Delay the dialog from popping up until progress takes at least N milliseconds.
  76. //------------------------------
  77. function start_popup_delay() {
  78. popup_delay_started = true;
  79. popup_timer = setTimeout(function(){
  80. popup_delay_expired = true;
  81. update_progress();
  82. }, popup_delay);
  83. }
  84.  
  85. //------------------------------
  86. // Update the contents of the progress dialog (if it's currently visible)
  87. //------------------------------
  88. function update_dialog() {
  89. if (!externals_requested) {
  90. externals_requested = true;
  91. load_externals()
  92. .then(function(){
  93. externals_loaded = true;
  94. update_progress();
  95. });
  96. return;
  97. }
  98. if (!externals_loaded) return;
  99. if (user_closed) return;
  100.  
  101. if (!dialog_visible) {
  102. dialog_visible = true;
  103. if ($('#wkof_ds').length === 0) $('body').prepend('<div id="wkof_ds"></div>');
  104.  
  105. dialog = $('<div id="wkof_progbar_dlg" class="wkofs_progress_dlg" style="display:none;"></div>');
  106.  
  107. dialog.dialog({
  108. title: 'Loading Data...',
  109. minHeight: 20,
  110. maxHeight: window.innerHeight,
  111. height: 'auto',
  112. modal: false,
  113. resizable: false,
  114. autoOpen: false,
  115. appendTo: '#wkof_ds',
  116. close: dialog_close
  117. });
  118. dialog.dialog('open');
  119. }
  120.  
  121. var all_done = true;
  122. for (name in progress_bars) {
  123. var progress_bar = progress_bars[name];
  124. if (progress_bar.value < progress_bar.max) all_done = false;
  125. var bar = $('#wkof_progbar_dlg .wkof_progbar_wrap[name="'+name+'"]');
  126. if (bar.length === 0) {
  127. bar = $('<div class="wkof_progbar_wrap" name="'+name+'"><label>'+progress_bar.label+'</label><div class="wkof_progbar"></div></div>');
  128. var bars = $('#wkof_progbar_dlg .wkof_progbar_wrap');
  129. bars.push(bar[0]);
  130. $('#wkof_progbar_dlg').append(bars.sort(bar_label_compare));
  131. }
  132. if (progress_bar.is_updated) {
  133. progress_bar.is_updated = false;
  134. bar.find('.wkof_progbar').progressbar({value: progress_bar.value, max: progress_bar.max});
  135. }
  136. }
  137.  
  138. if (all_done) shutdown();
  139. }
  140.  
  141. function dialog_close() {
  142. dialog.dialog('destroy');
  143. dialog_visible = false;
  144. user_closed = true;
  145. }
  146.  
  147. //------------------------------
  148. // Load external support files (jquery UI and stylesheet)
  149. //------------------------------
  150. function load_externals() {
  151. if (location.hostname.match(/^(www\.)?wanikani\.com$/) !== null)
  152. css_url = 'https://raw.githubusercontent.com/rfindley/wanikani-open-framework/0017ff1257f2fae9823ac0fccdc6874315a8d039/jqui-wkmain.css';
  153.  
  154. return Promise.all([
  155. wkof.load_script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js', true /* cache */),
  156. wkof.load_css(css_url, true /* cache */)
  157. ]);
  158. }
  159.  
  160. //------------------------------
  161. // Comparison function for sorting progress bars.
  162. //------------------------------
  163. function bar_label_compare(a, b){
  164. var a = $(a).find('label').text();
  165. var b = $(b).find('label').text();
  166. return a.localeCompare(b);
  167. }
  168.  
  169. //------------------------------
  170. // Shut down the dialog box and cancel the popup delay timer.
  171. //------------------------------
  172. function shutdown() {
  173. // If popup timer was pending, cancel it.
  174. if (popup_delay_started && !popup_delay_expired)
  175. clearTimeout(popup_timer);
  176. popup_delay_started = false;
  177. popup_delay_expired = false;
  178.  
  179. // If progress dialog is open, close it.
  180. if (dialog_visible) dialog.dialog('close');
  181. user_closed = false;
  182. progress_bars = {};
  183. }
  184.  
  185. function set_ready_state(){
  186. // Delay guarantees include() callbacks are called before ready() callbacks.
  187. setTimeout(function(){wkof.set_state('wkof.Progress', 'ready');},0);
  188. }
  189. set_ready_state();
  190.  
  191. })(window);