UNPKG

@atlassian/aui

Version:

Atlassian User Interface Framework

64 lines (54 loc) 1.94 kB
(function (global, factory) { if (typeof define === "function" && define.amd) { define(['module', 'exports', './internal/globalize'], factory); } else if (typeof exports !== "undefined") { factory(module, exports, require('./internal/globalize')); } else { var mod = { exports: {} }; factory(mod, mod.exports, global.globalize); global.uniqueId = mod.exports; } })(this, function (module, exports, _globalize) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _globalize2 = _interopRequireDefault(_globalize); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var uniqueID; var uniqueIDstring; var uniqueIDcounter = 0; /** * Generate a unique ID string, checking the ID is not present in the DOM before * returning. Note uniqueID, uniqueIDstring, uniqueIDcounter = 0; set at top of * file. * * @param {String} prefix String to prepend to ID instead of default AUI prefix. * * @returns {String} */ function generateUniqueId(prefix) { uniqueID = uniqueIDcounter++ + ''; uniqueIDstring = prefix ? prefix + uniqueID : 'aui-uid-' + uniqueID; if (!document.getElementById(uniqueIDstring)) { return uniqueIDstring; } else { uniqueIDstring = uniqueIDstring + '-' + new Date().getTime(); if (!document.getElementById(uniqueIDstring)) { return uniqueIDstring; } else { throw new Error('Timestamped fallback ID "' + uniqueIDstring + '" exists.'); } } } (0, _globalize2.default)('id', generateUniqueId); exports.default = generateUniqueId; module.exports = exports['default']; }); //# sourceMappingURL=unique-id.js.map