Skip to content
This repository was archived by the owner on Oct 29, 2018. It is now read-only.

Commit afab288

Browse files
author
Eduardo Ottaviani Aragão
committed
New logger for jails 2.2.x version
1 parent 81f0e06 commit afab288

File tree

1 file changed

+24
-85
lines changed

1 file changed

+24
-85
lines changed

logger/logger.js

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,43 @@
1-
define(['jails'],function( jails ){
1+
define(function(){
22

3-
var root = document;
4-
var Component = jails.Component;
5-
var components = jails.components;
3+
return function( jails ){
64

7-
//1. Html markup not found
8-
function markup( target ){
5+
var base = jails.component;
6+
var root = document.documentElement;
97

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();
229

23-
var m = (target||root).querySelectorAll('[data-component]');
10+
jails.component = function( name, element ){
2411

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;
3513

36-
}
14+
module_not_loaded( name, jails.components );
15+
module_not_found( jails.components );
3716

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;
4919
};
5020

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');
7324
};
7425
}
7526

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);
7930
}
8031

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');
8436
}
85-
}
86-
87-
function style( string ){
8837

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');
9140
}
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+
};
10342

10443
});

0 commit comments

Comments
 (0)