      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 () {
     35         var r, i,
     36             uuid = [],
     37             rnd = 0;
     38
     39         for (i = 0; i < 36; i++) {
     40             if (i === 8 || i === 13 || i === 18 || i === 23) {
     41                 uuid[i] = '-';
     42             } else if (i === 14) {
     43                 uuid[i] = '4';
     44             } else {
     45                 if (rnd <= 0x02) {
     46                     rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
     47                 }
     48
     49                 r = rnd & 0xf;
     50                 rnd = rnd >> 4;
     51                 uuid[i] = uuidChars[(i === 19) ? (r & 0x3) | 0x8 : r];
     52             }
     53         }
     54
     55         return uuid.join('');
     56     };
     57
     58     return JXG.Util;
     59 });