1 // ==========================================================================
  2 // Project:   The M-Project - Mobile HTML5 Application Framework
  3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
  4 //            (c) 2011 panacoda GmbH. All rights reserved.
  5 // Creator:   Sebastian
  6 // Date:      04.01.2011
  7 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
  8 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
  9 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
 10 // ==========================================================================
 11 
 12 // Returns a unique identifier
 13 
 14 m_require('core/foundation/object.js');
 15 
 16 M.UniqueId = M.Object.extend({
 17     uuid: function(len, radix) {
 18         // based on Robert Kieffer's randomUUID.js at http://www.broofa.com
 19         var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
 20         var uuid = [];
 21         //len = len ? len : 32; 
 22         radix = radix || chars.length;
 23         var i;
 24 
 25         if (len) {
 26             for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
 27         } else {
 28             // rfc4122, version 4 form
 29             var r;
 30 
 31             // rfc4122 requires these characters
 32             uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
 33             uuid[14] = '4';
 34 
 35             // Fill in random data.  At i==19 set the high bits of clock sequence as
 36             // per rfc4122, sec. 4.1.5
 37             for (i = 0; i < 36; i++) {
 38                 if (!uuid[i]) {
 39                     r = 0 | Math.random() * 16;
 40                     uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
 41                 }
 42             }
 43         }
 44         return uuid.join('');
 45     }
 46 });