summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/lang/oo/mixin.js
blob: ec2c060bc02bd666b2618fe528d8121f12e1511b (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//>>built
// wrapped by build app
define("dojox/lang/oo/mixin", ["dijit","dojo","dojox","dojo/require!dojox/lang/oo/Filter,dojox/lang/oo/Decorator"], function(dijit,dojo,dojox){
dojo.provide("dojox.lang.oo.mixin");

dojo.experimental("dojox.lang.oo.mixin");

dojo.require("dojox.lang.oo.Filter");
dojo.require("dojox.lang.oo.Decorator");

(function(){
	var oo = dojox.lang.oo, Filter = oo.Filter, Decorator = oo.Decorator, empty = {},
		defaultFilter = function(name){ return name; },
		defaultDecorator = function(name, newValue, oldValue){ return newValue; },
		defaultMixer = function(target, name, newValue, oldValue){ target[name] = newValue; },
		defaults = {},	// for the internal use in the mixin()
		extraNames = dojo._extraNames, extraLen = extraNames.length,

		applyDecorator = oo.applyDecorator = function(decorator, name, newValue, oldValue){
			//	summary:
			//		applies a decorator unraveling all embedded decorators
			//	decorator: Function:
			//		top-level decorator to apply
			//	name: String:
			//		name of the property
			//	newValue: Object:
			//		new value of the property
			//	oldValue: Object:
			//		old value of the property
			//	returns: Object:
			//		returns the final value of the property
			if(newValue instanceof Decorator){
				var d = newValue.decorator;
				newValue = applyDecorator(decorator, name, newValue.value, oldValue);
				return d(name, newValue, oldValue);
			}
			return decorator(name, newValue, oldValue);
		};

	/*=====
	dojox.lang.oo.__MixinDefaults = function(){
		//	summary:
		//		a dict of default parameters for dojox.lang.oo._mixin
		//	decorator: Function:
		//		a decorator function to be used in absence of other decorators
		//	filter: Function:
		//		a filter function to be used in absence of other filters
		//	mixer: Function:
		//		a mixer function to be used to mix in new properties
		this.decorator = decorator;
		this.filter = filter;
		this.mixer = mixer;
	};
	=====*/

	oo.__mixin = function(target, source, decorator, filter, mixer){
		//	summary:
		//		mixes in two objects processing decorators and filters
		//	target: Object:
		//		target to receive new/updated properties
		//	source: Object:
		//		source of properties
		//	defaults: dojox.lang.oo.__MixinDefaults?:
		//		default functions for various aspects of mixing
		//	returns: Object:
		//		target

		var name, targetName, prop, newValue, oldValue, i;

		// start mixing in properties
		for(name in source){
			prop = source[name];
			if(!(name in empty) || empty[name] !== prop){
				targetName = filter(name, target, source, prop);
				if(targetName && (!(targetName in target) || !(targetName in empty) || empty[targetName] !== prop)){
					// name is accepted
					oldValue = target[targetName];
					newValue = applyDecorator(decorator, targetName, prop, oldValue);
					if(oldValue !== newValue){
						mixer(target, targetName, newValue, oldValue);
					}
				}
			}
		}
		if(extraLen){
			for(i = 0; i < extraLen; ++i){
				name = extraNames[i];
				// repeating the body above
				prop = source[name];
				if(!(name in empty) || empty[name] !== prop){
					targetName = filter(name, target, source, prop);
					if(targetName && (!(targetName in target) || !(targetName in empty) || empty[targetName] !== prop)){
						// name is accepted
						oldValue = target[targetName];
						newValue = applyDecorator(decorator, targetName, prop, oldValue);
						if(oldValue !== newValue){
							mixer(target, targetName, newValue, oldValue);
						}
					}
				}
			}
		}

		return target;	// Object
	};

	oo.mixin = function(target, source){
		// summary:
		//		mixes in two or more objects processing decorators and filters
		//		using defaults as a fallback
		// target: Object:
		//		target to receive new/updated properties
		// source: Object...:
		//		source of properties, more than one source is allowed
		// returns: Object:
		//		target

		var decorator, filter, i = 1, l = arguments.length;
		for(; i < l; ++i){
			source = arguments[i];
			if(source instanceof Filter){
				filter = source.filter;
				source = source.bag;
			}else{
				filter = defaultFilter;
			}
			if(source instanceof Decorator){
				decorator = source.decorator;
				source = source.value;
			}else{
				decorator = defaultDecorator;
			}
			oo.__mixin(target, source, decorator, filter, defaultMixer);
		}
		return target;	// Object
	};
})();

});