-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjs_lib_util_util.js.html
More file actions
195 lines (168 loc) · 6.42 KB
/
js_lib_util_util.js.html
File metadata and controls
195 lines (168 loc) · 6.42 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: js/lib/util/util.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: js/lib/util/util.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Common utilities to develop classes
* @class Util
*/
function Util(){}
/**
* Create global namespace. This namespace is available how goblal variable on window object.
* If parents of base namespace not exists, it's created.
* @param {String} nameSpaceString full namespace
*/
Util.prototype.createNameSpace = function createNameSpace(nameSpaceString) {
var names = nameSpaceString.split("."),
parent = window,
imax = names.length,
i;
//if any nameSpace level doesn't exist, create it
for (i = 0; i < imax; i++) {
if (!parent[names[i]]) {
parent[names[i]] = {};
}
parent = parent[names[i]];
}
};
/**
* Return only folders from path. This can be relative or absolute path.
* @param {String} directorypath folder string
* @return {String[]} Names of folders
*/
Util.prototype.readOnlyDirectories = function readOnlyDirectories(directorypath) {
var fs = require('fs');
var path = require('path');
var listfiles = fs.readdirSync(directorypath);
var listfolders = [];
listfiles.forEach(function (filename){
var relativepath = path.join(directorypath,filename);
if(fs.statSync(relativepath).isDirectory()){
listfolders.push(filename);
}
});
return listfolders;
};
/**
* Get object from namespace by string
* @param {String} namespace String namespace
* @return {Object} object
*/
Util.prototype.getObjectFromString = function(namespace) {
var x = namespace.split('.');
var auxobj = window;
for(i=0;i<x.length;i++){
auxobj = auxobj[x[i]];
}
return auxobj;
};
/**
* Simulate inheritance how other languages
* @param {Object} ctor class
* @param {Object} superCtor Class to extend
*/
Util.prototype.inherits = function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
/**
* Work how require nodejs function. Get file and create object with content, but only that exported by module.exports or exports variable
* @example <caption>mymodule.js</caption>
* function foo(a,b){ return a + b}
* function bar(a,b){ return a - b}
* module.exports = { publicfunction : foo }
* @example <caption>main.js</caption>
* var util = new Util();
* var mymodule = util.req('mymodule.js');
* mymodule.publicfunction(1,2);
* // return 3
* @param {String} filepath file module path
* @return {Object} module export object
*/
Util.prototype.req = function req(filepath){
var fs = require('fs');
var path = require('path');
var _mod = {};
var __module_path__ = path.dirname(filepath) + "/";
_mod.exports = {};
miclase = fs.readFileSync(filepath,'utf-8');
var _function_require = new Function('module','exports','__module_path__',miclase);
_function_require(_mod,_mod.exports,__module_path__);
return _mod.exports;
}
/**
* Generate unique id with numbers and letters.
* @return {String} UID
*/
Util.prototype.uniqueId = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);return v.toString(16);});
};
/**
* Is similar to req function, but this create node script and append into head document
* @param {'String'} path path script
*/
Util.prototype.include = function include(path) {
var script = document.createElement('script');
script.src = path;
script.type = "text/javascript";
document.head.appendChild(script);
};
/**
* @class Singleton
* @name Singleton
* @function getInstance
*/
/**
* With this function apply singleton pattern. Get library by argument and return object that
* only has a function (getInstance). This function return instance class and if not exists create it.
* @param {Object} Library Module class
* @return {Singleton} Cbject class with getInstance function.
*/
Util.prototype.singleton = function singleton(Library) {
var instance;
function createInstance(){
var object = new Library();
return object;
}
return {getInstance: function(){
if(!instance){
instance = createInstance();
}
return instance;
}};
};</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Backend.html">Backend</a></li><li><a href="CBObject.html">CBObject</a></li><li><a href="CBSection.html">CBSection</a></li><li><a href="ConfigUserManager.html">ConfigUserManager</a></li><li><a href="Controller.html">Controller</a></li><li><a href="StorageManager.html">StorageManager</a></li><li><a href="TextBox.html">TextBox</a></li><li><a href="Translator.html">Translator</a></li><li><a href="UI.html">UI</a></li><li><a href="Util.html">Util</a></li></ul><h3>Namespaces</h3><ul><li><a href="application.html">application</a></li><li><a href="application.backend.html">backend</a></li><li><a href="application.controller.html">controller</a></li><li><a href="application.storagemanager.html">storagemanager</a></li><li><a href="application.ui.html">ui</a></li><li><a href="Cloudbook.html">Cloudbook</a></li><li><a href="Cloudbook.Actions.html">Actions</a></li><li><a href="Cloudbook.Sections.html">Sections</a></li><li><a href="Cloudbook.UI.html">UI</a></li><li><a href="Project.html">Project</a></li><li><a href="Project.Data.html">Data</a></li><li><a href="Project.Info.html">Info</a></li></ul><h3>Global</h3><ul><li><a href="global.html#getInstance">getInstance</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-beta3</a> on Tue Mar 24 2015 12:37:37 GMT+0100 (CET)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>