samandesk 1.0.24 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,102 +1,139 @@
1
- let m;
2
- const c = document.createElement("iframe");
3
- let p, h, f;
4
- const g = document.createElement("div"), k = {
1
+ let token;
2
+ const frame = document.createElement("iframe");
3
+ let theme;
4
+ let color;
5
+ let language;
6
+ let linkHandler = (link) => window.open(link, "_blank", "noopener");
7
+ const backdrop = document.createElement("div");
8
+ const run = {
5
9
  count: 0,
6
10
  promises: {}
7
11
  };
8
- function b() {
9
- return k;
12
+ function getRun() {
13
+ return run;
10
14
  }
11
- function y(n) {
12
- m = n;
15
+ function setToken(newToken) {
16
+ token = newToken;
13
17
  }
14
- function E() {
15
- return m;
18
+ function getToken() {
19
+ return token;
16
20
  }
17
- function d() {
18
- c.src = "";
21
+ function clearFrame() {
22
+ frame.src = "";
19
23
  }
20
- function l(n) {
21
- c.src = n;
24
+ function setFrameSrc(src) {
25
+ frame.src = src;
22
26
  }
23
- function s() {
24
- return c;
27
+ function getFrame() {
28
+ return frame;
25
29
  }
26
- function w() {
27
- return { theme: p, color: h };
30
+ function getTheme() {
31
+ return { theme, color };
28
32
  }
29
- function x() {
30
- return { language: f };
33
+ function getLanguage() {
34
+ return { language };
31
35
  }
32
- function v(n) {
33
- p = n.darkMode, h = n.color;
36
+ function setTheme(newTheme) {
37
+ theme = newTheme.darkMode;
38
+ color = newTheme.color;
34
39
  }
35
- function S(n) {
36
- f = n;
40
+ function setLanguage(newLanguage) {
41
+ language = newLanguage;
37
42
  }
38
- function i() {
39
- return g;
43
+ function getBackdrop() {
44
+ return backdrop;
40
45
  }
41
- const C = new Event("ready"), D = new Event("open"), O = new Event("close");
42
- function I() {
43
- return C;
46
+ function setOpenLinkFn(fn) {
47
+ if (fn) linkHandler = fn;
44
48
  }
45
- function M() {
46
- return D;
49
+ function getOpenLinkFn() {
50
+ return linkHandler;
47
51
  }
48
- function T() {
49
- return O;
52
+ const readyEvent = new Event("ready");
53
+ const openEvent = new Event("open");
54
+ const closeEvent = new Event("close");
55
+ function getReadyEvent() {
56
+ return readyEvent;
50
57
  }
51
- function R(n) {
52
- window.addEventListener("message", (e) => {
53
- if (!e.data.from || e.data.from !== "SamanDesk")
54
- return;
55
- let t = !1;
56
- console.log(`message from (${e.origin})`, e);
58
+ function getOpenEvent() {
59
+ return openEvent;
60
+ }
61
+ function getCloseEvent() {
62
+ return closeEvent;
63
+ }
64
+ function handleEvents(interoperation) {
65
+ window.addEventListener("message", (event) => {
66
+ if (!event.data.from || event.data.from !== "SamanDesk") return;
67
+ let handledByInterOperation = false;
68
+ console.log(`message from (${event.origin})`, event);
57
69
  try {
58
- n[e.data.action](e.data), t = !0;
59
- } catch {
70
+ interoperation[event.data.action](event.data);
71
+ handledByInterOperation = true;
72
+ } catch (err) {
60
73
  console.error("error while run event action");
61
74
  }
62
- const o = e.data, a = o.id, r = b();
63
- a in r.promises ? (r.promises[a](o), delete r.promises[a]) : t || console.error("Message from outside of frame is invalid");
75
+ const data = event.data;
76
+ const id = data.id;
77
+ const run2 = getRun();
78
+ if (id in run2.promises) {
79
+ run2.promises[id](data);
80
+ delete run2.promises[id];
81
+ } else if (!handledByInterOperation) {
82
+ console.error("Message from outside of frame is invalid");
83
+ }
64
84
  });
65
85
  }
66
- function $(n, e, t) {
67
- var r;
68
- const o = { from: "SamanDesk", action: t, data: e, id: n };
69
- (r = s().contentWindow) == null || r.postMessage(o, "*");
86
+ function resolveRun(id, data, action) {
87
+ var _a;
88
+ const postData = { from: "SamanDesk", action, data, id };
89
+ const frame2 = getFrame();
90
+ (_a = frame2.contentWindow) == null ? void 0 : _a.postMessage(postData, "*");
70
91
  }
71
- class L {
92
+ class InterOperation {
72
93
  async init() {
73
- R(this);
94
+ handleEvents(this);
74
95
  }
75
96
  /** توکن را از میزبان دریافت کرده و آن را ذخیره می‌کند. سپس به میزبان اعلام می‌کند که آماده‌ی ادامه‌ی کار است */
76
- setToken(e) {
77
- y(e.data.token), d(), window.dispatchEvent(I());
97
+ setToken(data) {
98
+ setToken(data.data.token);
99
+ clearFrame();
100
+ window.dispatchEvent(getReadyEvent());
78
101
  }
79
102
  /** بستن کتابخانه */
80
103
  close() {
81
- d();
82
- const e = i();
83
- document.body.removeChild(e), e.onclick = null, document.body.removeChild(s()), window.dispatchEvent(T());
104
+ clearFrame();
105
+ const backdrop2 = getBackdrop();
106
+ document.body.removeChild(backdrop2);
107
+ backdrop2.onclick = null;
108
+ document.body.removeChild(getFrame());
109
+ window.dispatchEvent(getCloseEvent());
84
110
  }
85
111
  /** اطلاعات کتابخانه را به بیرون اطلاع می‌دهد */
86
- getConfig(e) {
87
- const t = w(), o = x();
88
- $(e.id, { color: t.color, theme: t.theme, language: o.language }, e.action);
112
+ getConfig(data) {
113
+ const themeData = getTheme();
114
+ const languageData = getLanguage();
115
+ resolveRun(data.id, { color: themeData.color, theme: themeData.theme, language: languageData.language }, data.action);
116
+ }
117
+ /** لینک موردنظر را به وسیله‌ی تابع از پیش تعریف‌شده باز می‌کند */
118
+ openLink(data) {
119
+ getOpenLinkFn()(data.data.link);
89
120
  }
90
121
  }
91
- const u = {}.VITE_DEV_MODE === "1" ? "http://localhost:2525" : "https://samandesk.com";
92
- class F {
122
+ const SITE_URL = "https://samandesk.com";
123
+ class SamanDesk {
93
124
  /**
94
125
  * ⁧این تابع در ابتدا مقدار verifiedToken را دریافت کرده و سپس مقدار token‌ را دریافت و سپس آن را ذخیره می‌کند
95
126
  */
96
- async init(e) {
97
- this.interoperation = new L(), this.interoperation.init();
98
- const t = s(), o = i();
99
- l(`${u}/setup-interoperation/${e}`), t.width = "0", t.style.width = "0", document.body.appendChild(o), document.body.appendChild(t);
127
+ async init(verifiedToken) {
128
+ this.interoperation = new InterOperation();
129
+ this.interoperation.init();
130
+ const frame2 = getFrame();
131
+ const backdrop2 = getBackdrop();
132
+ setFrameSrc(`${SITE_URL}/setup-interoperation/${verifiedToken}`);
133
+ frame2.width = "0";
134
+ frame2.style.width = "0";
135
+ document.body.appendChild(backdrop2);
136
+ document.body.appendChild(frame2);
100
137
  }
101
138
  /**
102
139
  * استایل مربوط به آیفریم را تنظیم می‌کند
@@ -104,10 +141,12 @@ class F {
104
141
  * @param fullScreen - تمام صفحه باز شود یا خیر
105
142
  *
106
143
  */
107
- setStyle(e) {
108
- const t = s(), o = i();
109
- t.style.position = "fixed", t.style.zIndex = "1000";
110
- const a = {
144
+ setStyle(fullScreen) {
145
+ const frame2 = getFrame();
146
+ const backdrop2 = getBackdrop();
147
+ frame2.style.position = "fixed";
148
+ frame2.style.zIndex = "1000";
149
+ const backdropStyles = {
111
150
  position: "fixed",
112
151
  top: "0",
113
152
  bottom: "0",
@@ -116,46 +155,61 @@ class F {
116
155
  backgroundColor: "rgba(0,0,0,0.5)",
117
156
  zIndex: "1000"
118
157
  };
119
- Object.assign(o.style, a), e ? (Object.assign(t.style, {
120
- bottom: "0",
121
- left: "0",
122
- top: "0",
123
- right: "0",
124
- width: "100%"
125
- }), t.width = "100%", t.height = "100%") : Object.assign(t.style, {
126
- top: "50%",
127
- left: "50%",
128
- transform: "translate(-50%, -50%)",
129
- width: "100%",
130
- maxWidth: "500px",
131
- boxShadow: "0px 0px 100px 0px rgba(0,0,0,0.48)",
132
- borderRadius: "24px",
133
- height: "100%",
134
- maxHeight: "660px"
135
- });
158
+ Object.assign(backdrop2.style, backdropStyles);
159
+ if (fullScreen) {
160
+ Object.assign(frame2.style, {
161
+ bottom: "0",
162
+ left: "0",
163
+ top: "0",
164
+ right: "0",
165
+ width: "100%"
166
+ });
167
+ frame2.width = "100%";
168
+ frame2.height = "100%";
169
+ } else {
170
+ Object.assign(frame2.style, {
171
+ top: "50%",
172
+ left: "50%",
173
+ transform: "translate(-50%, -50%)",
174
+ width: "100%",
175
+ maxWidth: "500px",
176
+ boxShadow: "0px 0px 100px 0px rgba(0,0,0,0.48)",
177
+ borderRadius: "24px",
178
+ height: "100%",
179
+ maxHeight: "660px"
180
+ });
181
+ }
136
182
  }
137
183
  /**
138
184
  * این تابع آیفریم را باز می‌کند.
139
185
  */
140
- open(e) {
141
- const t = s(), o = i();
142
- v({
143
- darkMode: e.theme.darkMode ?? "light",
144
- color: e.theme.color ?? "default"
145
- }), S(e.language ?? "fa");
146
- const a = e.link || `${u}/department-selection?token=${E()}`;
147
- l(a), this.setStyle(e.fullScreen), document.body.appendChild(o), document.body.appendChild(t), o.onclick = () => {
186
+ open(config) {
187
+ const frame2 = getFrame();
188
+ const backdrop2 = getBackdrop();
189
+ setTheme({
190
+ darkMode: config.theme.darkMode ?? "light",
191
+ color: config.theme.color ?? "default"
192
+ });
193
+ setLanguage(config.language ?? "fa");
194
+ const src = config.link || `${SITE_URL}/department-selection?token=${getToken()}`;
195
+ setFrameSrc(src);
196
+ setOpenLinkFn(config.linkHandler);
197
+ this.setStyle(config.fullScreen);
198
+ document.body.appendChild(backdrop2);
199
+ document.body.appendChild(frame2);
200
+ backdrop2.onclick = () => {
148
201
  this.close();
149
- }, window.dispatchEvent(M());
202
+ };
203
+ window.dispatchEvent(getOpenEvent());
150
204
  }
151
205
  /**
152
206
  * بستن کتابخانه
153
207
  */
154
208
  close() {
155
- var e;
156
- (e = this.interoperation) == null || e.close();
209
+ var _a;
210
+ (_a = this.interoperation) == null ? void 0 : _a.close();
157
211
  }
158
212
  }
159
213
  export {
160
- F as SamanDesk
214
+ SamanDesk
161
215
  };
@@ -1 +1 @@
1
- (function(a,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(a=typeof globalThis<"u"?globalThis:a||self,r(a.SamanDesk={}))})(this,function(a){"use strict";let r;const l=document.createElement("iframe");let u,m,p;const k=document.createElement("div"),y={count:0,promises:{}};function b(){return y}function E(n){r=n}function w(){return r}function f(){l.src=""}function h(n){l.src=n}function c(){return l}function x(){return{theme:u,color:m}}function v(){return{language:p}}function S(n){u=n.darkMode,m=n.color}function D(n){p=n}function d(){return k}const C=new Event("ready"),T=new Event("open"),O=new Event("close");function M(){return C}function I(){return T}function j(){return O}function R(n){window.addEventListener("message",e=>{if(!e.data.from||e.data.from!=="SamanDesk")return;let t=!1;console.log(`message from (${e.origin})`,e);try{n[e.data.action](e.data),t=!0}catch{console.error("error while run event action")}const o=e.data,s=o.id,i=b();s in i.promises?(i.promises[s](o),delete i.promises[s]):t||console.error("Message from outside of frame is invalid")})}function $(n,e,t){var i;const o={from:"SamanDesk",action:t,data:e,id:n};(i=c().contentWindow)==null||i.postMessage(o,"*")}class L{async init(){R(this)}setToken(e){E(e.data.token),f(),window.dispatchEvent(M())}close(){f();const e=d();document.body.removeChild(e),e.onclick=null,document.body.removeChild(c()),window.dispatchEvent(j())}getConfig(e){const t=x(),o=v();$(e.id,{color:t.color,theme:t.theme,language:o.language},e.action)}}const g={}.VITE_DEV_MODE==="1"?"http://localhost:2525":"https://samandesk.com";class F{async init(e){this.interoperation=new L,this.interoperation.init();const t=c(),o=d();h(`${g}/setup-interoperation/${e}`),t.width="0",t.style.width="0",document.body.appendChild(o),document.body.appendChild(t)}setStyle(e){const t=c(),o=d();t.style.position="fixed",t.style.zIndex="1000";const s={position:"fixed",top:"0",bottom:"0",left:"0",right:"0",backgroundColor:"rgba(0,0,0,0.5)",zIndex:"1000"};Object.assign(o.style,s),e?(Object.assign(t.style,{bottom:"0",left:"0",top:"0",right:"0",width:"100%"}),t.width="100%",t.height="100%"):Object.assign(t.style,{top:"50%",left:"50%",transform:"translate(-50%, -50%)",width:"100%",maxWidth:"500px",boxShadow:"0px 0px 100px 0px rgba(0,0,0,0.48)",borderRadius:"24px",height:"100%",maxHeight:"660px"})}open(e){const t=c(),o=d();S({darkMode:e.theme.darkMode??"light",color:e.theme.color??"default"}),D(e.language??"fa");const s=e.link||`${g}/department-selection?token=${w()}`;h(s),this.setStyle(e.fullScreen),document.body.appendChild(o),document.body.appendChild(t),o.onclick=()=>{this.close()},window.dispatchEvent(I())}close(){var e;(e=this.interoperation)==null||e.close()}}a.SamanDesk=F,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).SamanDesk={})}(this,(function(e){"use strict";let t;const o=document.createElement("iframe");let n,i,a,s=e=>window.open(e,"_blank","noopener");const d=document.createElement("div"),r={count:0,promises:{}};function c(){o.src=""}function l(e){o.src=e}function p(){return o}function m(){return d}const h=new Event("ready"),u=new Event("open"),f=new Event("close");function g(e){window.addEventListener("message",(t=>{if(!t.data.from||"SamanDesk"!==t.data.from)return;let o=!1;console.log(`message from (${t.origin})`,t);try{e[t.data.action](t.data),o=!0}catch(s){console.error("error while run event action")}const n=t.data,i=n.id,a=r;i in a.promises?(a.promises[i](n),delete a.promises[i]):o||console.error("Message from outside of frame is invalid")}))}class y{async init(){g(this)}setToken(e){var o;o=e.data.token,t=o,c(),window.dispatchEvent(h)}close(){c();const e=m();document.body.removeChild(e),e.onclick=null,document.body.removeChild(p()),window.dispatchEvent(f)}getConfig(e){const t={theme:n,color:i},o={language:a};!function(e,t,o){var n;const i={from:"SamanDesk",action:o,data:t,id:e};null==(n=p().contentWindow)||n.postMessage(i,"*")}(e.id,{color:t.color,theme:t.theme,language:o.language},e.action)}openLink(e){s(e.data.link)}}const b="https://samandesk.com";e.SamanDesk=class{async init(e){this.interoperation=new y,this.interoperation.init();const t=p(),o=m();l(`${b}/setup-interoperation/${e}`),t.width="0",t.style.width="0",document.body.appendChild(o),document.body.appendChild(t)}setStyle(e){const t=p(),o=m();t.style.position="fixed",t.style.zIndex="1000";Object.assign(o.style,{position:"fixed",top:"0",bottom:"0",left:"0",right:"0",backgroundColor:"rgba(0,0,0,0.5)",zIndex:"1000"}),e?(Object.assign(t.style,{bottom:"0",left:"0",top:"0",right:"0",width:"100%"}),t.width="100%",t.height="100%"):Object.assign(t.style,{top:"50%",left:"50%",transform:"translate(-50%, -50%)",width:"100%",maxWidth:"500px",boxShadow:"0px 0px 100px 0px rgba(0,0,0,0.48)",borderRadius:"24px",height:"100%",maxHeight:"660px"})}open(e){const o=p(),d=m();var r,c;r={darkMode:e.theme.darkMode??"light",color:e.theme.color??"default"},n=r.darkMode,i=r.color,c=e.language??"fa",a=c;var h;l(e.link||`${b}/department-selection?token=${t}`),(h=e.linkHandler)&&(s=h),this.setStyle(e.fullScreen),document.body.appendChild(d),document.body.appendChild(o),d.onclick=()=>{this.close()},window.dispatchEvent(u)}close(){var e;null==(e=this.interoperation)||e.close()}},Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
@@ -42,4 +42,8 @@ export declare function setTheme(newTheme: ProjectTheme): void;
42
42
  export declare function setLanguage(newLanguage: string): void;
43
43
  /** عنصر بکدراپ را برمی‌گرداند */
44
44
  export declare function getBackdrop(): HTMLDivElement;
45
+ /** تابع مدیریت‌کننده‌ی لینک‌ها را ست می‌کند */
46
+ export declare function setOpenLinkFn(fn?: (link: string) => void): void;
47
+ /** تابع مدیریت‌کننده‌ی لینک‌ها را برمی‌گرداند */
48
+ export declare function getOpenLinkFn(): (link: string) => void;
45
49
  export {};
@@ -20,6 +20,8 @@ export interface OpenConfig {
20
20
  theme: Partial<ProjectTheme>;
21
21
  /** زبان کتابیخانه */
22
22
  language: string;
23
+ /** تابع مدیریت‌کننده‌ی لینک‌ها */
24
+ linkHandler?: (link: string) => void;
23
25
  }
24
26
  export declare class SamanDesk {
25
27
  /**
@@ -1,4 +1,12 @@
1
- import type { FunctionKeys } from 'utility-types';
1
+ import { FunctionKeys } from 'utility-types';
2
+ type MessageData<T = {}> = {
3
+ /** شناسه داده */
4
+ id: number;
5
+ /** تابعی که داده برای آن ارسال شده است */
6
+ action: string;
7
+ /** داده موردنظر */
8
+ data: T;
9
+ };
2
10
  /**
3
11
  * این تابع برای فراخوانی متدهاییست که مقداری بر نمی‌گردانند
4
12
  */
@@ -10,9 +18,16 @@ export declare function run<A extends FunctionKeys<InterOperation>>(action: A, d
10
18
  export declare class InterOperation {
11
19
  init(): Promise<void>;
12
20
  /** توکن را از میزبان دریافت کرده و آن را ذخیره می‌کند. سپس به میزبان اعلام می‌کند که آماده‌ی ادامه‌ی کار است */
13
- setToken(data: any): void;
21
+ setToken(data: MessageData<{
22
+ token: string;
23
+ }>): void;
14
24
  /** بستن کتابخانه */
15
25
  close(): void;
16
26
  /** اطلاعات کتابخانه را به بیرون اطلاع می‌دهد */
17
- getConfig(data: any): void;
27
+ getConfig(data: MessageData): void;
28
+ /** لینک موردنظر را به وسیله‌ی تابع از پیش تعریف‌شده باز می‌کند */
29
+ openLink(data: MessageData<{
30
+ link: string;
31
+ }>): void;
18
32
  }
33
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "samandesk",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "files": [
@@ -22,9 +22,10 @@
22
22
  },
23
23
  "devDependencies": {
24
24
  "cross-env": "^7.0.3",
25
- "typescript": "^5.2.2",
26
- "utility-types": "^3.10.0",
27
- "vite": "^4.4.9",
28
- "vite-plugin-dts": "^3.5.4"
25
+ "terser": "^5.39.0",
26
+ "typescript": "^5.8.3",
27
+ "utility-types": "^3.11.0",
28
+ "vite": "^6.3.3",
29
+ "vite-plugin-dts": "^4.5.3"
29
30
  }
30
31
  }