|
| 1 | +warn "`console' has been moved to Opal's stdlib, please `require 'console'` instead." if RUBY_ENGINE_VERSION.to_f >= 0.9 |
| 2 | + |
| 3 | +module Browser |
| 4 | + |
| 5 | +# Manipulate the browser console. |
| 6 | +# |
| 7 | +# @see https://developer.mozilla.org/en-US/docs/Web/API/console |
| 8 | +class Console |
| 9 | + include Native |
| 10 | + |
| 11 | + # Clear the console. |
| 12 | + def clear |
| 13 | + `#@native.clear()` |
| 14 | + end |
| 15 | + |
| 16 | + # Print a stacktrace from the call site. |
| 17 | + def trace |
| 18 | + `#@native.trace()` |
| 19 | + end |
| 20 | + |
| 21 | + # Log the passed objects based on an optional initial format. |
| 22 | + def log(*args) |
| 23 | + `#@native.log.apply(#@native, args)` |
| 24 | + end |
| 25 | + |
| 26 | + # Log the passed objects based on an optional initial format as informational |
| 27 | + # log. |
| 28 | + def info(*args) |
| 29 | + `#@native.info.apply(#@native, args)` |
| 30 | + end |
| 31 | + |
| 32 | + # Log the passed objects based on an optional initial format as warning. |
| 33 | + def warn(*args) |
| 34 | + `#@native.warn.apply(#@native, args)` |
| 35 | + end |
| 36 | + |
| 37 | + # Log the passed objects based on an optional initial format as error. |
| 38 | + def error(*args) |
| 39 | + `#@native.error.apply(#@native, args)` |
| 40 | + end |
| 41 | + |
| 42 | + # Time the given block with the given label. |
| 43 | + def time(label, &block) |
| 44 | + raise ArgumentError, "no block given" unless block |
| 45 | + |
| 46 | + `#@native.time(label)` |
| 47 | + |
| 48 | + begin |
| 49 | + if block.arity == 0 |
| 50 | + instance_exec(&block) |
| 51 | + else |
| 52 | + block.call(self) |
| 53 | + end |
| 54 | + ensure |
| 55 | + `#@native.timeEnd()` |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # Group the given block. |
| 60 | + def group(*args, &block) |
| 61 | + raise ArgumentError, "no block given" unless block |
| 62 | + |
| 63 | + `#@native.group.apply(#@native, args)` |
| 64 | + |
| 65 | + begin |
| 66 | + if block.arity == 0 |
| 67 | + instance_exec(&block) |
| 68 | + else |
| 69 | + block.call(self) |
| 70 | + end |
| 71 | + ensure |
| 72 | + `#@native.groupEnd()` |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # Group the given block but collapse it. |
| 77 | + def group!(*args, &block) |
| 78 | + return unless block_given? |
| 79 | + |
| 80 | + `#@native.groupCollapsed.apply(#@native, args)` |
| 81 | + |
| 82 | + begin |
| 83 | + if block.arity == 0 |
| 84 | + instance_exec(&block) |
| 85 | + else |
| 86 | + block.call(self) |
| 87 | + end |
| 88 | + ensure |
| 89 | + `#@native.groupEnd()` |
| 90 | + end |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +class Window |
| 95 | + # Get the {Console} for this window. |
| 96 | + # |
| 97 | + # @return [Console] |
| 98 | + def console |
| 99 | + Console.new(`#@native.console`) |
| 100 | + end |
| 101 | +end |
| 102 | + |
| 103 | +$console = $window.console |
| 104 | + |
| 105 | +end |
0 commit comments