github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/view/assets/js/page/base.js (about) 1 export default { 2 props: { 3 activeAccount: { 4 type: Object, 5 default() { 6 return { 7 id: 0, 8 username: "", 9 owner: false, 10 }; 11 }, 12 }, 13 appOptions: { 14 type: Object, 15 default() { 16 return { 17 ShowId: false, 18 ListMode: false, 19 NightMode: false, 20 HideThumbnail: false, 21 HideExcerpt: false, 22 KeepMetadata: false, 23 UseArchive: false, 24 CreateEbook: false, 25 MakePublic: false, 26 }; 27 }, 28 }, 29 }, 30 data() { 31 return { 32 dialog: {}, 33 }; 34 }, 35 methods: { 36 defaultDialog() { 37 return { 38 visible: false, 39 loading: false, 40 title: "", 41 content: "", 42 fields: [], 43 showLabel: false, 44 mainText: "Yes", 45 secondText: "", 46 mainClick: () => { 47 this.dialog.visible = false; 48 }, 49 secondClick: () => { 50 this.dialog.visible = false; 51 }, 52 escPressed: () => { 53 if (!this.loading) this.dialog.visible = false; 54 }, 55 }; 56 }, 57 showDialog(cfg) { 58 var base = this.defaultDialog(); 59 base.visible = true; 60 if (cfg.loading) base.loading = cfg.loading; 61 if (cfg.title) base.title = cfg.title; 62 if (cfg.content) base.content = cfg.content; 63 if (cfg.fields) base.fields = cfg.fields; 64 if (cfg.showLabel) base.showLabel = cfg.showLabel; 65 if (cfg.mainText) base.mainText = cfg.mainText; 66 if (cfg.secondText) base.secondText = cfg.secondText; 67 if (cfg.mainClick) base.mainClick = cfg.mainClick; 68 if (cfg.secondClick) base.secondClick = cfg.secondClick; 69 if (cfg.escPressed) base.escPressed = cfg.escPressed; 70 this.dialog = base; 71 }, 72 async getErrorMessage(err) { 73 switch (err.constructor) { 74 case Error: 75 return err.message; 76 case Response: 77 var text = await err.text(); 78 return `${text} (${err.status})`; 79 default: 80 return err; 81 } 82 }, 83 isSessionError(err) { 84 switch ( 85 err 86 .toString() 87 .replace(/\(\d+\)/g, "") 88 .trim() 89 .toLowerCase() 90 ) { 91 case "session is not exist": 92 case "session has been expired": 93 return true; 94 default: 95 return false; 96 } 97 }, 98 showErrorDialog(msg) { 99 var sessionError = this.isSessionError(msg), 100 dialogContent = sessionError 101 ? "Session has expired, please login again." 102 : msg; 103 104 this.showDialog({ 105 visible: true, 106 title: "Error", 107 content: dialogContent, 108 mainText: "OK", 109 mainClick: () => { 110 this.dialog.visible = false; 111 if (sessionError) { 112 var loginUrl = new Url("login", document.baseURI); 113 loginUrl.query.dst = window.location.href; 114 115 document.cookie = `session-id=; Path=${ 116 new URL(document.baseURI).pathname 117 }; Expires=Thu, 01 Jan 1970 00:00:00 GMT;`; 118 location.href = loginUrl.toString(); 119 } 120 }, 121 }); 122 }, 123 }, 124 };