vanilla-jet 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,522 @@
1
+ function Dipper(options, shared) {
2
+
3
+ let path = require('path');
4
+
5
+ this.options = options;
6
+ this.shared = shared;
7
+ this.metas = [];
8
+ this.site_title = shared.site_name;
9
+ this.page_title = this.site_title;
10
+ this.description = shared.description;
11
+ this.fbAppId = shared.fbAppId;
12
+
13
+ this.site_url = ((this.options.site_url == 'localhost') ?
14
+ 'http://localhost' :
15
+ this.options.site_url) + ':' + this.options.port;
16
+ this.base_dir = path.join(process.cwd(), '/');
17
+ this.base_url = this.site_url + this.options.base_url;
18
+
19
+ // Dirs
20
+ this.dirs = {
21
+
22
+ 'images' : '/public/images/',
23
+ 'scripts' : '/public/scripts/',
24
+ 'styles' : '/public/styles/',
25
+ 'fonts' : '/public/fonts/'
26
+ }
27
+
28
+ // -- Static content
29
+ this.scripts = [];
30
+ this.styles = [];
31
+ this.anims = [];
32
+ this.enqueued_scripts = [];
33
+ this.enqueued_styles = [];
34
+
35
+ // -- Base
36
+ this.registerScript('modernizr', '//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js');
37
+ this.registerScript('respond', '//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js');
38
+ this.registerScript('jquery', '//cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js');
39
+ this.registerScript('underscore', '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.6/underscore-min.js');
40
+ }
41
+
42
+ Dipper.prototype.getPageTitle = function() {
43
+
44
+ var obj = this;
45
+ return obj.site_title;
46
+ }
47
+
48
+ Dipper.prototype.getSiteTitle = function() {
49
+
50
+ var obj = this;
51
+ return obj.page_title;
52
+ }
53
+
54
+ Dipper.prototype.getDescription = function() {
55
+
56
+ var obj = this;
57
+ return obj.description;
58
+ }
59
+
60
+ Dipper.prototype.getFbAppId = function() {
61
+
62
+ var obj = this;
63
+ return obj.fbAppId;
64
+ }
65
+
66
+ Dipper.prototype.addMeta = function(name, content, attribute) {
67
+
68
+ var obj = this;
69
+ attribute = attribute || 'name';
70
+ content = content || '';
71
+ var meta = [];
72
+ meta['name'] = name;
73
+ meta['content'] = content;
74
+ meta['attribute'] = attribute;
75
+ obj.metas[name] = meta;
76
+ }
77
+
78
+ Dipper.prototype.metaTags = function() {
79
+
80
+ var obj = this;
81
+ var _ = require('underscore');
82
+ var stringMeta = '';
83
+
84
+ var keys = Object.keys(obj.metas);
85
+ _.each(keys, function(key) {
86
+
87
+ var name = obj.metas[key]['name'],
88
+ content = obj.metas[key]['content'],
89
+ attribute = obj.metas[key]['attribute'];
90
+
91
+ stringMeta += obj.metas[key]['content'] != '' ?
92
+ '<meta ' + attribute + '=\"' + name + '\" content=\"' + content + '\">\n\t' :
93
+ '<meta ' + attribute + '=\"' + name + '\">\n\t';
94
+ });
95
+ return stringMeta;
96
+ }
97
+
98
+ Dipper.prototype.img = function(filename) {
99
+
100
+ var obj = this,
101
+ dir = this.getDir('images', false);
102
+ ret = this.urlTo(dir + filename);
103
+ return ret;
104
+ }
105
+
106
+ Dipper.prototype.script = function(filename) {
107
+
108
+ var obj = this,
109
+ dir = this.getDir('scripts', false);
110
+
111
+ var filenameParts = filename.split('.'),
112
+ length = filenameParts.length;
113
+ if (length > 0) {
114
+
115
+ let position = length - 1;
116
+ filenameParts[position] = 'min.' + filenameParts[position];
117
+ filename = filenameParts.join('.');
118
+ }
119
+ return this.urlTo(dir + filename);
120
+ }
121
+
122
+ Dipper.prototype.style = function(filename) {
123
+
124
+ var obj = this,
125
+ dir = this.getDir('styles', false),
126
+ ret = this.urlTo(dir + filename);
127
+ return ret;
128
+ }
129
+
130
+ Dipper.prototype.pdf = function(filename) {
131
+
132
+ var obj = this,
133
+ dir = '/assets/pdf/'
134
+ ret = this.urlTo(dir + filename);
135
+ //console.log(ret);
136
+ return ret;
137
+ }
138
+
139
+ Dipper.prototype.getDir = function(dir, full) {
140
+
141
+ var obj = this,
142
+ full = (full == undefined) ? true : full;
143
+
144
+ if ( obj.dirs[dir] != undefined ) {
145
+ return (full == true ? obj.baseDir(obj.dirs[dir]) : obj.dirs[dir]);
146
+ }
147
+ return false;
148
+ }
149
+
150
+ Dipper.prototype.baseDir = function(path) {
151
+
152
+ var obj = this,
153
+ path = path || '',
154
+ ret = obj.base_dir + path;
155
+ return ret;
156
+ }
157
+
158
+ Dipper.prototype.isSecureRequest = function() {
159
+
160
+ var obj = this;
161
+ // Cheking for http or https
162
+ if (/^((http):\/\/)/.test(obj.options.site_url) || /^((localhost))/.test(obj.options.site_url)) {
163
+ return false;
164
+ } else if (/^((https):\/\/)/.test(obj.options.site_url)) {
165
+ return true;
166
+ }
167
+ }
168
+
169
+ Dipper.prototype.urlTo = function(route, protocol) {
170
+
171
+ var obj = this,
172
+ protocol = protocol || undefined,
173
+ url = obj.base_url + route;
174
+ //url = obj.baseUrl(route, protocol);
175
+ return url;
176
+ }
177
+
178
+ Dipper.prototype.registerStyle = function(
179
+ name, url, requires,
180
+ cdn = false, async = false,
181
+ origin = '', integrity = '') {
182
+
183
+ var obj = this;
184
+ obj.styles[name] = {
185
+ 'resource' : url,
186
+ 'requires' : requires,
187
+ 'cdn' : cdn,
188
+ 'async' : async,
189
+ 'origin' : origin,
190
+ 'integrity' : integrity
191
+ };
192
+ }
193
+
194
+ Dipper.prototype.registerScript = function(
195
+ name, url, requires,
196
+ cdn = false, async = false, defer = false,
197
+ origin = '', integrity = '') {
198
+
199
+ var obj = this;
200
+ obj.scripts[name] = {
201
+
202
+ 'resource' : url,
203
+ 'requires' : requires,
204
+ 'cdn' : cdn,
205
+ 'async' : async,
206
+ 'defer': defer,
207
+ 'origin' : origin,
208
+ 'integrity' : integrity
209
+ };
210
+ }
211
+
212
+ Dipper.prototype.enqueueStyle = function(name) {
213
+
214
+ var obj = this,
215
+ _ = require('underscore');
216
+
217
+ if (obj.styles[name] != undefined) {
218
+ if (obj.enqueued_styles[name] == undefined) {
219
+
220
+ var item = obj.styles[name];
221
+ _.each(item.requires, function(dep) {
222
+ obj.enqueueStyle(dep);
223
+ });
224
+ obj.enqueued_styles[name] = name;
225
+ }
226
+ }
227
+ }
228
+
229
+ Dipper.prototype.enqueueScript = function(name) {
230
+
231
+ var obj = this,
232
+ _ = require('underscore');
233
+
234
+ if (obj.scripts[name] != undefined) {
235
+ if (obj.enqueued_scripts[name] == undefined) {
236
+
237
+ var item = obj.scripts[name];
238
+ _.each(item.requires, function(dep) {
239
+ obj.enqueueScript(dep);
240
+ });
241
+ obj.enqueued_scripts[name] = name;
242
+ }
243
+ }
244
+ }
245
+
246
+ Dipper.prototype.dequeueStyle = function(name, dependencies) {
247
+
248
+ var obj = this,
249
+ _ = require('underscore')
250
+ dependencies = (dependencies == undefined) ? false : dependencies;
251
+
252
+ if (obj.styles[name] != undefined) {
253
+ if (obj.enqueued_styles[name] != undefined) {
254
+ var item = obj.styles[name];
255
+ if (dependencies != undefined) {
256
+ _.each(item.require, function(dep) {
257
+ obj.dequeueStyle(dep);
258
+ });
259
+ }
260
+ delete obj.enqueued_styles[name];
261
+ }
262
+ }
263
+ }
264
+
265
+ Dipper.prototype.dequeueScript = function(name, dependencies) {
266
+
267
+ var obj = this,
268
+ _ = require('underscore')
269
+ dependencies = (dependencies == undefined) ? false : dependencies;
270
+
271
+ if (obj.scripts[name] != undefined) {
272
+ if (obj.enqueued_scripts[name] != undefined) {
273
+ var item = obj.scripts[name];
274
+ if (dependencies != undefined) {
275
+ _.each(item.require, function(dep) {
276
+ obj.dequeueScript(dep);
277
+ });
278
+ }
279
+ delete obj.enqueued_scripts[name];
280
+ }
281
+ }
282
+ }
283
+
284
+ Dipper.prototype.includeStyle = function(style) {
285
+
286
+ var obj = this;
287
+ if (obj.styles[style]) {
288
+
289
+ var item = obj.styles[style],
290
+ output = '',
291
+ //type = item['cdn'] ? "" : 'type=\"text/javascript\"',
292
+ resource = item['resource'],
293
+ isAsync = item['async'] ? 'preload' : 'stylesheet',
294
+ origin = item['origin'] != '' ? ' crossorigin=\"' + item['origin'] + '\"' : '',
295
+ integrity = item['integrity'] != '' ? ' integrity=\"' + item['integrity'] + '\"' : '';
296
+
297
+ if (item['async']) {
298
+
299
+ var a = " as=\"style\" onload=\"this.onload=null; this.rel='stylesheet'\"";
300
+ output = '<link rel=\"' + isAsync + '\" type=\"text/css\" href=\"' + resource + '\"' + a + integrity + origin + ">";
301
+ } else {
302
+ output = '<link rel=\"' + isAsync + '\" type=\"text/css\" href=\"' + resource + '\"' + integrity + origin + ">";
303
+ }
304
+ return output + "\n";
305
+ }
306
+ }
307
+
308
+ Dipper.prototype.includeScript = function(script) {
309
+
310
+ var obj = this;
311
+ if (obj.scripts[script]) {
312
+
313
+ var item = obj.scripts[script],
314
+ output = '',
315
+ //type = item['cdn'] ? "" : 'type=\"text/javascript\"',
316
+ resource = item['resource'],
317
+ isAsync = item['async'] ? ' async' : '',
318
+ defer = item['defer'] ? ' defer' : '',
319
+ origin = item['origin'] != '' ? ' crossorigin=\"' + item['origin'] + '\"' : '',
320
+ integrity = item['integrity'] != '' ? ' integrity=\"' + item['integrity'] + '\"' : '';
321
+
322
+ output = '<script src=\"' + resource + '\"' + defer + isAsync + integrity + origin + '></script>';
323
+ //console.log(output);
324
+ return output + "\n";
325
+ }
326
+ }
327
+
328
+ Dipper.prototype.includeStyles = function() {
329
+
330
+ var obj = this,
331
+ _ = require('underscore')
332
+ stylesString = '',
333
+ keys = Object.keys(obj.enqueued_styles);
334
+
335
+ _.each(keys, function(style) {
336
+ stylesString += obj.includeStyle(style);
337
+ });
338
+
339
+ //console.log(stylesString);
340
+ //$site->executeHook('core.includeStyles');
341
+ return stylesString;
342
+ }
343
+
344
+ Dipper.prototype.includeScripts = function () {
345
+
346
+ var obj = this,
347
+ _ = require('underscore')
348
+ scriptsString = '',
349
+ keys = Object.keys(obj.enqueued_scripts);
350
+
351
+ _.each(keys, function(script) {
352
+ scriptsString += obj.includeScript(script);
353
+ });
354
+ return scriptsString;
355
+ }
356
+
357
+ Dipper.prototype.includeManifest = function() {
358
+
359
+ var obj = this,
360
+ url = obj.urlTo('/public/manifest.json'),
361
+ tagString = '<link rel=\"manifest\" href=\"' + url + '\">';
362
+ return tagString;
363
+ }
364
+
365
+ /**
366
+ * Setup Sentry for error reporting on production and qa environments.
367
+ */
368
+ Dipper.prototype.includeSentry = function() {
369
+
370
+ const obj = this;
371
+
372
+ // Falllback report error function on development
373
+ if (obj.shared.environment === 'development') {
374
+ return `
375
+ <script>
376
+ function reportError(message, data = {}) {
377
+ console.log(message, data);
378
+ }
379
+ </script>`;
380
+ }
381
+
382
+ const releaseString = (typeof process.env.SENTRY_RELEASE === 'undefined')
383
+ ? ''
384
+ : `<script> var SENTRY_RELEASE = '${ process.env.SENTRY_RELEASE }'; </script>`;
385
+
386
+ const tagString = `
387
+ <script
388
+ src="https://browser.sentry-cdn.com/${ obj.shared.sentry.bundleVersion }/bundle.tracing.replay.min.js"
389
+ integrity="${ obj.shared.sentry.bundleSha }"
390
+ crossorigin="anonymous"
391
+ ></script>`;
392
+
393
+ const loadString = `
394
+ <script>
395
+ if (window.Sentry) {
396
+ Sentry.init({
397
+ dsn: '${ obj.shared.sentry.dsn_js }',
398
+ environment: '${ obj.shared.environment }',
399
+ integrations: [new Sentry.BrowserTracing(), new Sentry.Replay()],
400
+ sampleRate: ${ obj.shared.sentry.sampleRate },
401
+ tracesSampleRate: ${ obj.shared.sentry.tracesSampleRate }
402
+ });
403
+
404
+ function reportError(message, data = {} ) {
405
+ Sentry.captureException(new Error(message), { extra: data });
406
+ }
407
+ } else {
408
+ function reportError(message, data = {}) {
409
+ console.log(message, data);
410
+ }
411
+ }
412
+ </script>`;
413
+
414
+ return releaseString + tagString + loadString;
415
+ }
416
+
417
+ Dipper.prototype.getStyles = function() {
418
+
419
+ var obj = this;
420
+ return obj.styles;
421
+ }
422
+
423
+ Dipper.prototype.getScripts = function() {
424
+
425
+ var obj = this;
426
+ return obj.scripts;
427
+ }
428
+
429
+ Dipper.prototype.template = function(template, id, templates_dir, params) {
430
+
431
+ var obj = this,
432
+ fs = require('fs'),
433
+ path = require('path'),
434
+ templates_dir = (templates_dir == undefined) ? 'assets/templates/' : templates_dir,
435
+ params = params || [],
436
+ dir = path.join(process.cwd(), templates_dir),
437
+ templateString = '<script type=\"text/template\" id=\"' + id + '\">';
438
+
439
+ fs.exists(dir, function(exists) {
440
+
441
+ if (exists) {
442
+
443
+ var tempTemplate = template + '.html';
444
+ if (fs.statSync(dir).isDirectory()) dir += tempTemplate;
445
+
446
+ fs.readFile(dir, "binary", function(err, file) {
447
+
448
+ if (err) {
449
+
450
+ templateString = '<script type=\"text/template\" id=\"' + id + '\"><pre>Template ' + id + ' not found</pre></script>';
451
+ return templateString;
452
+ }
453
+
454
+ if (file) {
455
+
456
+ //console.log(global.render);
457
+ var t = '/templates/' + tempTemplate;
458
+ var data = {};
459
+ data['app'] = global.dipper;
460
+ global.render.renderString(file, data, function(err, res) {
461
+
462
+ //console.log(templateStr);
463
+
464
+ templateString += '\n';
465
+ templateString += '\t' + res;
466
+ templateString += '\n';
467
+ templateString += '</script>';
468
+ templateString += '\n';
469
+ //console.log(templateString);
470
+ return templateString;
471
+ });
472
+ }
473
+ });
474
+ }
475
+ });
476
+ }
477
+
478
+ // -- Method to get a varable from Shared array
479
+ Dipper.prototype.getSharedVar = function(name) {
480
+
481
+ var obj = this,
482
+ ret = obj.shared[name] || '';
483
+ if (ret != '') { ret = "'" + ret + "'"; }
484
+ return ret;
485
+ }
486
+
487
+ Dipper.prototype.get_google_fonts = function(fonts) {
488
+
489
+ var obj = this,
490
+ _ = require('underscore');
491
+
492
+ if (fonts != undefined) {
493
+
494
+ var parts = [],
495
+ keys = Object.keys(fonts);
496
+ _.each(keys, function(font) {
497
+
498
+ var font_name = encodeURI(font),
499
+ font_weight = fonts[font].join(',');
500
+ parts.push(font_name + ':' + font_weight);
501
+ });
502
+
503
+ var params = parts.join('|'),
504
+ ret = '//fonts.googleapis.com/css?family=' + params;
505
+
506
+ return ret;
507
+ }
508
+ }
509
+
510
+ Dipper.prototype.includeEnvironment = function() {
511
+
512
+ const obj = this;
513
+
514
+ return `
515
+ <script>
516
+ var ENVIRONMENT = '${obj.shared.environment}';
517
+ var API_URL = '${obj.options.api_url}';
518
+ var VERSION = '${obj.shared.version}';
519
+ </script>`;
520
+ }
521
+
522
+ module.exports = Dipper;
@@ -0,0 +1,147 @@
1
+ /**
2
+ Version 3.0
3
+ Created by: nalancer08 <https://github.com/nalancer08>
4
+ **/
5
+
6
+ var url = require('url');
7
+ var querystring = require('querystring');
8
+
9
+ function Request(req, options) {
10
+
11
+ this.params = {
12
+ get: {},
13
+ post: {},
14
+ body: ''
15
+ };
16
+ this.format = 'html';
17
+ this.type = 'get';
18
+ this.controller = 'index';
19
+ this.action = 'index';
20
+ this.id = '';
21
+ this.path = '';
22
+ this.parts = [];
23
+ // Call initialization callback
24
+ this.init(req, options);
25
+ }
26
+
27
+ Request.prototype.init = function(req, options) {
28
+
29
+ var obj = this,
30
+ body = '',
31
+ options = options || {},
32
+ parsed = url.parse(req.url, true),
33
+ server = require('../../index').server;
34
+
35
+ //console.log(obj);
36
+
37
+ // Save callbacks
38
+ obj.onDataReceived = options.onDataReceived || obj.onDataReceived;
39
+ // Save request method (get, post, etc)
40
+ obj.type = req.method.toLowerCase();
41
+ // Parse path parts
42
+ obj.path = parsed.pathname.replace(/\/$/, ''); // This is important
43
+ obj.parts = parsed.pathname.replace(/(^\/|\/$)/, '').split('/');
44
+
45
+ // // Try to guess MVC parameters
46
+ // obj.controller = obj.parts[0] || '';
47
+ // obj.action = obj.parts[1] || '';
48
+ // obj.id = obj.parts[2] || '';
49
+ obj.controller = 'app';
50
+ obj.action = obj.parts[0];
51
+ obj.id = obj.parts[1] || null;
52
+
53
+ // Patch for hanlde default route
54
+ if (obj.action == '' && obj.path == '') {
55
+ if ( server != null && server.getDefaultRoute() != '') {
56
+ obj.path = server.getDefaultRoute();
57
+ }
58
+ }
59
+
60
+ /*console.log("======= Start ========");
61
+ console.log(obj.action);
62
+ console.log(obj.path);
63
+ console.log("======= End ========");*/
64
+
65
+ // Patch for catch if not pass second parameter
66
+ if (obj.id == undefined || obj.id === 'undefined' || obj.id == null) {
67
+ obj.id = null;
68
+ }
69
+
70
+ /*console.log("======= Start ========");
71
+ console.log(obj.id);
72
+ console.log("======= End ========");*/
73
+
74
+ // Get output format (if specified)
75
+ //if (obj.id && obj.id != undefined && obj.id !== 'undefined') {
76
+ var matches = (obj.id == null) ? null : obj.id.match(/\.([a-z0-9]+)$/);
77
+ if (matches) {
78
+ obj.format = matches[1] || 'html';
79
+ obj.id = obj.id.replace(/\.([a-z0-9]+)$/, '');
80
+ }
81
+ //}
82
+
83
+ // Stream request body
84
+ req.on('data', function(chunk) {
85
+ body += chunk.toString();
86
+ });
87
+ // Parse GET and POST params
88
+ obj.params.get = parsed.query;
89
+ req.on('end', function(chunk) {
90
+ obj.params.body = body;
91
+ obj.params.post = querystring.parse(body);
92
+ obj.onDataReceived.call(obj);
93
+ });
94
+ }
95
+
96
+ /**
97
+ * This method try to get a parameter from GET OR POST global parameters
98
+ * @param name: Name of the parameter
99
+ * @param value: Default value if the parameter is missing
100
+ **/
101
+ Request.prototype.param = function(name, value) {
102
+
103
+ var obj = this,
104
+ ret = value || '';
105
+ // Try to retrieve parameter from both, POST and GET objects
106
+ if ( typeof obj.params.post[name] !== 'undefined' ) {
107
+ ret = obj.params.post[name];
108
+ } else if ( typeof obj.params.get[name] !== 'undefined' ) {
109
+ ret = obj.params.get[name];
110
+ }
111
+ return ret;
112
+ }
113
+
114
+ Request.prototype.post = function(name, value) {
115
+
116
+ var obj = this,
117
+ ret = value || '';
118
+ // Try to retrieve parameter from POST object
119
+ if ( typeof obj.params.post[name] !== 'undefined' ) {
120
+ ret = obj.params.post[name];
121
+ }
122
+ return ret;
123
+ }
124
+
125
+ Request.prototype.get = function(name, value) {
126
+
127
+ var obj = this,
128
+ ret = value || '';
129
+ // Try to retrieve parameter from GET object
130
+ if ( typeof obj.params.get[name] !== 'undefined' ) {
131
+ ret = obj.params.get[name];
132
+ }
133
+ return ret;
134
+ }
135
+
136
+ Request.prototype.body = function(name, value) {
137
+
138
+ var obj = this;
139
+ return obj.params.body;
140
+ }
141
+
142
+ Request.prototype.onDataReceived = function() {
143
+
144
+ // Interface
145
+ }
146
+
147
+ module.exports = Request;