/* Xeslint-disable */ ///////////////////////////////// // General utility library // export default { /////////////////////////////// // CLEAN OBJECT // Clear all properties from object without resorting to assigning a new object (o={}) // which can be problematic in some cases (IE bugs, watched data items in forms etc) removeAllPropertiesFromObject: function(o) { for (let variableKey in o) { if (o.hasOwnProperty(variableKey)) { delete o[variableKey]; } } }, /** * Copy a string to clipboard * @param {String} string The string to be copied to clipboard * @return {Boolean} returns a boolean correspondent to the success of the copy operation. * Modified from an example here: https://stackoverflow.com/a/53951634/8939 * Basically a fallback if navigator.clipboard is not available */ copyToClipboard: function(string) { let textarea; let result; if (navigator && navigator.clipboard) { navigator.clipboard.writeText(string); } else { try { textarea = document.createElement("textarea"); textarea.setAttribute("readonly", true); textarea.setAttribute("contenteditable", true); textarea.style.position = "fixed"; // prevent scroll from jumping to the bottom when focus is set. textarea.value = string; document.body.appendChild(textarea); textarea.focus(); textarea.select(); const range = document.createRange(); range.selectNodeContents(textarea); const sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); textarea.setSelectionRange(0, textarea.value.length); result = document.execCommand("copy"); } catch (err) { //console.error(err); result = null; } finally { document.body.removeChild(textarea); } // manual copy fallback using prompt if (!result) { const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0; const copyHotkey = isMac ? "⌘C" : "CTRL+C"; result = prompt(`Press ${copyHotkey}`, string); if (!result) { return false; } } } return true; }, /////////////////////////////// // CLEAN TAG NAME // Clean up a tag with same rules as server // normalizeTag: function(tagName) { //kebab case takes care of all the things we need for tags in one go tagName = window.$gz._.kebabCase(tagName); //No longer than 255 characters tagName = tagName.length > 255 ? tagName.substr(0, 255 - 1) : tagName; return tagName; }, /////////////////////////////// // Quick hash for trivial purposes // not cryptographic // https://stackoverflow.com/a/7616484/8939 // quickHash: function(theString) { let hash = 0, i, chr; if (theString.length === 0) return hash; for (i = 0; i < theString.length; i++) { chr = theString.charCodeAt(i); hash = (hash << 5) - hash + chr; hash |= 0; // Convert to 32bit integer } return hash; }, /////////////////////////////// // CONVERT STRING TO BOOLEAN // https://stackoverflow.com/a/1414175/8939 // stringToBoolean: function(string) { switch (string.toLowerCase().trim()) { case "true": case "yes": case "1": return true; case "false": case "no": case "0": case null: return false; default: return Boolean(string); } }, /////////////////////////////// // CONVERT STRING TO FLOAT // https://stackoverflow.com/a/9409894/8939 // stringToFloat: function(string) { //null or empty then zero if (!string) { return 0; } //A number already then parse and return if (window.$gz._.isNumber(string)) { if (string === NaN) { return 0; } return parseFloat(string); } //Not a string at all? if (!window.$gz._.isString(string)) { return 0; } let ret = parseFloat(string.replace(/[^\d.-]/g, "")); if (ret == NaN) { return 0; } return ret; }, /////////////////////////////// // Splice a string //changes the content of a string by removing a range of // characters and/or adding new characters. // // @param {String} source string // @param {number} start Index at which to start changing the string. // @param {number} delCount An integer indicating the number of old chars to remove. // @param {string} newSubStr The String that is spliced in. // @return {string} A new string with the spliced substring. stringSplice: function(source, start, delCount, newSubStr) { if (source == null || source == "") { return ""; } return ( source.slice(0, start) + newSubStr + source.slice(start + Math.abs(delCount)) ); }, /////////////////////////////// // ICON FOR *ALL* OBJECT TYPES //(used for search results and event log / history) //NOTE: Any object type could appear in event log, they all need to be supported where possible //CoreBizObject add here iconForType: function(aytype) { switch (aytype) { case window.$gz.type.NoType: return "fa-genderless"; case window.$gz.type.Global: return "fa-globe"; case window.$gz.type.User: return "fa-user"; case window.$gz.type.Widget: return "fa-vial"; case window.$gz.type.ServerState: return "fa-door-open"; case window.$gz.type.License: return "fa-ticket-alt"; case window.$gz.type.LogFile: return "fa-glasses"; case window.$gz.type.PickListTemplate: return "fa-pencil-ruler"; case window.$gz.type.ServerJob: return "fa-robot"; case window.$gz.type.AyaNova7Import: return "fa-file-import"; case window.$gz.type.TrialSeeder: return "fa-seedling"; case window.$gz.type.Metrics: return "fa-file-medical-alt"; case window.$gz.type.Translation: return "fa-language"; case window.$gz.type.UserOptions: return "fa-user-cog"; case window.$gz.type.FileAttachment: return "fa-paperclip"; case window.$gz.type.DataListView: return "fa-filter"; case window.$gz.type.FormCustom: return "fa-sliders-h"; default: return null; } }, //https://gist.github.com/colemanw/9c9a12aae16a4bfe2678de86b661d922 iconForMIMEType: function(mimeType) { // List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml var icon_classes = { // Media image: "fa-file-image", audio: "fa-file-audio", video: "fa-file-video", // Documents "application/pdf": "fa-file-pdf", "application/msword": "fa-file-word", "application/vnd.ms-word": "fa-file-word", "application/vnd.oasis.opendocument.text": "fa-file-word", "application/vnd.openxmlformats-officedocument.wordprocessingml": "fa-file-word", "application/vnd.ms-excel": "fa-file-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml": "fa-file-excel", "application/vnd.oasis.opendocument.spreadsheet": "fa-file-excel", "application/vnd.ms-powerpoint": "fa-file-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml": "fa-file-powerpoint", "application/vnd.oasis.opendocument.presentation": "fa-file-powerpoint", "text/plain": "fa-file-alt", "text/html": "fa-file-code", "application/json": "fa-file-code", // Archives "application/gzip": "fa-file-archive", "application/zip": "fa-file-archive" }; for (var key in icon_classes) { if (icon_classes.hasOwnProperty(key)) { if (mimeType.search(key) === 0) { // Found it return icon_classes[key]; } } else { return "fa-file"; } } } //new functions above here };