summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/binding-comet-runtime/src/main/resources/jquery.guid.js
blob: 1c7ad4e2fb22c96a51e6aab77a1ddba3e7d8110d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * jQuery Guid v1.0.0-1
 * Requires jQuery 1.2.6+ (Not tested with earlier versions).
 * Copyright (c) 2010 Aaron E. [jquery at happinessinmycheeks dot com] 
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 *	Usage:
 *		jQuery.Guid.Value() // Returns value of internal Guid. If no guid has been specified, returns a new one (value is then stored internally).
 *		jQuery.Guid.New() // Returns a new Guid and sets it's value internally. Also accepts GUID, Sets it internally.
 *		jQuery.Guid.Empty() // Returns an empty Guid 00000000-0000-0000-0000-000000000000.
 *		jQuery.Guid.IsEmpty() // Returns boolean. True if empty/undefined/blank/null.
 *		jQuery.Guid.IsValid() // Returns boolean. True valid guid, false if not.
 *		jQuery.Guid.Set() // Retrns Guid. Sets Guid to user specified Guid, if invalid, returns an empty guid.
 *
 */

jQuery.extend({
	Guid: {
		Set: function(val) {
			var value;
			if (arguments.length == 1) {
				if (this.IsValid(arguments[0])) {
					value = arguments[0];
				} else {
					value = this.Empty();
				}
			}
			$(this).data("value", value);
			return value;
		},

		Empty: function() {
			return "00000000-0000-0000-0000-000000000000";
		},

		IsEmpty: function(gid) {
			return gid == this.Empty() || typeof (gid) == 'undefined' || gid == null || gid == '';
		},

		IsValid: function(value) {
			rGx = new RegExp("\\b(?:[A-F0-9]{8})(?:-[A-F0-9]{4}){3}-(?:[A-F0-9]{12})\\b");
			return rGx.exec(value) != null;
		},

		New: function() {
			if (arguments.length == 1 && this.IsValid(arguments[0])) {
				$(this).data("value", arguments[0]);
				value = arguments[0];
				return value;
			}

			var res = [], hv;
			var rgx = new RegExp("[2345]");
			for (var i = 0; i < 8; i++) {
				hv = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
				if (rgx.exec(i.toString()) != null) {
					if (i == 3) { hv = "6" + hv.substr(1, 3); }
					res.push("-");
				}
				res.push(hv.toUpperCase());
			}
			value = res.join('');
			$(this).data("value", value);
			return value;
		},

		Value: function() {
			if ($(this).data("value")) {
				return $(this).data("value");
			}
			var val = this.New();
			$(this).data("value", val);
			return val;
		}
	}
})();