1 /** |
|
2 * Factory.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /*global tinymce:true */ |
|
12 |
|
13 /** |
|
14 * This class is a factory for control instances. This enables you |
|
15 * to create instances of controls without having to require the UI controls directly. |
|
16 * |
|
17 * It also allow you to override or add new control types. |
|
18 * |
|
19 * @class tinymce.ui.Factory |
|
20 */ |
|
21 define("tinymce/ui/Factory", [], function() { |
|
22 "use strict"; |
|
23 |
|
24 var types = {}, namespaceInit; |
|
25 |
|
26 return { |
|
27 /** |
|
28 * Adds a new control instance type to the factory. |
|
29 * |
|
30 * @method add |
|
31 * @param {String} type Type name for example "button". |
|
32 * @param {function} typeClass Class type function. |
|
33 */ |
|
34 add: function(type, typeClass) { |
|
35 types[type.toLowerCase()] = typeClass; |
|
36 }, |
|
37 |
|
38 /** |
|
39 * Returns true/false if the specified type exists or not. |
|
40 * |
|
41 * @method has |
|
42 * @param {String} type Type to look for. |
|
43 * @return {Boolean} true/false if the control by name exists. |
|
44 */ |
|
45 has: function(type) { |
|
46 return !!types[type.toLowerCase()]; |
|
47 }, |
|
48 |
|
49 /** |
|
50 * Creates a new control instance based on the settings provided. The instance created will be |
|
51 * based on the specified type property it can also create whole structures of components out of |
|
52 * the specified JSON object. |
|
53 * |
|
54 * @example |
|
55 * tinymce.ui.Factory.create({ |
|
56 * type: 'button', |
|
57 * text: 'Hello world!' |
|
58 * }); |
|
59 * |
|
60 * @method create |
|
61 * @param {Object/String} settings Name/Value object with items used to create the type. |
|
62 * @return {tinymce.ui.Control} Control instance based on the specified type. |
|
63 */ |
|
64 create: function(type, settings) { |
|
65 var ControlType, name, namespace; |
|
66 |
|
67 // Build type lookup |
|
68 if (!namespaceInit) { |
|
69 namespace = tinymce.ui; |
|
70 |
|
71 for (name in namespace) { |
|
72 types[name.toLowerCase()] = namespace[name]; |
|
73 } |
|
74 |
|
75 namespaceInit = true; |
|
76 } |
|
77 |
|
78 // If string is specified then use it as the type |
|
79 if (typeof type == 'string') { |
|
80 settings = settings || {}; |
|
81 settings.type = type; |
|
82 } else { |
|
83 settings = type; |
|
84 type = settings.type; |
|
85 } |
|
86 |
|
87 // Find control type |
|
88 type = type.toLowerCase(); |
|
89 ControlType = types[type]; |
|
90 |
|
91 // #if debug |
|
92 |
|
93 if (!ControlType) { |
|
94 throw new Error("Could not find control by type: " + type); |
|
95 } |
|
96 |
|
97 // #endif |
|
98 |
|
99 ControlType = new ControlType(settings); |
|
100 ControlType.type = type; // Set the type on the instance, this will be used by the Selector engine |
|
101 |
|
102 return ControlType; |
|
103 } |
|
104 }; |
|
105 }); |
|