mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 06:01:52 +08:00

* update wml events to be parseable * [v3] Rename `CustomEvent` to `WailsEvent` to prevent clashing with the standard JS `CustomEvent` object --------- Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2 lines
5.9 KiB
JavaScript
2 lines
5.9 KiB
JavaScript
(()=>{var Z=Object.defineProperty;var p=(t,e)=>{for(var n in e)Z(t,n,{get:e[n],enumerable:!0})};var v={};p(v,{SetText:()=>te,Text:()=>ne});var $=window.location.origin+"/wails/runtime";function ee(t,e,n){let i=new URL($);i.searchParams.append("method",t),n&&i.searchParams.append("args",JSON.stringify(n));let o={headers:{}};return e&&(o.headers["x-wails-window-name"]=e),new Promise((l,f)=>{fetch(i,o).then(a=>{if(a.ok)return a.headers.get("Content-Type")&&a.headers.get("Content-Type").indexOf("application/json")!==-1?a.json():a.text();f(Error(a.statusText))}).then(a=>l(a)).catch(a=>f(a))})}function r(t,e){return function(n,i=null){return ee(t+"."+n,e,i)}}var k=r("clipboard");function te(t){k("SetText",{text:t})}function ne(){return k("Text")}var S={};p(S,{Hide:()=>ie,Quit:()=>re,Show:()=>oe});var C=r("application");function ie(){C("Hide")}function oe(){C("Show")}function re(){C("Quit")}var M={};p(M,{Log:()=>ae});var le=r("log");function ae(t){return le("Log",t)}var E={};p(E,{GetAll:()=>ue,GetCurrent:()=>se,GetPrimary:()=>ce});var b=r("screens");function ue(){return b("GetAll")}function ce(){return b("GetPrimary")}function se(){return b("GetCurrent")}var fe="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";var w=(t=21)=>{let e="",n=t;for(;n--;)e+=fe[Math.random()*64|0];return e};var de=r("call"),c=new Map;function me(){let t;do t=w();while(c.has(t));return t}function A(t,e,n){let i=c.get(t);i&&(n?i.resolve(JSON.parse(e)):i.resolve(e),c.delete(t))}function W(t,e){let n=c.get(t);n&&(n.reject(e),c.delete(t))}function R(t,e){return new Promise((n,i)=>{let o=me();e=e||{},e["call-id"]=o,c.set(o,{resolve:n,reject:i}),de(t,e).catch(l=>{i(l),c.delete(o)})})}function T(t){return R("Call",t)}function P(t,e,...n){return R("Call",{packageName:"wails-plugins",structName:t,methodName:e,args:n})}function y(t){let e=r("window",t);return{Center:()=>void e("Center"),SetTitle:n=>void e("SetTitle",{title:n}),Fullscreen:()=>void e("Fullscreen"),UnFullscreen:()=>void e("UnFullscreen"),SetSize:(n,i)=>e("SetSize",{width:n,height:i}),Size:()=>e("Size"),SetMaxSize:(n,i)=>void e("SetMaxSize",{width:n,height:i}),SetMinSize:(n,i)=>void e("SetMinSize",{width:n,height:i}),SetAlwaysOnTop:n=>void e("SetAlwaysOnTop",{alwaysOnTop:n}),SetPosition:(n,i)=>e("SetPosition",{x:n,y:i}),Position:()=>e("Position"),Screen:()=>e("Screen"),Hide:()=>void e("Hide"),Maximise:()=>void e("Maximise"),Show:()=>void e("Show"),Close:()=>void e("Close"),ToggleMaximise:()=>void e("ToggleMaximise"),UnMaximise:()=>void e("UnMaximise"),Minimise:()=>void e("Minimise"),UnMinimise:()=>void e("UnMinimise"),SetBackgroundColour:(n,i,o,l)=>void e("SetBackgroundColour",{r:n,g:i,b:o,a:l})}}var pe=r("events"),L=class{constructor(e,n,i){this.eventName=e,this.maxCallbacks=i||-1,this.Callback=o=>(n(o),this.maxCallbacks===-1?!1:(this.maxCallbacks-=1,this.maxCallbacks===0))}},g=class{constructor(e,n=null){this.name=e,this.data=n}},u=new Map;function x(t,e,n){let i=u.get(t)||[],o=new L(t,e,n);return i.push(o),u.set(t,i),()=>we(o)}function N(t,e){return x(t,e,-1)}function F(t,e){return x(t,e,1)}function we(t){let e=t.eventName,n=u.get(e).filter(i=>i!==t);n.length===0?u.delete(e):u.set(e,n)}function D(t){console.log("dispatching event: ",{event:t});let e=u.get(t.name);if(e){let n=[];e.forEach(i=>{i.Callback(t)&&n.push(i)}),n.length>0&&(e=e.filter(i=>!n.includes(i)),e.length===0?u.delete(t.name):u.set(t.name,e))}}function U(t,...e){[t,...e].forEach(i=>{u.delete(i)})}function z(){u.clear()}function h(t){pe("Emit",t)}var ge=r("dialog"),s=new Map;function xe(){let t;do t=w();while(s.has(t));return t}function G(t,e,n){let i=s.get(t);i&&(n?i.resolve(JSON.parse(e)):i.resolve(e),s.delete(t))}function B(t,e){let n=s.get(t);n&&(n.reject(e),s.delete(t))}function d(t,e){return new Promise((n,i)=>{let o=xe();e=e||{},e["dialog-id"]=o,s.set(o,{resolve:n,reject:i}),ge(t,e).catch(l=>{i(l),s.delete(o)})})}function I(t){return d("Info",t)}function Q(t){return d("Warning",t)}function H(t){return d("Error",t)}function m(t){return d("Question",t)}function J(t){return d("OpenFile",t)}function Y(t){return d("SaveFile",t)}var he=r("contextmenu");function ve(t,e,n,i){return he("OpenContextMenu",{id:t,x:e,y:n,data:i})}function j(t){t?window.addEventListener("contextmenu",q):window.removeEventListener("contextmenu",q)}function q(t){X(t.target,t)}function X(t,e){let n=t.getAttribute("data-contextmenu");if(n)e.preventDefault(),ve(n,e.clientX,e.clientY,t.getAttribute("data-contextmenu-data"));else{let i=t.parentElement;i&&X(i,e)}}function _(t,e=null){let n=new g(t,e);h(n)}function Ce(){document.querySelectorAll("[data-wml-event]").forEach(function(e){let n=e.getAttribute("data-wml-event"),i=e.getAttribute("data-wml-confirm"),o=e.getAttribute("data-wml-trigger")||"click",l=function(){if(i){m({Title:"Confirm",Message:i,Buttons:[{Label:"Yes"},{Label:"No",IsDefault:!0}]}).then(function(f){f!=="No"&&_(n)});return}_(n)};e.removeEventListener(o,l),e.addEventListener(o,l)})}function K(t){wails.Window[t]===void 0&&console.log("Window method "+t+" not found"),wails.Window[t]()}function Se(){document.querySelectorAll("[data-wml-window]").forEach(function(e){let n=e.getAttribute("data-wml-window"),i=e.getAttribute("data-wml-confirm"),o=e.getAttribute("data-wml-trigger")||"click",l=function(){if(i){m({Title:"Confirm",Message:i,Buttons:[{Label:"Yes"},{Label:"No",IsDefault:!0}]}).then(function(f){f!=="No"&&K(n)});return}K(n)};e.removeEventListener(o,l),e.addEventListener(o,l)})}function O(){Ce(),Se()}window.wails={...V(null)};window._wails={dialogCallback:G,dialogErrorCallback:B,dispatchWailsEvent:D,callCallback:A,callErrorCallback:W};function V(t){return{Clipboard:{...v},Application:{...S,GetWindowByName(e){return V(e)}},Log:M,Screens:E,Call:T,Plugin:P,WML:{Reload:O},Dialog:{Info:I,Warning:Q,Error:H,Question:m,OpenFile:J,SaveFile:Y},Events:{Emit:h,On:N,Once:F,OnMultiple:x,Off:U,OffAll:z},Window:y(t)}}console.log("Wails v3.0.0 Debug Mode Enabled");j(!0);document.addEventListener("DOMContentLoaded",function(t){O()});})();
|