Created
March 15, 2014 02:06
-
-
Save 11111000000/9560821 to your computer and use it in GitHub Desktop.
Angular.js log decorator (LiveScript). Support for "@ |> log" watches with diff (functions.ls)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: livescript -*- | |
# | |
# Log decorator | |
# author: Peter | |
# | |
do -> | |
module = angular.module \logDecorator, [] | |
module.config [ | |
\$provide | |
($provide) -> | |
$provide.decorator \$log, [ | |
\$delegate | |
($delegate) -> | |
instance = $delegate | |
$delegate <<<< | |
options: | |
watchStyle: 'background-color: #eee; color: #333; font-weight: normal; border: 1px solid gray;' | |
logStyle: 'background-color: #aaa; color: #222; font-weight: normal; border: 1px solid gray;' | |
allowGroups: yes # TODO | |
allowColors: yes # TODO | |
instance: (options = {}) -> | |
instance := { [fn, $delegate[fn] |> decorate ] for fn in <[ log debug warn error ]> } | |
<<<< | |
options: $delegate.options with options | |
watch: watchLogFn # separate, beacause we need callee | |
now: $delegate.now | |
set: $delegate.set | |
now: -> | |
| &1? => @nowOptions[&0] = &1 | |
| typeof! it is \Object => @nowOptions = {} <<<< it | |
| _ => return | |
@ | |
set: -> | |
| &1? => @options[&0] = &1 | |
| typeof! it is \Object => @options <<<< it | |
| _ => return | |
@ | |
watch: watchLogFn | |
$delegate | |
] | |
] | |
decorate = (fn)-> | |
(...args)-> | |
#fn = console.log # TODO fn - type ? | |
setup = {} <<<< @options <<<< (@nowOptions or {}) | |
title = "[ #{setup.from or ''} ] " | |
if setup.allowColors => | |
title = "%c #{title}" | |
args.unshift setup.logStyle | |
args.unshift title | |
## $ \#console .prepend('<div>' + args[0] + angular.toJson(args[2]) + '</div><br>'); | |
## if skewer? | |
## skewer.log.apply skewer.log, args[0] | |
## skewer.log.apply skewer.log, args[1] | |
fn.apply window.console, args | |
return @ | |
# | |
# watchLogFn | |
# this -> this | |
# | |
watchLogFn = (context)-> | |
return if not context.exp # TODO crossbrowser debug | |
setup = {} <<<< @options <<<< (@nowOptions or {}) | |
title = plainTitle = "[ #{setup.from or ''} ] $watch \\#{context.exp}" | |
if setup.allowColors => | |
[ title, css ] = [ "%c #{title}", setup.watchStyle ] | |
else css = '' | |
if setup.allowGroups | |
if setup.expanded => console.group title, setup.watchStyle | |
else console.groupCollapsed title, setup.watchStyle | |
[current, last] = context.fn.arguments | |
args = | |
| [current, last] |> all is-type \Object => [objDiff last, current] | |
| _ => [( last ? 'Undefined'), '->', (current ? 'Undefined')] # shown 'Undefined' text | |
if not setup.allowGroups => args.unshift title | |
@log.apply @, args | |
if setup.allowGroups => console.groupEnd! | |
@nowOptions = {} | |
return @ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment