1 /**
  2  * Generate a random uuid.
  3  * Written by http://www.broofa.com (robert@broofa.com)
  4  *
  5  * Copyright (c) 2010 Robert Kieffer
  6  * Dual licensed under the MIT and GPL licenses.
  7  * @returns {String}
  8  * @example
  9  *   var uuid = JXG.Util.genUUID();
 10  *   > uuid = '92329D39-6F5C-4520-ABFC-AAB64544E172'
 11  */
 12 
 13 /*global JXG: true, define: true*/
 14 /*jslint nomen: true, plusplus: true, bitwise: true*/
 15 
 16 /* depends:
 17  jxg
 18  */
 19 
 20 define(['jxg'], function (JXG) {
 21 
 22     'use strict';
 23 
 24     // constants
 25     var uuidCharsStr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
 26         uuidChars = uuidCharsStr.split('');
 27 
 28     /**
 29      * General utility routines
 30      * @namespace
 31      */
 32     JXG.Util = JXG.Util || {};
 33 
 34     JXG.Util.genUUID = function (prefix) {
 35         var r, i,
 36             uuid = [],
 37             rnd = 0;
 38 
 39         prefix = prefix || '';
 40 
 41         if (prefix !== '' && prefix.substr(prefix.length - 1) !== '-') {
 42             prefix = prefix + '-';
 43         }
 44 
 45         for (i = 0; i < 36; i++) {
 46             if (i === 8 || i === 13 || i === 18 || i === 23) {
 47                 uuid[i] = '-';
 48             } else if (i === 14) {
 49                 uuid[i] = '4';
 50             } else {
 51                 if (rnd <= 0x02) {
 52                     rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
 53                 }
 54 
 55                 r = rnd & 0xf;
 56                 rnd = rnd >> 4;
 57                 uuid[i] = uuidChars[(i === 19) ? (r & 0x3) | 0x8 : r];
 58             }
 59         }
 60 
 61         return prefix + uuid.join('');
 62     };
 63 
 64     return JXG.Util;
 65 });