-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.js
152 lines (147 loc) · 5.56 KB
/
global.js
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
146
147
148
149
150
151
152
// export default globalFn;
// export let globalFn;
var WebNotifycount = 0;
const globalFn = {
OnAddinLoad(ribbonUI) {
if (typeof (wps.ribbonUI) != "object") {
wps.ribbonUI = ribbonUI;
}
if (typeof (wps.Enum) != "object") { // 如果没有内置枚举值
wps.Enum = WPS_Enum;
}
wps.PluginStorage.setItem("EnableFlag", false); //往PluginStorage中设置一个标记,用于控制两个按钮的置灰
wps.PluginStorage.setItem("ApiEventFlag", false); //往PluginStorage中设置一个标记,用于控制ApiEvent的按钮label
return true;
},
OnAction(control) {
const eleId = control.Id;
switch (eleId) {
case "btnShowMsg":
{
const doc = wps.EtApplication().ActiveWorkbook;
if (!doc) {
alert("当前没有打开任何文档");
return;
}
alert(doc.Name);
}
break;
case "btnIsEnbable":
{
let bFlag = wps.PluginStorage.getItem("EnableFlag");
wps.PluginStorage.setItem("EnableFlag", !bFlag);
//通知wps刷新以下几个按饰的状态
wps.ribbonUI.InvalidateControl("btnIsEnbable");
wps.ribbonUI.InvalidateControl("btnShowDialog");
wps.ribbonUI.InvalidateControl("btnShowTaskPane");
//wps.ribbonUI.Invalidate(); 这行代码打开则是刷新所有的按钮状态
break;
}
case "btnShowDialog":
wps.ShowDialog(GetUrlPath() + "/ui/dialog.html", "这是一个对话框网页", 400 * window.devicePixelRatio, 400 * window.devicePixelRatio, false);
break;
case "btnShowTaskPane":
{
let tsId = wps.PluginStorage.getItem("taskpane_id");
if (!tsId) {
let tskpane = wps.CreateTaskPane(GetUrlPath() + "/ui/taskpane.html");
let id = tskpane.ID;
wps.PluginStorage.setItem("taskpane_id", id);
tskpane.Visible = true;
} else {
let tskpane = wps.GetTaskPane(tsId);
tskpane.Visible = !tskpane.Visible;
}
}
break;
case "btnApiEvent":
{
let bFlag = wps.PluginStorage.getItem("ApiEventFlag");
let bRegister = bFlag ? false : true;
wps.PluginStorage.setItem("ApiEventFlag", bRegister);
if (bRegister) {
wps.ApiEvent.AddApiEventListener('NewWorkbook', OnNewDocumentApiEvent);
}
else {
wps.ApiEvent.RemoveApiEventListener('NewWorkbook', OnNewDocumentApiEvent);
}
wps.ribbonUI.InvalidateControl("btnApiEvent");
}
break;
case "btnWebNotify":
{
let currentTime = new Date();
let timeStr = currentTime.getHours() + ':' + currentTime.getMinutes() + ":" + currentTime.getSeconds();
wps.OAAssist.WebNotify("这行内容由wps加载项主动送达给业务系统,可以任意自定义, 比如时间值:" + timeStr + ",次数:" + (++WebNotifycount), true);
}
break;
default:
break;
}
return true;
},
GetImage(control) {
const eleId = control.Id;
switch (eleId) {
case "btnShowMsg":
return "images/1.svg";
case "btnShowDialog":
return "images/2.svg";
case "btnShowTaskPane":
return "images/3.svg";
default:
;
}
return "images/newFromTemp.svg";
},
OnGetEnabled(control) {
const eleId = control.Id;
switch (eleId) {
case "btnShowMsg":
return true;
break;
case "btnShowDialog":
{
let bFlag = wps.PluginStorage.getItem("EnableFlag");
return bFlag;
break;
}
case "btnShowTaskPane":
{
let bFlag = wps.PluginStorage.getItem("EnableFlag");
return bFlag;
break;
}
default:
break;
}
return true;
},
OnGetVisible(control) {
return true;
},
OnGetLabel(control) {
const eleId = control.Id;
switch (eleId) {
case "btnIsEnbable":
{
let bFlag = wps.PluginStorage.getItem("EnableFlag");
return bFlag ? "按钮Disable" : "按钮Enable";
break;
}
case "btnApiEvent":
{
let bFlag = wps.PluginStorage.getItem("ApiEventFlag");
return bFlag ? "清除新建文件事件" : "注册新建文件事件";
break;
}
}
return "";
},
OnNewDocumentApiEvent(doc) {
alert("新建文件事件响应,取文件名: " + doc.Name);
}
};
for (const key in globalFn) {
window[key] = globalFn[key];/* 挂载函数为全局函数 */
}