2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('console-filters', function(Y) {
11 * <p>Provides Plugin.ConsoleFilters plugin class.</p>
13 * <p>This plugin adds the ability to control which Console entries display by filtering on category and source. Two groups of checkboxes are added to the Console footer, one for categories and the other for sources. Only those messages that match a checked category or source are displayed.</p>
15 * @module console-filters
17 * @class ConsoleFilters
20 // Some common strings and functions
21 var getCN = Y.ClassNameManager.getClassName,
25 CATEGORY = 'category',
27 CATEGORY_DOT = 'category.',
28 SOURCE_DOT = 'source.',
31 PARENT_NODE = 'parentNode',
33 DEF_VISIBILITY = 'defaultVisibility',
38 C_BODY = DOT + Y.Console.CHROME_CLASSES.console_bd_class,
39 C_FOOT = DOT + Y.Console.CHROME_CLASSES.console_ft_class,
41 SEL_CHECK = 'input[type=checkbox].',
43 isString = Y.Lang.isString;
45 function ConsoleFilters() {
46 ConsoleFilters.superclass.constructor.apply(this,arguments);
49 Y.mix(ConsoleFilters,{
53 * @property ConsoleFilters.NAME
56 * @default 'consoleFilters'
58 NAME : 'consoleFilters',
61 * The namespace hung off the host object that this plugin will inhabit.
63 * @property ConsoleFilters.NS
71 * Markup template used to create the container for the category filters.
73 * @property ConsoleFilters.CATEGORIES_TEMPLATE
78 '<div class="{categories}"></div>',
81 * Markup template used to create the container for the source filters.
83 * @property ConsoleFilters.SOURCES_TEMPLATE
88 '<div class="{sources}"></div>',
91 * Markup template used to create the category and source filter checkboxes.
93 * @property ConsoleFilters.FILTER_TEMPLATE
98 // IE8 and FF3 don't permit breaking _between_ nowrap elements. IE8
99 // doesn't understand (non spec) wbr tag, nor does it create text nodes
100 // for spaces in innerHTML strings. The thin-space entity suffices to
101 // create a breakable point.
102 '<label class="{filter_label}">'+
103 '<input type="checkbox" value="{filter_name}" '+
104 'class="{filter} {filter_class}"> {filter_name}'+
108 * Classnames used by the templates when creating nodes.
110 * @property ConsoleFilters.CHROME_CLASSES
116 categories : getCN(CONSOLE,FILTERS,'categories'),
117 sources : getCN(CONSOLE,FILTERS,'sources'),
118 category : getCN(CONSOLE,FILTER,CATEGORY),
119 source : getCN(CONSOLE,FILTER,SOURCE),
120 filter : getCN(CONSOLE,FILTER),
121 filter_label : getCN(CONSOLE,FILTER,'label')
126 * Default visibility applied to new categories and sources.
128 * @attribute defaultVisibility
132 defaultVisibility : {
134 validator : Y.Lang.isBoolean
138 * <p>Map of entry categories to their visibility status. Update a
139 * particular category's visibility by setting the subattribute to true
140 * (visible) or false (hidden).</p>
142 * <p>For example, yconsole.filter.set('category.info', false) to hide
143 * log entries with the category/logLevel of 'info'.</p>
145 * <p>Similarly, yconsole.filter.get('category.warn') will return a
146 * boolean indicating whether that category is currently being included
149 * <p>Unlike the YUI instance configuration's logInclude and logExclude
150 * properties, filtered entries are only hidden from the UI, but
151 * can be made visible again.</p>
153 * @attribute category
158 validator : function (v,k) {
159 return this._validateCategory(k,v);
164 * <p>Map of entry sources to their visibility status. Update a
165 * particular sources's visibility by setting the subattribute to true
166 * (visible) or false (hidden).</p>
168 * <p>For example, yconsole.filter.set('sources.slider', false) to hide
169 * log entries originating from Y.Slider.</p>
176 validator : function (v,k) {
177 return this._validateSource(k,v);
182 * Maximum number of entries to store in the message cache. Use this to
183 * limit the memory footprint in environments with heavy log usage.
184 * By default, there is no limit (Number.POSITIVE_INFINITY).
186 * @attribute cacheLimit
188 * @default Number.POSITIVE_INFINITY
191 value : Number.POSITIVE_INFINITY,
192 setter : function (v) {
193 if (Y.Lang.isNumber(v)) {
194 this._cacheLimit = v;
197 return Y.Attribute.INVALID_VALUE;
204 Y.extend(ConsoleFilters, Y.Plugin.Base, {
207 * Collection of all log messages passed through since the plugin's
208 * instantiation. This holds all messages regardless of filter status.
209 * Used as a single source of truth for repopulating the Console body when
210 * filters are changed.
218 _cacheLimit : Number.POSITIVE_INFINITY,
221 * The container node created to house the category filters.
223 * @property _categories
230 * The container node created to house the source filters.
239 * Initialize entries collection and attach listeners to host events and
242 * @method initializer
245 initializer : function () {
248 this.get(HOST).on("entry", this._onEntry, this);
250 this.doAfter("renderUI", this.renderUI);
251 this.doAfter("syncUI", this.syncUI);
252 this.doAfter("bindUI", this.bindUI);
254 this.doAfter("clearConsole", this._afterClearConsole);
256 if (this.get(HOST).get('rendered')) {
262 this.after("cacheLimitChange", this._afterCacheLimitChange);
266 * Removes the plugin UI and unwires events.
271 destructor : function () {
272 //TODO: grab last {consoleLimit} entries and update the console with
273 //them (no filtering)
276 if (this._categories) {
277 this._categories.get(PARENT_NODE).removeChild(this._categories);
280 this._sources.get(PARENT_NODE).removeChild(this._sources);
285 * Adds the category and source filter sections to the Console footer.
290 renderUI : function () {
291 var foot = this.get(HOST).get('contentBox').query(C_FOOT),
296 ConsoleFilters.CATEGORIES_TEMPLATE,
297 ConsoleFilters.CHROME_CLASSES);
299 this._categories = foot.appendChild(Y.Node.create(html));
302 ConsoleFilters.SOURCES_TEMPLATE,
303 ConsoleFilters.CHROME_CLASSES);
305 this._sources = foot.appendChild(Y.Node.create(html));
310 * Binds to checkbox click events and internal attribute change events to
311 * maintain the UI state.
316 bindUI : function () {
317 this._categories.on('click', Y.bind(this._onCategoryCheckboxClick, this));
319 this._sources.on('click', Y.bind(this._onSourceCheckboxClick, this));
321 this.after('categoryChange',this._afterCategoryChange);
322 this.after('sourceChange', this._afterSourceChange);
326 * Updates the UI to be in accordance with the current state of the plugin.
330 syncUI : function () {
331 Y.each(this.get(CATEGORY), function (v, k) {
332 this._uiSetCheckbox(CATEGORY, k, v);
335 Y.each(this.get(SOURCE), function (v, k) {
336 this._uiSetCheckbox(SOURCE, k, v);
339 this.refreshConsole();
343 * Ensures a filter is set up for any new categories or sources and
344 * collects the messages in _entries. If the message is stamped with a
345 * category or source that is currently being filtered out, the message
346 * will not pass to the Console's print buffer.
349 * @param e {Event} the custom event object
352 _onEntry : function (e) {
353 this._entries.push(e.message);
355 var cat = CATEGORY_DOT + e.message.category,
356 src = SOURCE_DOT + e.message.source,
357 cat_filter = this.get(cat),
358 src_filter = this.get(src),
359 overLimit = this._entries.length - this._cacheLimit,
363 this._entries.splice(0, overLimit);
366 if (cat_filter === undefined) {
367 visible = this.get(DEF_VISIBILITY);
368 this.set(cat, visible);
369 cat_filter = visible;
372 if (src_filter === undefined) {
373 visible = this.get(DEF_VISIBILITY);
374 this.set(src, visible);
375 src_filter = visible;
378 if (!cat_filter || !src_filter) {
384 * Flushes the cached entries after a call to the Console's clearConsole().
386 * @method _afterClearConsole
389 _afterClearConsole : function () {
394 * Triggers the Console to update if a known category filter
395 * changes value (e.g. visible => hidden). Updates the appropriate
396 * checkbox's checked state if necessary.
398 * @method _afterCategoryChange
399 * @param e {Event} the attribute change event object
402 _afterCategoryChange : function (e) {
403 var cat = e.subAttrName.replace(/category\./, EMPTY),
407 // Don't update the console for new categories
408 if (!cat || before[cat] !== undefined) {
409 this.refreshConsole();
411 this._filterBuffer();
414 if (cat && !e.fromUI) {
415 this._uiSetCheckbox(CATEGORY, cat, after[cat]);
420 * Triggers the Console to update if a known source filter
421 * changes value (e.g. visible => hidden). Updates the appropriate
422 * checkbox's checked state if necessary.
424 * @method _afterSourceChange
425 * @param e {Event} the attribute change event object
428 _afterSourceChange : function (e) {
429 var src = e.subAttrName.replace(/source\./, EMPTY),
433 // Don't update the console for new sources
434 if (!src || before[src] !== undefined) {
435 this.refreshConsole();
437 this._filterBuffer();
440 if (src && !e.fromUI) {
441 this._uiSetCheckbox(SOURCE, src, after[src]);
446 * Flushes the Console's print buffer of any entries that have a category
447 * or source that is currently being excluded.
449 * @method _filterBuffer
452 _filterBuffer : function () {
453 var cats = this.get(CATEGORY),
454 srcs = this.get(SOURCE),
455 buffer = this.get(HOST).buffer,
459 for (i = buffer.length - 1; i >= 0; --i) {
460 if (!cats[buffer[i].category] || !srcs[buffer[i].source]) {
463 buffer.splice(i,(start - i));
468 buffer.splice(0,start + 1);
473 * Trims the cache of entries to the appropriate new length.
475 * @method _afterCacheLimitChange
476 * @param e {Event} the attribute change event object
479 _afterCacheLimitChange : function (e) {
480 if (isFinite(e.newVal)) {
481 var delta = this._entries.length - e.newVal;
484 this._entries.splice(0,delta);
490 * Repopulates the Console with entries appropriate to the current filter
493 * @method refreshConsole
495 refreshConsole : function () {
496 var entries = this._entries,
497 host = this.get(HOST),
498 body = host.get('contentBox').query(C_BODY),
499 remaining = host.get('consoleLimit'),
500 cats = this.get(CATEGORY),
501 srcs = this.get(SOURCE),
506 host._cancelPrintLoop();
508 // Evaluate all entries from latest to oldest
509 for (i = entries.length - 1; i >= 0 && remaining >= 0; --i) {
511 if (cats[e.category] && srcs[e.source]) {
517 body.set('innerHTML',EMPTY);
518 host.buffer = buffer;
524 * Updates the checked property of a filter checkbox of the specified type.
525 * If no checkbox is found for the input params, one is created.
527 * @method _uiSetCheckbox
528 * @param type {String} 'category' or 'source'
529 * @param item {String} the name of the filter (e.g. 'info', 'event')
530 * @param checked {Boolean} value to set the checkbox's checked property
533 _uiSetCheckbox : function (type, item, checked) {
535 var container = type === CATEGORY ?
538 sel = SEL_CHECK + getCN(CONSOLE,FILTER,item),
539 checkbox = container.query(sel),
543 host = this.get(HOST);
545 this._createCheckbox(container, item);
547 checkbox = container.query(sel);
549 host._uiSetHeight(host.get('height'));
552 checkbox.set(CHECKED, checked);
557 * Passes checkbox clicks on to the category attribute.
559 * @method _onCategoryCheckboxClick
560 * @param e {Event} the DOM event
563 _onCategoryCheckboxClick : function (e) {
564 var t = e.target, cat;
566 if (t.hasClass(ConsoleFilters.CHROME_CLASSES.filter)) {
567 cat = t.get('value');
568 if (cat && cat in this.get(CATEGORY)) {
569 this.set(CATEGORY_DOT + cat, t.get(CHECKED), { fromUI: true });
575 * Passes checkbox clicks on to the source attribute.
577 * @method _onSourceCheckboxClick
578 * @param e {Event} the DOM event
581 _onSourceCheckboxClick : function (e) {
582 var t = e.target, src;
584 if (t.hasClass(ConsoleFilters.CHROME_CLASSES.filter)) {
585 src = t.get('value');
586 if (src && src in this.get(SOURCE)) {
587 this.set(SOURCE_DOT + src, t.get(CHECKED), { fromUI: true });
593 * Hides any number of categories from the UI. Convenience method for
594 * myConsole.filter.set('category.foo', false); set('category.bar', false);
597 * @method hideCategory
598 * @param cat* {String} 1..n categories to filter out of the UI
600 hideCategory : function (cat, multiple) {
601 if (isString(multiple)) {
602 Y.Array.each(arguments, arguments.callee, this);
604 this.set(CATEGORY_DOT + cat, false);
609 * Shows any number of categories in the UI. Convenience method for
610 * myConsole.filter.set('category.foo', true); set('category.bar', true);
613 * @method showCategory
614 * @param cat* {String} 1..n categories to allow to display in the UI
616 showCategory : function (cat, multiple) {
617 if (isString(multiple)) {
618 Y.Array.each(arguments, arguments.callee, this);
620 this.set(CATEGORY_DOT + cat, true);
625 * Hides any number of sources from the UI. Convenience method for
626 * myConsole.filter.set('source.foo', false); set('source.bar', false);
630 * @param src* {String} 1..n sources to filter out of the UI
632 hideSource : function (src, multiple) {
633 if (isString(multiple)) {
634 Y.Array.each(arguments, arguments.callee, this);
636 this.set(SOURCE_DOT + src, false);
641 * Shows any number of sources in the UI. Convenience method for
642 * myConsole.filter.set('source.foo', true); set('source.bar', true);
646 * @param src* {String} 1..n sources to allow to display in the UI
648 showSource : function (src, multiple) {
649 if (isString(multiple)) {
650 Y.Array.each(arguments, arguments.callee, this);
652 this.set(SOURCE_DOT + src, true);
657 * Creates a checkbox and label from the ConsoleFilters.FILTER_TEMPLATE for
658 * the provided type and name. The checkbox and label are appended to the
659 * container node passes as the first arg.
661 * @method _createCheckbox
662 * @param container {Node} the parentNode of the new checkbox and label
663 * @param name {String} the identifier of the filter
666 _createCheckbox : function (container, name) {
667 var info = Y.merge(ConsoleFilters.CHROME_CLASSES, {
669 filter_class : getCN(CONSOLE, FILTER, name)
671 node = Y.Node.create(
672 Y.substitute(ConsoleFilters.FILTER_TEMPLATE, info));
674 container.appendChild(node);
678 * Validates category updates are objects and the subattribute is not too
681 * @method _validateCategory
682 * @param cat {String} the new category:visibility map
683 * @param v {String} the subattribute path updated
687 _validateCategory : function (cat, v) {
688 return Y.Lang.isObject(v,true) && cat.split(/\./).length < 3;
692 * Validates source updates are objects and the subattribute is not too
695 * @method _validateSource
696 * @param cat {String} the new source:visibility map
697 * @param v {String} the subattribute path updated
701 _validateSource : function (src, v) {
702 return Y.Lang.isObject(v,true) && src.split(/\./).length < 3;
707 Y.namespace('Plugin').ConsoleFilters = ConsoleFilters;
710 }, '3.0.0' ,{requires:['console','plugin']});