Factory.js
2.52 KB
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
/**
* Factory.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/*global tinymce:true */
/**
* This class is a factory for control instances. This enables you
* to create instances of controls without having to require the UI controls directly.
*
* It also allow you to override or add new control types.
*
* @class tinymce.ui.Factory
*/
define("tinymce/ui/Factory", [], function() {
"use strict";
var types = {}, namespaceInit;
return {
/**
* Adds a new control instance type to the factory.
*
* @method add
* @param {String} type Type name for example "button".
* @param {function} typeClass Class type function.
*/
add: function(type, typeClass) {
types[type.toLowerCase()] = typeClass;
},
/**
* Returns true/false if the specified type exists or not.
*
* @method has
* @param {String} type Type to look for.
* @return {Boolean} true/false if the control by name exists.
*/
has: function(type) {
return !!types[type.toLowerCase()];
},
/**
* Creates a new control instance based on the settings provided. The instance created will be
* based on the specified type property it can also create whole structures of components out of
* the specified JSON object.
*
* @example
* tinymce.ui.Factory.create({
* type: 'button',
* text: 'Hello world!'
* });
*
* @method create
* @param {Object/String} settings Name/Value object with items used to create the type.
* @return {tinymce.ui.Control} Control instance based on the specified type.
*/
create: function(type, settings) {
var ControlType, name, namespace;
// Build type lookup
if (!namespaceInit) {
namespace = tinymce.ui;
for (name in namespace) {
types[name.toLowerCase()] = namespace[name];
}
namespaceInit = true;
}
// If string is specified then use it as the type
if (typeof type == 'string') {
settings = settings || {};
settings.type = type;
} else {
settings = type;
type = settings.type;
}
// Find control type
type = type.toLowerCase();
ControlType = types[type];
// #if debug
if (!ControlType) {
throw new Error("Could not find control by type: " + type);
}
// #endif
ControlType = new ControlType(settings);
ControlType.type = type; // Set the type on the instance, this will be used by the Selector engine
return ControlType;
}
};
});