当前位置: 代码迷 >> JavaScript >> javascript logger blackbird jquery 改建
  详细解决方案

javascript logger blackbird jquery 改建

热度:408   发布时间:2012-12-05 14:13:29.0
javascript logger blackbird jquery 改造

var logger=BizPlant.namespace("logger");
logger.config={
??? pattern:"%d{yyyy-MM-dd HH:mm:ss,SSS} %p [%t] %c{1}.%M()| line:%L | %m%n",
??? level:"info"
};
$(document).ready(function(){
??? var enabled=true;
??? var level={
??????? debug: true,
??????? info: true,
??????? warn: true,
??????? error: true,
??????? profile: true
??? };
??? var COOKIE_KEY="BizPlantLogger";
??? var classes = {};
??? var profiler = {};
??? var spans = [];
??? var cache = [];
??? var IDs = {
??????? blackbird: 'blackbird',
??????? checkbox: 'bbVis',
??????? filters: 'bbFilters',
??????? controls: 'bbControls',
??????? size: 'bbSize'
??? }
??? var state=(function(){
??????? var bizPlantLoggerValue=$.cookie(COOKIE_KEY);
??????? if(bizPlantLoggerValue!=null && bizPlantLoggerValue!=""){
??????????? return JSON.parse(bizPlantLoggerValue);
??????? }else{
??????????? return {
??????????????? pos:1,
??????????????? size:0,
??????????????? load:true
??????????? };
??????? }
??? })();
???
??? var recordState=function(){
??????? $.cookie(COOKIE_KEY,JSON.stringify(state));
??????? var newClass = [];
??????? for (var word in classes ) {
??????????? newClass.push( classes[ word ] );
??????? }
??????? $("#"+IDs.blackbird).removeClass().addClass(newClass.join( ' ' ));
??? };
??? var? reposition=function( position ) {
??????? if ( position === undefined || position == null ) {
??????????? position = ( state && state.pos === null ) ? 1 : ( state.pos + 1 ) % 4; //set to initial position ('topRight') or move to next position
??????? }
??? ???
??????? switch ( position ) {
??????????? case 0:
??????????????? classes[ 0 ] = 'bbTopLeft';
??????????????? break;
??????????? case 1:
??????????????? classes[ 0 ] = 'bbTopRight';
??????????????? break;
??????????? case 2:
??????????????? classes[ 0 ] = 'bbBottomLeft';
??????????????? break;
??????????? case 3:
??????????????? classes[ 0 ] = 'bbBottomRight';
??????????????? break;
??????? }
??????? state.pos = position;
??????? recordState();
??? }
??? var? resize=function( size ) {
??????? if ( size === undefined || size === null ) {
??????????? size = ( state && state.size == null ) ? 0 : ( state.size + 1 ) % 2;
??????? }
??????? classes[ 1 ] = ( size === 0 ) ? 'bbSmall' : 'bbLarge'
??????? var title=( size === 1 ) ? 'small' : 'large';
??????? $( "#"+IDs.size).attr("title",title).removeClass().addClass(title);
??????? state.size = size;
??????? recordState();
??? };
???
??? for ( type in level ) {
??????? spans.push( [ '<span class="', type, '" type="', type, '"></span>'].join( '' ) );
??? }
??? var inner = [
??? '<div class="header">',
??? '<div class="left">',
??? '<div id="', IDs.filters, '" class="filters" title="click to filter by message type">', spans.join( '' ), '</div>',
??? '</div>',
??? '<div class="right">',
??? '<div id="', IDs.controls, '" class="controls">',
??? '<span id="', IDs.size ,'" title="contract" op="resize"></span>',
??? '<span class="clear" title="clear" op="clear"></span>',
??? '<span class="close" title="close" op="close"></span>',
??? '</div>',
??? '</div>',
??? '</div>',
??? '<div class="main">',
??? '<div class="left"></div><div class="mainBody">',
??? '<ol>', cache.join( '' ), '</ol>',
??? '</div><div class="right"></div>',
??? '</div>',
??? '<div class="footer">',
??? '<div class="left"><label for="', IDs.checkbox, '"><input type="checkbox" id="', IDs.checkbox, '" />Visible on page load</label></div>',
??? '<div class="right"></div>',
??? '</div>'
??? ].join( '' );

??? $("body").append("<div id='"+IDs.blackbird+"'class='bbSmall bbTopRight' >"+inner+"</div>");
??? if(state.load){
??????? $("#"+IDs.checkbox).attr("checked","checked");
??? }else{
??????? $("#"+IDs.blackbird).hide();
??? }
??? resize( state.size );
??? reposition( state.pos );

??? $("#"+IDs.checkbox).bind("click",function(evt){
??????? var el = ( evt.target ) ? evt.target : evt.srcElement;
??????? state.load = $(this).is(":checked");
??????? recordState();
??? });
??? $("#"+IDs.filters).bind("click",function(evt){
??????? var span = ( evt.target ) ? evt.target : evt.srcElement;
??????? if ( $(span).is('span')) {
??????????? var type = $(span).attr("type");
??????????? //console.log(type+"控制按钮被点击!");
??????????? if ( evt.altKey ) {
??????????????? //console.log("ALT键 被按住");
??????????????? var active = 0;
??????????????? for ( var entry in level ) {
??????????????????? if ( level[ entry ] ) active++;
??????????????? }
??????????????? var oneActiveFilter = ( active == 1 && level[ type ] );
??????????????? $("#"+IDs.filters).find("span").each(function(){
??????????????????? var spanType = $(this).attr("type");
??????????????????? if( oneActiveFilter || ( spanType == type )){
??????????????????????? $(this).removeClass(spanType + 'Disabled ');
??????????????????????? $(this).addClass(spanType);
??????????????????? }else{
??????????????????????? $(this).removeClass(spanType);
??????????????????????? $(this).addClass(spanType + 'Disabled ');
??????????????????? }
??????????????????? level[ spanType ] = oneActiveFilter || ( spanType == type );
??????????????? });
??????????? }else {
??????????????? //window.console.log("普通点击");??
??????????????? if(!level[ type ]){
??????????????????? $(span).removeClass(type + 'Disabled ');
??????????????????? $(span).addClass(type);
??????????????? }else{
??????????????????? $(span).removeClass(type);
??????????????????? $(span).addClass(type + 'Disabled ');
??????????????? }
??????????????? level[ type ] = ! level[ type ];
??????????? }

??????????? //build outputList's class from messageTypes object
??????????? var disabledTypes = [];
??????????? for ( type in level ) {
??????????????? if ( ! level[ type ] ) disabledTypes.push( type );
??????????? }
???????????
??????????? disabledTypes.push( '' );
??????????? $("#"+IDs.blackbird).find("ol:eq(0)").removeClass().addClass(disabledTypes.join( 'Hidden ' ));
??????? }
??? });
??? $("#"+IDs.controls).bind("click",function(evt){
??????? var el = ( evt.target ) ? evt.target : evt.srcElement;
??????? if ( $(el).is("span")) {
??????????? switch ( $(el).attr("op") ) {
??????????????? case 'resize':
??????????????????? resize();
??????????????????? break;
??????????????? case 'clear':
??????????????????? $("#"+IDs.blackbird).find("ol:eq(0)").html("");
??????????????????? break;
??????????????? case 'close':
??????????????????? $("#"+IDs.blackbird).hide();
??????????????????? break;
??????????? }
??????? }
??? });
??? var addMessage=function(type,msg,func){
??????? var message = ( msg.constructor == Array ) ? msg.join( '' ) : msg;
???????
??????? if(!enabled){
??????????? return ;
??????? }
??????? if(logger.config.level=="error" && type!="error"){
??????????? return;
??????? }
??????? if(logger.config.level=="warn" && !(type=="error" || type=="warn")){
??????????? return;
??????? }
??????? if(logger.config.level=="debug" && !(type=="error" || type=="warn" || type=="debug")){
??????????? return;
??????? }
??????? $("#"+IDs.blackbird).find("ol:eq(0)").append("<li class='"+type+"'><span class='icon'></span>"+message+"</li>");
??? };
??? logger.info=function(msg,func){
??????? addMessage("info",msg,func);
??? };
??? logger.debug=function(msg,func){
??????? addMessage("debug",msg,func);
??? };
??? logger.warn=function(msg,func){
??????? addMessage("warn",msg,func);
??? };
??? logger.error=function(msg,func){
??????? addMessage("error",msg,func);
??? };
??? logger.profile=function(label){
??????? var currentTime = new Date(); //record the current time when profile() is executed
??????? if ( label == undefined || label == '' ) {
??????????? addMessage( 'error', '<b>ERROR:</b> Please specify a label for your profile statement' );
??????? }
??????? else if ( profiler[ label ] ) {
??????????? addMessage( 'profile', [ label, ': ', currentTime - profiler[ label ],??? 'ms' ].join( '' ) );
??????????? delete profiler[ label ];
??????? }
??????? else {
??????????? profiler[ label ] = currentTime;
??????????? addMessage( 'profile', label );
??????? }
??????? return currentTime;
??? };
??? logger.move=function(){
??????? reposition();
??? };
??? logger.toggle=function(){
??????? if($("#"+IDs.blackbird).is(":hidden")){
??????????? $("#"+IDs.blackbird).show();
??????? }else{
??????????? $("#"+IDs.blackbird).hide();
??????? }
??? };
??? logger.config.setEnabled=function(enable){
??????? enabled=enable;
??????? if($("#"+IDs.blackbird).is(":hidden") && enable==true){
??????????? $("#"+IDs.blackbird).show();
??????? }
??????? if(!$("#"+IDs.blackbird).is(":hidden") && enable==false){
??????????? $("#"+IDs.blackbird).hide();
??????? }
??? };
??? logger.config.getEnabled=function(){
??????? return enabled;
??? };
??? $(document).bind('keydown.Shift_f1',function (evt){
??????? logger.toggle();
??????? return false;
??? });
??? $(document).bind('keydown.Shift_f2',function (evt){
??????? logger.move();
??????? return false;
??? });
});

  相关解决方案