|
1 | | -define(['jails'],function( jails ){ |
| 1 | +define(function(){ |
2 | 2 |
|
3 | | - var root = document; |
4 | | - var Component = jails.Component; |
5 | | - var components = jails.components; |
| 3 | + return function( jails ){ |
6 | 4 |
|
7 | | - //1. Html markup not found |
8 | | - function markup( target ){ |
| 5 | + var base = jails.component; |
| 6 | + var root = document.documentElement; |
9 | 7 |
|
10 | | - for( var name in components ){ |
11 | | - name = name.replace(/\//g, '\\/'); |
12 | | - if( !(target||root).querySelector('[data-component*='+name+']' ) ){ |
13 | | - console.warn( |
14 | | - style( 'Component.{0} was not found on html markup.', name ) |
15 | | - ); |
16 | | - } |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - //2. Html markup found but not the module |
21 | | - function module_not_found( target ){ |
| 8 | + log(); |
22 | 9 |
|
23 | | - var m = (target||root).querySelectorAll('[data-component]'); |
| 10 | + jails.component = function( name, element ){ |
24 | 11 |
|
25 | | - for( var c = 0; c < m.length; c++){ |
26 | | - var name = m[c].getAttribute('data-component').replace(/\,/, ' ').replace(/\s{2,}/, ' ').split(/\s/); |
27 | | - |
28 | | - for(var x = 0; x < name.length; x++) |
29 | | - if( !(name[x] in jails.components) ){ |
30 | | - console.warn( |
31 | | - style( 'Component.{0} is referenced on markup, but module is not loaded into Jails yet.', name[x] ) |
32 | | - ); |
33 | | - } |
34 | | - } |
| 12 | + var component; |
35 | 13 |
|
36 | | - } |
| 14 | + module_not_loaded( name, jails.components ); |
| 15 | + module_not_found( jails.components ); |
37 | 16 |
|
38 | | - //3. Watch for emit and listening |
39 | | - function emit_and_listen(){ |
40 | | - |
41 | | - var proto = Component.prototype; |
42 | | - |
43 | | - var base = { |
44 | | - emit :proto.emit, |
45 | | - listen :proto.listen, |
46 | | - get :proto.get, |
47 | | - publish :proto.publish, |
48 | | - subscribe :proto.subscribe |
| 17 | + component = base.apply( this, arguments ); |
| 18 | + return component; |
49 | 19 | }; |
50 | 20 |
|
51 | | - proto.emit = function(ev, args){ |
52 | | - console.log( style( '%cComponent.{0} emits \'{0}:{1}\'', this.name, ev ), 'color:green;', args ); |
53 | | - base.emit.apply( this, arguments ); |
54 | | - } |
55 | | - |
56 | | - proto.listen = function(ev, args){ |
57 | | - var name = this.name, ret; |
58 | | - console.log( style( 'Component.{0} is listening to %c\'{1}\'', name, ev ), 'color:#336699;' ); |
59 | | - ret = base.listen.apply( this, arguments ); |
60 | | - jails.events.on(this.element, ev, function(){ |
61 | | - console.log( style( 'Component.{0} listened to %c\'{1}\'', name, ev ), 'color:green;' ); |
62 | | - }); |
63 | | - return ret; |
64 | | - } |
65 | | - |
66 | | - proto.get = function( target ){ |
67 | | - var _self = this; |
68 | | - return function(method){ |
69 | | - var args = Array.prototype.slice.call(arguments); |
70 | | - args.shift(); |
71 | | - console.log( style( '[{0}] => '+target+'.%c'+method +' ✓', _self.name, target ), 'color:#336699', args ); |
72 | | - return base.get.call(_self, target).apply(_self, arguments); |
| 21 | + function empty( name ){ |
| 22 | + return function( c, el, props ){ |
| 23 | + console.info('%c||\\| %c[ Logger %c] - [%c '+name+' %c] is not loaded on jails.start().', 'color:#369BBF; font-weight:bolder; padding:5px 0;', 'font-weight:bold', 'font-weight:normal', 'font-weight:bold; color:#369BBF', 'font-weight:normal'); |
73 | 24 | }; |
74 | 25 | } |
75 | 26 |
|
76 | | - proto.publish = function(ev, args){ |
77 | | - console.log( style( '[{0}] %cpublished \'{1}\' ✓', this.name || 'jails', ev ), 'color:green;', args ); |
78 | | - base.publish.apply( this, arguments ); |
| 27 | + function module_not_loaded( name, components ){ |
| 28 | + if( !(name in components) ) |
| 29 | + components[name] = empty(name); |
79 | 30 | } |
80 | 31 |
|
81 | | - proto.subscribe = function(ev, args){ |
82 | | - console.log( style( '[{0}] %csubscribed to \'{1}\' ✓', this.name || 'jails', ev ), 'color:green;' ); |
83 | | - base.subscribe.apply( this, arguments ); |
| 32 | + function module_not_found( components ){ |
| 33 | + for(var name in components) |
| 34 | + if( !document.querySelector('[data-component*='+name+']') ) |
| 35 | + console.info('%c||\\| %c[ Logger %c] - [%c '+name+' %c] not found in markup on jails.start().', 'color:#369BBF; font-weight:bolder; padding:5px 0;', 'font-weight:bold', 'font-weight:normal', 'font-weight:bold; color:#369BBF', 'font-weight:normal'); |
84 | 36 | } |
85 | | - } |
86 | | - |
87 | | - function style( string ){ |
88 | 37 |
|
89 | | - for (var i = 1; i < arguments.length; i++){ |
90 | | - string = string.replace( new RegExp('\\{' + (i-1) + '\\}', 'g'), arguments[i] ); |
| 38 | + function log(){ |
| 39 | + console.log('%c||\\| %c[ Logger %c] is ready.', 'color:#369BBF; font-weight:bolder; padding:5px 0;', 'font-weight:bold', 'font-weight:normal'); |
91 | 40 | } |
92 | | - |
93 | | - return '👓 [Jails::logger] 👉 ' + string; |
94 | | - } |
95 | | - |
96 | | - return function( target ){ |
97 | | - console.log( '👓%c[ Welcome to Jails Logger ]👓', 'color:#336699'); |
98 | | - |
99 | | - markup( target ); |
100 | | - module_not_found( target ); |
101 | | - emit_and_listen(); |
102 | | - } |
| 41 | + }; |
103 | 42 |
|
104 | 43 | }); |
0 commit comments