|
| 1 | +import { BNavbar } from "bootstrap-vue"; |
| 2 | +import { Multiselect } from "@processmaker/vue-multiselect"; |
| 3 | +import moment from "moment-timezone"; |
| 4 | +import { sanitizeUrl } from "@braintree/sanitize-url"; |
| 5 | +import VueHtml2Canvas from "vue-html2canvas"; |
| 6 | +import Sidebaricon from "./components/Sidebaricon"; |
| 7 | +import SelectStatus from "./components/SelectStatus"; |
| 8 | +import SelectUser from "./components/SelectUser"; |
| 9 | +import SelectUserGroup from "./components/SelectUserGroup"; |
| 10 | +import CategorySelect from "./processes/categories/components/CategorySelect"; |
| 11 | +import ProjectSelect from "./components/shared/ProjectSelect"; |
| 12 | +import SelectFromApi from "./components/SelectFromApi"; |
| 13 | +import Breadcrumbs from "./components/Breadcrumbs"; |
| 14 | +import TimelineItem from "./components/TimelineItem"; |
| 15 | +import Required from "./components/shared/Required"; |
| 16 | +import { FileUpload, FileDownload } from "./processes/screen-builder/components"; |
| 17 | +import RequiredCheckbox from "./processes/screen-builder/components/inspector/RequiredCheckbox"; |
| 18 | +import WelcomeModal from "./Mobile/WelcomeModal"; |
| 19 | +/** **** |
| 20 | + * Global adjustment parameters for moment.js. |
| 21 | + */ |
| 22 | +import __ from "./modules/lang"; |
| 23 | + |
| 24 | +require("bootstrap"); |
| 25 | + |
| 26 | +const { Vue } = window; |
| 27 | + |
| 28 | +Vue.use(VueHtml2Canvas); |
| 29 | + |
| 30 | +if (window.ProcessMaker && window.ProcessMaker.user) { |
| 31 | + moment.tz.setDefault(window.ProcessMaker.user.timezone); |
| 32 | + moment.defaultFormat = window.ProcessMaker.user.datetime_format; |
| 33 | + moment.defaultFormatUtc = window.ProcessMaker.user.datetime_format; |
| 34 | +} |
| 35 | +if (document.documentElement.lang) { |
| 36 | + moment.locale(document.documentElement.lang); |
| 37 | + window.ProcessMaker.user.lang = document.documentElement.lang; |
| 38 | +} |
| 39 | +Vue.prototype.moment = moment; |
| 40 | +// initializing global instance of a moment object |
| 41 | +window.moment = moment; |
| 42 | +/** ***** */ |
| 43 | + |
| 44 | +Vue.prototype.$sanitize = sanitizeUrl; |
| 45 | + |
| 46 | +Vue.component("Multiselect", Multiselect); |
| 47 | +Vue.component("Sidebaricon", Sidebaricon); |
| 48 | +Vue.component("SelectStatus", SelectStatus); |
| 49 | +Vue.component("SelectUser", SelectUser); |
| 50 | +Vue.component("SelectUserGroup", SelectUserGroup); |
| 51 | +Vue.component("CategorySelect", CategorySelect); |
| 52 | +Vue.component("ProjectSelect", ProjectSelect); |
| 53 | +Vue.component("SelectFromApi", SelectFromApi); |
| 54 | +Vue.component("FileUpload", FileUpload); |
| 55 | +Vue.component("FileDownload", FileDownload); |
| 56 | +Vue.component("RequiredCheckbox", RequiredCheckbox); |
| 57 | +Vue.component("Breadcrumbs", Breadcrumbs); |
| 58 | +Vue.component("TimelineItem", TimelineItem); |
| 59 | +Vue.component("Required", Required); |
| 60 | +Vue.component("Welcome", WelcomeModal); |
| 61 | + |
| 62 | +// Event bus ProcessMaker |
| 63 | +window.ProcessMaker.events = new Vue(); |
| 64 | + |
| 65 | +window.ProcessMaker.nodeTypes = []; |
| 66 | +window.ProcessMaker.nodeTypes.get = function (id) { |
| 67 | + return this.find((node) => node.id === id); |
| 68 | +}; |
| 69 | + |
| 70 | +// Assign our navbar component to our global ProcessMaker object |
| 71 | +window.ProcessMaker.navbar = {}; |
| 72 | + |
| 73 | +// Set our own specific alert function at the ProcessMaker global object that could |
| 74 | +// potentially be overwritten by some custom theme support |
| 75 | +window.ProcessMaker.navbar.alerts = []; |
| 76 | +window.ProcessMaker.alert = function (msg, variant, showValue = 5, stayNextScreen = false, showLoader = false, msgLink = "", msgTitle = "") { |
| 77 | + if (showValue === 0) { |
| 78 | + // Just show it indefinitely, no countdown |
| 79 | + showValue = true; |
| 80 | + } |
| 81 | + // amount of items allowed in array |
| 82 | + if (ProcessMaker.navbar.alerts.length > 5) { |
| 83 | + ProcessMaker.navbar.alerts.shift(); |
| 84 | + } |
| 85 | + ProcessMaker.navbar.alerts.push({ |
| 86 | + alertText: msg, |
| 87 | + alertLink: msgLink, |
| 88 | + alertTitle: msgTitle, |
| 89 | + alertShow: showValue, |
| 90 | + alertVariant: String(variant), |
| 91 | + showLoader, |
| 92 | + stayNextScreen, |
| 93 | + timestamp: Date.now(), |
| 94 | + }); |
| 95 | +}; |
| 96 | + |
| 97 | +// Set out own specific confirm modal. |
| 98 | +window.ProcessMaker.confirmModal = function (title, message, variant, callback, size = "md", dataTestClose = "confirm-btn-close", dataTestOk = "confirm-btn-ok") { |
| 99 | + ProcessMaker.navbar.confirmTitle = title || __("Confirm"); |
| 100 | + ProcessMaker.navbar.confirmMessage = message || __("Are you sure you want to delete?"); |
| 101 | + ProcessMaker.navbar.confirmVariant = variant; |
| 102 | + ProcessMaker.navbar.confirmCallback = callback; |
| 103 | + ProcessMaker.navbar.confirmShow = true; |
| 104 | + ProcessMaker.navbar.confirmSize = size; |
| 105 | + ProcessMaker.navbar.confirmDataTestClose = dataTestClose; |
| 106 | + ProcessMaker.navbar.confirmDataTestOk = dataTestOk; |
| 107 | +}; |
| 108 | + |
| 109 | +// Set out own specific message modal. |
| 110 | +window.ProcessMaker.messageModal = function (title, message, variant, callback) { |
| 111 | + ProcessMaker.navbar.messageTitle = title || __("Message"); |
| 112 | + ProcessMaker.navbar.messageMessage = message || __(""); |
| 113 | + ProcessMaker.navbar.messageVariant = variant; |
| 114 | + ProcessMaker.navbar.messageCallback = callback; |
| 115 | + ProcessMaker.navbar.messageShow = true; |
| 116 | +}; |
| 117 | + |
| 118 | +// flags print forms |
| 119 | +window.ProcessMaker.apiClient.requestCount = 0; |
| 120 | +window.ProcessMaker.apiClient.requestCountFlag = false; |
| 121 | + |
| 122 | +window.ProcessMaker.apiClient.interceptors.request.use((request) => { |
| 123 | + // flags print forms |
| 124 | + if (window.ProcessMaker.apiClient.requestCountFlag) { |
| 125 | + window.ProcessMaker.apiClient.requestCount++; |
| 126 | + } |
| 127 | + |
| 128 | + window.ProcessMaker.EventBus.$emit("api-client-loading", request); |
| 129 | + return request; |
| 130 | +}); |
| 131 | + |
| 132 | +window.ProcessMaker.apiClient.interceptors.response.use((response) => { |
| 133 | + // TODO: this could be used to show a default "created/upated/deleted resource" alert |
| 134 | + // response.config.method (PUT, POST, DELETE) |
| 135 | + // response.config.url (extract resource name) |
| 136 | + window.ProcessMaker.EventBus.$emit("api-client-done", response); |
| 137 | + // flags print forms |
| 138 | + if (window.ProcessMaker.apiClient.requestCountFlag && window.ProcessMaker.apiClient.requestCount > 0) { |
| 139 | + window.ProcessMaker.apiClient.requestCount--; |
| 140 | + } |
| 141 | + return response; |
| 142 | +}, (error) => { |
| 143 | + // Set in your .catch to false to not show the alert inside window.ProcessMaker.apiClient |
| 144 | + if (!error?.response?.showAlert) { |
| 145 | + return Promise.reject(error); |
| 146 | + } |
| 147 | + |
| 148 | + if (error.code && error.code === "ERR_CANCELED") { |
| 149 | + return Promise.reject(error); |
| 150 | + } |
| 151 | + window.ProcessMaker.EventBus.$emit("api-client-error", error); |
| 152 | + if (error.response && error.response.status && error.response.status === 401) { |
| 153 | + // stop 401 error consuming endpoints with data-sources |
| 154 | + const { url } = error.config; |
| 155 | + if (url.includes("/data_sources/")) { |
| 156 | + if (url.includes("requests/") || url.includes("/test")) { |
| 157 | + throw error; |
| 158 | + } |
| 159 | + } |
| 160 | + window.location = "/login"; |
| 161 | + } else { |
| 162 | + if (_.has(error, "config.url") && !error.config.url.match("/debug")) { |
| 163 | + window.ProcessMaker.apiClient.post("/debug", { |
| 164 | + name: "Javascript ProcessMaker.apiClient Error", |
| 165 | + message: JSON.stringify({ |
| 166 | + message: error.message, |
| 167 | + code: error.code, |
| 168 | + config: error.config, |
| 169 | + }), |
| 170 | + }); |
| 171 | + } |
| 172 | + return Promise.reject(error); |
| 173 | + } |
| 174 | +}); |
| 175 | + |
| 176 | +// Display any uncaught promise rejections from axios in the Process Maker alert box |
| 177 | +window.addEventListener("unhandledrejection", (event) => { |
| 178 | + const error = event.reason; |
| 179 | + if (error.config && error.config._defaultErrorShown) { |
| 180 | + // Already handeled |
| 181 | + event.preventDefault(); // stops the unhandled rejection error |
| 182 | + } else if (error.response && error.response.data && error.response.data.message) { |
| 183 | + if (!(error.code && error.code === "ECONNABORTED")) { |
| 184 | + window.ProcessMaker.alert(error.response.data.message, "danger"); |
| 185 | + } |
| 186 | + } else if (error.message) { |
| 187 | + if (!(error.code && error.code === "ECONNABORTED")) { |
| 188 | + window.ProcessMaker.alert(error.message, "danger"); |
| 189 | + } |
| 190 | + } |
| 191 | +}); |
0 commit comments