-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPlugin.ts
More file actions
executable file
·145 lines (117 loc) · 5.4 KB
/
TestPlugin.ts
File metadata and controls
executable file
·145 lines (117 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/// <reference types="jquery" />
import * as jQueryModule from 'jquery';
import {GlobalWPPluginData as GlobalWPPluginDataClass} from "./Classes/GlobalWPPluginData";
import {WPAjaxPayload as WPAjaxPayloadClass} from "./Classes/WPAjaxPayload";
import {WPAjaxResponse as WPAjaxResponseClass} from "./Classes/WPAjaxResponse";
import {AJAX as AJAXClass} from "./Classes/AJAX";
import {Helper as HelperClass} from "./Classes/Helper";
import {TypeChecker as TypeCheckerClass} from "./Classes/TypeChecker";
import {IndicatorUIHelper as IndicatorUIHelperClass} from "./Classes/IndicatorUIHelper";
import {LoaderUIHelper as LoaderUIHelperClass} from "./Classes/LoaderUIHelper";
import {ContextHelper as ContextHelperClass} from "./Classes/ContextHelper";
import {ElementGeneratorHelper as ElementGeneratorHelperClass} from "./Classes/ElementGeneratorHelper";
import {BrowserDetect as BrowserDetectClass} from "./Classes/BrowserDetect";
import {testplugin_Context as testplugin_ContextClass} from "./Contexts/admin/testplugin_Context";
export namespace TestPlugin {
export const GlobalWPPluginData = GlobalWPPluginDataClass;
export type GlobalWPPluginData = GlobalWPPluginDataClass;
export let PluginData = null;
export const WPAjaxPayload = WPAjaxPayloadClass;
export type WPAjaxPayload = WPAjaxPayloadClass;
export const WPAjaxResponse = WPAjaxResponseClass;
export type WPAjaxResponse = WPAjaxResponseClass;
export const ContextHelper = ContextHelperClass;
export type ContextHelper = ContextHelperClass;
export class Utilities {}
export namespace Utilities {
export const AJAX = AJAXClass;
export type AJAX = AJAXClass;
export const Helper = HelperClass;
export type Helper = HelperClass;
export const TypeChecker = TypeCheckerClass;
export type TypeChecker = TypeCheckerClass;
export const BrowserDetect = BrowserDetectClass;
export type BrowserDetect = BrowserDetectClass;
export const LoaderUIHelper = LoaderUIHelperClass;
export type LoaderUIHelper = LoaderUIHelperClass;
export const IndicatorUIHelper = IndicatorUIHelperClass;
export type IndicatorUIHelper = IndicatorUIHelperClass;
export const ElementGeneratorHelper = ElementGeneratorHelperClass;
export type ElementGeneratorHelper = ElementGeneratorHelperClass;
}
/* Use these on pages when grouping data to be loaded for that page. Strongly types some fields / data to help prevent runtime issues. */
export class Contexts {}
export namespace Contexts {
export class admin {}
export namespace admin {
export const testplugin_Context = testplugin_ContextClass;
export type testplugin_Context = testplugin_ContextClass;
}
export class user {}
export namespace user {}
}
export class Extensions {}
export namespace Extensions {
//used to expose items to the page scripts that are only available to TypeScript (and not requirejs)
}
/* Any custom events to help trigger customized functionality, but have some common method for triggering them */
export class Events {}
export namespace Events {
export const MyEvent = new CustomEvent('customEvent', {
bubbles: true
});
}
export function init(pluginData:any) {
if(!pluginData) {
throw new TypeError("The pluginData parameter was invalid.")
} else {
TestPlugin.PluginData = TestPlugin.GlobalWPPluginData.fromRawObj(pluginData);
}
setJQueryDefaults(jQueryModule);
}
export function attachHandlers() {
let body = jQuery("body");
//body.on("click",".testClass",function(){});
body.on("click",".linkButton", function(){
let $this = jQuery(this);
let urlToOpen = $this.data('link-to-open');
let urlTarget = $this.data('open-target');
if(TestPlugin.Utilities.TypeChecker.isEmpty(urlToOpen) ) {
return;
}
if(TestPlugin.Utilities.TypeChecker.isEmpty(urlTarget) ) {
urlTarget = "current";
}
switch (urlTarget) {
case "new":
TestPlugin.Utilities.Helper.openInNewTab(urlToOpen);
break;
default:
window.location.href = urlToOpen;
}
});
}
export function setJQueryDefaults(jq:JQueryStatic){
//set ajax defaults
var jsonMimeType = "application/json;charset=UTF-8";
jq.ajaxSetup({
type: "POST",
url: TestPlugin.PluginData.ajaxurl,
dataType: "json",
traditional: true,
error: TestPlugin.Utilities.AJAX.standardAjaxError,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType(jsonMimeType);
}
},
});
jq(this).ajaxSuccess(function(event, request) {
let data = request.responseJSON;
if(!TestPlugin.Utilities.TypeChecker.isUndefined(data.newNonce)) {
TestPlugin.PluginData.nonce = data.newNonce;
}
});
TestPlugin.Utilities.Helper.addCustomValidators(jQuery);
}
}