add: notify hook example
This commit is contained in:
		
							parent
							
								
									aa9763e9a9
								
							
						
					
					
						commit
						84beaab5fd
					
				@ -1,6 +1,6 @@
 | 
			
		||||
<vbox
 | 
			
		||||
  id="zotero-prefpane-__addonRef__"
 | 
			
		||||
  onload="Zotero.AddonTemplate.hooks.onCustomEvent('prefLoad', {window})"
 | 
			
		||||
  onload="Zotero.AddonTemplate.hooks.onPrefsEvent('load', {window})"
 | 
			
		||||
>
 | 
			
		||||
  <groupbox>
 | 
			
		||||
    <label><html:h2>&zotero.__addonRef__.pref.title;</html:h2></label>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										48
									
								
								src/hooks.ts
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								src/hooks.ts
									
									
									
									
									
								
							@ -10,20 +10,20 @@ import { registerPrefsScripts } from "./modules/preferenceScript";
 | 
			
		||||
async function onStartup() {
 | 
			
		||||
  initLocale();
 | 
			
		||||
 | 
			
		||||
  const w = showProgressWindow(
 | 
			
		||||
  const progWin = showProgressWindow(
 | 
			
		||||
    config.addonName,
 | 
			
		||||
    getString("startup.begin"),
 | 
			
		||||
    "default",
 | 
			
		||||
    -1
 | 
			
		||||
  );
 | 
			
		||||
  changeProgressWindowLine(w, { newProgress: 0 });
 | 
			
		||||
  changeProgressWindowLine(progWin, { newProgress: 0 });
 | 
			
		||||
 | 
			
		||||
  BasicExampleFactory.registerPrefs();
 | 
			
		||||
 | 
			
		||||
  BasicExampleFactory.registerNotifier();
 | 
			
		||||
 | 
			
		||||
  await Zotero.Promise.delay(1000);
 | 
			
		||||
  changeProgressWindowLine(w, {
 | 
			
		||||
  changeProgressWindowLine(progWin, {
 | 
			
		||||
    newProgress: 30,
 | 
			
		||||
    newText: `[30%] ${getString("startup.begin")}`,
 | 
			
		||||
  });
 | 
			
		||||
@ -47,11 +47,11 @@ async function onStartup() {
 | 
			
		||||
  await UIExampleFactory.registerReaderTabPanel();
 | 
			
		||||
 | 
			
		||||
  await Zotero.Promise.delay(1000);
 | 
			
		||||
  changeProgressWindowLine(w, {
 | 
			
		||||
  changeProgressWindowLine(progWin, {
 | 
			
		||||
    newProgress: 100,
 | 
			
		||||
    newText: `[100%] ${getString("startup.finish")}`,
 | 
			
		||||
  });
 | 
			
		||||
  w.startCloseTimer(5000);
 | 
			
		||||
  progWin.startCloseTimer(5000);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function onShutdown(): void {
 | 
			
		||||
@ -63,14 +63,37 @@ function onShutdown(): void {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This function is just a dispatcher for UI events.
 | 
			
		||||
 * Any operations should be placed in a function to keep this funcion clear
 | 
			
		||||
 * This function is just an example of dispatcher for Notify events.
 | 
			
		||||
 * Any operations should be placed in a function to keep this funcion clear.
 | 
			
		||||
 */
 | 
			
		||||
async function onNotify(
 | 
			
		||||
  event: string,
 | 
			
		||||
  type: string,
 | 
			
		||||
  ids: Array<string>,
 | 
			
		||||
  extraData: { [key: string]: any }
 | 
			
		||||
) {
 | 
			
		||||
  // You can add your code to the corresponding notify type
 | 
			
		||||
  ztoolkit.Tool.log("notify", event, type, ids, extraData);
 | 
			
		||||
  if (
 | 
			
		||||
    event == "select" &&
 | 
			
		||||
    type == "tab" &&
 | 
			
		||||
    extraData[ids[0]].type == "reader"
 | 
			
		||||
  ) {
 | 
			
		||||
    BasicExampleFactory.exampleNotifierCallback();
 | 
			
		||||
  } else {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This function is just an example of dispatcher for Preference UI events.
 | 
			
		||||
 * Any operations should be placed in a function to keep this funcion clear.
 | 
			
		||||
 * @param type event type
 | 
			
		||||
 * @param data event data
 | 
			
		||||
 */
 | 
			
		||||
function onCustomEvent(type: string, data: { [key: string]: any }) {
 | 
			
		||||
async function onPrefsEvent(type: string, data: { [key: string]: any }) {
 | 
			
		||||
  switch (type) {
 | 
			
		||||
    case "prefLoad":
 | 
			
		||||
    case "load":
 | 
			
		||||
      registerPrefsScripts(data.window);
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
@ -78,8 +101,13 @@ function onCustomEvent(type: string, data: { [key: string]: any }) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Add your hooks here. For element click, etc.
 | 
			
		||||
// Keep in mind hooks only do dispatch. Don't add code that does real jobs in hooks.
 | 
			
		||||
// Otherwise the code would be hard to read and maintian.
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
  onStartup,
 | 
			
		||||
  onShutdown,
 | 
			
		||||
  onCustomEvent,
 | 
			
		||||
  onNotify,
 | 
			
		||||
  onPrefsEvent,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
import { config } from "../../package.json";
 | 
			
		||||
import { getString } from "./locale";
 | 
			
		||||
import { showProgressWindow } from "./progressWindow";
 | 
			
		||||
 | 
			
		||||
function example(
 | 
			
		||||
  target: any,
 | 
			
		||||
@ -38,18 +39,7 @@ export class BasicExampleFactory {
 | 
			
		||||
          this.unregisterNotifier(notifierID);
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        ztoolkit.Tool.log("notify", event, type, ids, extraData);
 | 
			
		||||
        // You can add your code to the corresponding notify type
 | 
			
		||||
        if (
 | 
			
		||||
          event == "select" &&
 | 
			
		||||
          type == "tab" &&
 | 
			
		||||
          extraData[ids[0]].type == "reader"
 | 
			
		||||
        ) {
 | 
			
		||||
          // Select a reader tab
 | 
			
		||||
        }
 | 
			
		||||
        if (event == "add" && type == "item") {
 | 
			
		||||
          // Add an item
 | 
			
		||||
        }
 | 
			
		||||
        addon.hooks.onNotify(event, type, ids, extraData);
 | 
			
		||||
      },
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
@ -70,6 +60,11 @@ export class BasicExampleFactory {
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @example
 | 
			
		||||
  static exampleNotifierCallback() {
 | 
			
		||||
    showProgressWindow(config.addonName, "Open Tab Detected!", "success");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @example
 | 
			
		||||
  private static unregisterNotifier(notifierID: string) {
 | 
			
		||||
    Zotero.Notifier.unregisterObserver(notifierID);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user