UNPKG

ldx-widgets

Version:

widgets

236 lines (228 loc) 7.73 kB
(function() { var React, _, a, div, moment, ref; React = require('react'); moment = require('moment'); _ = require('lodash'); ref = React.DOM, div = ref.div, a = ref.a; module.exports = { /** * Return Age from datetstring * @param {String} datestring * @param {Boolean} return the month default false. */ formatAge: function(datestring, includeMonth) { var age, birthDate, days, months, now, years; now = moment().utc(); birthDate = moment(datestring).utc(); age = ''; months = now.diff(birthDate, 'months'); if (months < 1) { days = now.diff(birthDate, 'days'); age = days + " DO"; } else if (months < 48) { age = months + " MO"; } else { years = now.diff(birthDate, 'years'); age = years + " YO"; if (includeMonth) { months = months - (years * 12); if (months) { age = years + " yrs " + months + " mnth"; } } } return age; }, formatNHS: function(nhs) { var nhs1, nhs2, nhs3; if (nhs != null) { nhs = nhs.replace(/[^0-9]/g, ""); if (nhs.length !== 10) { return nhs; } nhs1 = nhs.substr(0, 3); nhs2 = nhs.substr(3, 3); nhs3 = nhs.substr(6, 4); return nhs1 + "-" + nhs2 + "-" + nhs3; } else { return ""; } }, formatPhoneNumber: function(phone) { if (phone != null) { phone = "" + phone; phone = phone.replace(/[^0-9]/g, ''); if (phone.length === 10) { phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"); } if (phone.length === 7) { phone = phone.replace(/(\d{3})(\d{4})/, "$1-$2"); } return phone; } else { return ""; } }, formatSource: function(options) { var facilityName, orgName, sourceName, systemName; orgName = options.orgName, facilityName = options.facilityName, systemName = options.systemName; sourceName = ''; if (orgName != null) { sourceName += orgName; } if ((facilityName != null) && (facilityName !== orgName && facilityName !== '')) { if (sourceName !== '') { sourceName += ' - '; } sourceName += facilityName; } if (((facilityName != null) || (orgName != null)) && (systemName != null) && systemName !== '') { if (sourceName !== '') { sourceName += ' - '; } sourceName += systemName; } return sourceName; }, formatTimezoneForAPI: function(relevantDate) { var hours, minutes, minutestring, negative; minutes = moment(relevantDate).utcOffset(); if (minutes < 0) { negative = "-"; } else { negative = "+"; } minutes = Math.abs(minutes); hours = Math.floor(minutes / 60); if (hours < 10) { hours = "0" + hours; } minutestring = minutes % 60; if (minutestring < 10) { minutestring = "0" + minutestring; } return "" + negative + hours + ":" + minutestring; }, escapeRegExp: function(string) { return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); }, isValidEmail: function(addr) { return /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(addr); }, bytesToSize: function(bytes, precision) { var gigabyte, kilobyte, megabyte, terabyte; if (bytes == null) { return ''; } kilobyte = 1024; megabyte = kilobyte * 1024; gigabyte = megabyte * 1024; terabyte = gigabyte * 1024; if (bytes >= 0 && bytes < kilobyte) { return bytes + ' B'; } else if (bytes >= kilobyte && bytes < megabyte) { return (bytes / kilobyte).toFixed(precision) + ' KB'; } else if (bytes >= megabyte && bytes < gigabyte) { return (bytes / megabyte).toFixed(precision) + ' MB'; } else if (bytes >= gigabyte && bytes < terabyte) { return (bytes / gigabyte).toFixed(precision) + ' GB'; } else if (bytes >= terabyte) { return (bytes / terabyte).toFixed(precision) + ' TB'; } else { return bytes + ' B'; } }, measureScrollBarWidth: function() { var $tester, scrollBarWidth; $tester = $("<div id='outer' style='overflow: scroll; height: 500px; width: 500px; position: absolute; top: 100px; left: 100px;'><div id='inner' style='position: absolute; height: 100%; width: 100%;'></div><div style='height: 600px; width: 600px;'></div></div>"); $('body').append($tester); scrollBarWidth = $tester.height() - $tester.find('#inner').height(); $tester.remove(); return scrollBarWidth; }, formatFileName: function(name, targetLength) { var charactersFromEnd, endRemove, fileEnd, fileLength, fileStart, removeCount, startRemove; fileLength = name.length; if (fileLength <= targetLength) { return name; } removeCount = (fileLength + 3) - targetLength; if ((targetLength - removeCount) <= 6) { charactersFromEnd = 4; } else { charactersFromEnd = 6; } endRemove = fileLength - charactersFromEnd; startRemove = endRemove - removeCount; fileStart = name.substr(0, startRemove); fileEnd = name.substr(endRemove); return fileStart + "…" + fileEnd; }, parseFileExtension: function(filename) { var filenameSplit, name; filenameSplit = filename.split('.'); return name = filenameSplit.length > 1 ? filenameSplit[filenameSplit.length - 1] : ''; }, makeGuid: function() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r, v; r = Math.random() * 16 | 0; v = c === 'x' ? r : r & 0x3 | 0x8; return v.toString(16); }); }, idsMatch: function(itemA, itemB) { if (!((itemA != null) && (itemB != null))) { return false; } if ((itemA.id != null) && (itemB.id != null)) { return itemA.id === itemB.id; } else if ((itemA.guid != null) && (itemB.guid != null)) { return itemA.guid === itemB.guid; } else { return false; } }, synthesizeMouseEvent: function(target, type, options) { var event, opts; if (options == null) { options = {}; } event = target.ownerDocument.createEvent('MouseEvents'); opts = { type: type, canBubble: false, cancelable: true, view: target.ownerDocument.defaultView, detail: 1, screenX: 0, screenY: 0, clientX: 0, clientY: 0, ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, button: 0, relatedTarget: null }; _.assign(opts, options); event.initMouseEvent(opts.type, opts.canBubble, opts.cancelable, opts.view, opts.detail, opts.screenX, opts.screenY, opts.clientX, opts.clientY, opts.ctrlKey, opts.altKey, opts.shiftKey, opts.metaKey, opts.button, opts.relatedTarget); return target.dispatchEvent(event); }, isNumber: function(input) { return !isNaN(parseFloat(input)) && isFinite(input); }, toFixed: function(value) { return Math.round(value * 100) / 100; }, measureDOMProp: function(el, DOMProp) { var measurer, prop; measurer = document.getElementById('measurer'); measurer.appendChild(el); prop = el[DOMProp]; measurer.removeChild(el); return prop; } }; }).call(this);