2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
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.',
32 DEF_VISIBILITY = 'defaultVisibility',
37 C_BODY = DOT + Y.Console.CHROME_CLASSES.console_bd_class,
38 C_FOOT = DOT + Y.Console.CHROME_CLASSES.console_ft_class,
40 SEL_CHECK = 'input[type=checkbox].',
42 isString = Y.Lang.isString;
44 function ConsoleFilters() {
45 ConsoleFilters.superclass.constructor.apply(this,arguments);
48 Y.namespace('Plugin').ConsoleFilters = Y.extend(ConsoleFilters, Y.Plugin.Base,
50 // Y.Plugin.ConsoleFilters prototype
53 * Collection of all log messages passed through since the plugin's
54 * instantiation. This holds all messages regardless of filter status.
55 * Used as a single source of truth for repopulating the Console body when
56 * filters are changed.
65 * Maximum number of entries to store in the message cache.
67 * @property _cacheLimit
72 _cacheLimit : Number.POSITIVE_INFINITY,
75 * The container node created to house the category filters.
77 * @property _categories
84 * The container node created to house the source filters.
93 * Initialize entries collection and attach listeners to host events and
99 initializer : function () {
102 this.get(HOST).on("entry", this._onEntry, this);
104 this.doAfter("renderUI", this.renderUI);
105 this.doAfter("syncUI", this.syncUI);
106 this.doAfter("bindUI", this.bindUI);
108 this.doAfter("clearConsole", this._afterClearConsole);
110 if (this.get(HOST).get('rendered')) {
116 this.after("cacheLimitChange", this._afterCacheLimitChange);
120 * Removes the plugin UI and unwires events.
125 destructor : function () {
126 //TODO: grab last {consoleLimit} entries and update the console with
127 //them (no filtering)
130 if (this._categories) {
131 this._categories.remove();
134 this._sources.remove();
139 * Adds the category and source filter sections to the Console footer.
144 renderUI : function () {
145 var foot = this.get(HOST).get('contentBox').one(C_FOOT),
150 ConsoleFilters.CATEGORIES_TEMPLATE,
151 ConsoleFilters.CHROME_CLASSES);
153 this._categories = foot.appendChild(Y.Node.create(html));
156 ConsoleFilters.SOURCES_TEMPLATE,
157 ConsoleFilters.CHROME_CLASSES);
159 this._sources = foot.appendChild(Y.Node.create(html));
164 * Binds to checkbox click events and internal attribute change events to
165 * maintain the UI state.
170 bindUI : function () {
171 this._categories.on('click', Y.bind(this._onCategoryCheckboxClick, this));
173 this._sources.on('click', Y.bind(this._onSourceCheckboxClick, this));
175 this.after('categoryChange',this._afterCategoryChange);
176 this.after('sourceChange', this._afterSourceChange);
180 * Updates the UI to be in accordance with the current state of the plugin.
184 syncUI : function () {
185 Y.each(this.get(CATEGORY), function (v, k) {
186 this._uiSetCheckbox(CATEGORY, k, v);
189 Y.each(this.get(SOURCE), function (v, k) {
190 this._uiSetCheckbox(SOURCE, k, v);
193 this.refreshConsole();
197 * Ensures a filter is set up for any new categories or sources and
198 * collects the messages in _entries. If the message is stamped with a
199 * category or source that is currently being filtered out, the message
200 * will not pass to the Console's print buffer.
203 * @param e {Event} the custom event object
206 _onEntry : function (e) {
207 this._entries.push(e.message);
209 var cat = CATEGORY_DOT + e.message.category,
210 src = SOURCE_DOT + e.message.source,
211 cat_filter = this.get(cat),
212 src_filter = this.get(src),
213 overLimit = this._entries.length - this._cacheLimit,
217 this._entries.splice(0, overLimit);
220 if (cat_filter === undefined) {
221 visible = this.get(DEF_VISIBILITY);
222 this.set(cat, visible);
223 cat_filter = visible;
226 if (src_filter === undefined) {
227 visible = this.get(DEF_VISIBILITY);
228 this.set(src, visible);
229 src_filter = visible;
232 if (!cat_filter || !src_filter) {
238 * Flushes the cached entries after a call to the Console's clearConsole().
240 * @method _afterClearConsole
243 _afterClearConsole : function () {
248 * Triggers the Console to update if a known category filter
249 * changes value (e.g. visible => hidden). Updates the appropriate
250 * checkbox's checked state if necessary.
252 * @method _afterCategoryChange
253 * @param e {Event} the attribute change event object
256 _afterCategoryChange : function (e) {
257 var cat = e.subAttrName.replace(/category\./, EMPTY),
261 // Don't update the console for new categories
262 if (!cat || before[cat] !== undefined) {
263 this.refreshConsole();
265 this._filterBuffer();
268 if (cat && !e.fromUI) {
269 this._uiSetCheckbox(CATEGORY, cat, after[cat]);
274 * Triggers the Console to update if a known source filter
275 * changes value (e.g. visible => hidden). Updates the appropriate
276 * checkbox's checked state if necessary.
278 * @method _afterSourceChange
279 * @param e {Event} the attribute change event object
282 _afterSourceChange : function (e) {
283 var src = e.subAttrName.replace(/source\./, EMPTY),
287 // Don't update the console for new sources
288 if (!src || before[src] !== undefined) {
289 this.refreshConsole();
291 this._filterBuffer();
294 if (src && !e.fromUI) {
295 this._uiSetCheckbox(SOURCE, src, after[src]);
300 * Flushes the Console's print buffer of any entries that have a category
301 * or source that is currently being excluded.
303 * @method _filterBuffer
306 _filterBuffer : function () {
307 var cats = this.get(CATEGORY),
308 srcs = this.get(SOURCE),
309 buffer = this.get(HOST).buffer,
313 for (i = buffer.length - 1; i >= 0; --i) {
314 if (!cats[buffer[i].category] || !srcs[buffer[i].source]) {
317 buffer.splice(i,(start - i));
322 buffer.splice(0,start + 1);
327 * Trims the cache of entries to the appropriate new length.
329 * @method _afterCacheLimitChange
330 * @param e {Event} the attribute change event object
333 _afterCacheLimitChange : function (e) {
334 if (isFinite(e.newVal)) {
335 var delta = this._entries.length - e.newVal;
338 this._entries.splice(0,delta);
344 * Repopulates the Console with entries appropriate to the current filter
347 * @method refreshConsole
349 refreshConsole : function () {
350 var entries = this._entries,
351 host = this.get(HOST),
352 body = host.get('contentBox').one(C_BODY),
353 remaining = host.get('consoleLimit'),
354 cats = this.get(CATEGORY),
355 srcs = this.get(SOURCE),
360 host._cancelPrintLoop();
362 // Evaluate all entries from latest to oldest
363 for (i = entries.length - 1; i >= 0 && remaining >= 0; --i) {
365 if (cats[e.category] && srcs[e.source]) {
371 body.setContent(EMPTY);
372 host.buffer = buffer;
378 * Updates the checked property of a filter checkbox of the specified type.
379 * If no checkbox is found for the input params, one is created.
381 * @method _uiSetCheckbox
382 * @param type {String} 'category' or 'source'
383 * @param item {String} the name of the filter (e.g. 'info', 'event')
384 * @param checked {Boolean} value to set the checkbox's checked property
387 _uiSetCheckbox : function (type, item, checked) {
389 var container = type === CATEGORY ?
392 sel = SEL_CHECK + getCN(CONSOLE,FILTER,item),
393 checkbox = container.one(sel),
397 host = this.get(HOST);
399 this._createCheckbox(container, item);
401 checkbox = container.one(sel);
403 host._uiSetHeight(host.get('height'));
406 checkbox.set(CHECKED, checked);
411 * Passes checkbox clicks on to the category attribute.
413 * @method _onCategoryCheckboxClick
414 * @param e {Event} the DOM event
417 _onCategoryCheckboxClick : function (e) {
418 var t = e.target, cat;
420 if (t.hasClass(ConsoleFilters.CHROME_CLASSES.filter)) {
421 cat = t.get('value');
422 if (cat && cat in this.get(CATEGORY)) {
423 this.set(CATEGORY_DOT + cat, t.get(CHECKED), { fromUI: true });
429 * Passes checkbox clicks on to the source attribute.
431 * @method _onSourceCheckboxClick
432 * @param e {Event} the DOM event
435 _onSourceCheckboxClick : function (e) {
436 var t = e.target, src;
438 if (t.hasClass(ConsoleFilters.CHROME_CLASSES.filter)) {
439 src = t.get('value');
440 if (src && src in this.get(SOURCE)) {
441 this.set(SOURCE_DOT + src, t.get(CHECKED), { fromUI: true });
447 * Hides any number of categories from the UI. Convenience method for
448 * myConsole.filter.set('category.foo', false); set('category.bar', false);
451 * @method hideCategory
452 * @param cat* {String} 1..n categories to filter out of the UI
454 hideCategory : function (cat, multiple) {
455 if (isString(multiple)) {
456 Y.Array.each(arguments, this.hideCategory, this);
458 this.set(CATEGORY_DOT + cat, false);
463 * Shows any number of categories in the UI. Convenience method for
464 * myConsole.filter.set('category.foo', true); set('category.bar', true);
467 * @method showCategory
468 * @param cat* {String} 1..n categories to allow to display in the UI
470 showCategory : function (cat, multiple) {
471 if (isString(multiple)) {
472 Y.Array.each(arguments, this.showCategory, this);
474 this.set(CATEGORY_DOT + cat, true);
479 * Hides any number of sources from the UI. Convenience method for
480 * myConsole.filter.set('source.foo', false); set('source.bar', false);
484 * @param src* {String} 1..n sources to filter out of the UI
486 hideSource : function (src, multiple) {
487 if (isString(multiple)) {
488 Y.Array.each(arguments, this.hideSource, this);
490 this.set(SOURCE_DOT + src, false);
495 * Shows any number of sources in the UI. Convenience method for
496 * myConsole.filter.set('source.foo', true); set('source.bar', true);
500 * @param src* {String} 1..n sources to allow to display in the UI
502 showSource : function (src, multiple) {
503 if (isString(multiple)) {
504 Y.Array.each(arguments, this.showSource, this);
506 this.set(SOURCE_DOT + src, true);
511 * Creates a checkbox and label from the ConsoleFilters.FILTER_TEMPLATE for
512 * the provided type and name. The checkbox and label are appended to the
513 * container node passes as the first arg.
515 * @method _createCheckbox
516 * @param container {Node} the parentNode of the new checkbox and label
517 * @param name {String} the identifier of the filter
520 _createCheckbox : function (container, name) {
521 var info = Y.merge(ConsoleFilters.CHROME_CLASSES, {
523 filter_class : getCN(CONSOLE, FILTER, name)
525 node = Y.Node.create(
526 Y.substitute(ConsoleFilters.FILTER_TEMPLATE, info));
528 container.appendChild(node);
532 * Validates category updates are objects and the subattribute is not too
535 * @method _validateCategory
536 * @param cat {String} the new category:visibility map
537 * @param v {String} the subattribute path updated
541 _validateCategory : function (cat, v) {
542 return Y.Lang.isObject(v,true) && cat.split(/\./).length < 3;
546 * Validates source updates are objects and the subattribute is not too
549 * @method _validateSource
550 * @param cat {String} the new source:visibility map
551 * @param v {String} the subattribute path updated
555 _validateSource : function (src, v) {
556 return Y.Lang.isObject(v,true) && src.split(/\./).length < 3;
560 * Setter method for cacheLimit attribute. Basically a validator to ensure
563 * @method _setCacheLimit
564 * @param v {Number} Maximum number of entries
568 _setCacheLimit: function (v) {
569 if (Y.Lang.isNumber(v)) {
570 this._cacheLimit = v;
573 return Y.Attribute.INVALID_VALUE;
578 // Y.Plugin.ConsoleFilters static properties
583 * @property ConsoleFilters.NAME
586 * @default 'consoleFilters'
588 NAME : 'consoleFilters',
591 * The namespace hung off the host object that this plugin will inhabit.
593 * @property ConsoleFilters.NS
601 * Markup template used to create the container for the category filters.
603 * @property ConsoleFilters.CATEGORIES_TEMPLATE
607 CATEGORIES_TEMPLATE :
608 '<div class="{categories}"></div>',
611 * Markup template used to create the container for the source filters.
613 * @property ConsoleFilters.SOURCES_TEMPLATE
618 '<div class="{sources}"></div>',
621 * Markup template used to create the category and source filter checkboxes.
623 * @property ConsoleFilters.FILTER_TEMPLATE
628 // IE8 and FF3 don't permit breaking _between_ nowrap elements. IE8
629 // doesn't understand (non spec) wbr tag, nor does it create text nodes
630 // for spaces in innerHTML strings. The thin-space entity suffices to
631 // create a breakable point.
632 '<label class="{filter_label}">'+
633 '<input type="checkbox" value="{filter_name}" '+
634 'class="{filter} {filter_class}"> {filter_name}'+
638 * Classnames used by the templates when creating nodes.
640 * @property ConsoleFilters.CHROME_CLASSES
646 categories : getCN(CONSOLE,FILTERS,'categories'),
647 sources : getCN(CONSOLE,FILTERS,'sources'),
648 category : getCN(CONSOLE,FILTER,CATEGORY),
649 source : getCN(CONSOLE,FILTER,SOURCE),
650 filter : getCN(CONSOLE,FILTER),
651 filter_label : getCN(CONSOLE,FILTER,'label')
656 * Default visibility applied to new categories and sources.
658 * @attribute defaultVisibility
662 defaultVisibility : {
664 validator : Y.Lang.isBoolean
668 * <p>Map of entry categories to their visibility status. Update a
669 * particular category's visibility by setting the subattribute to true
670 * (visible) or false (hidden).</p>
672 * <p>For example, yconsole.filter.set('category.info', false) to hide
673 * log entries with the category/logLevel of 'info'.</p>
675 * <p>Similarly, yconsole.filter.get('category.warn') will return a
676 * boolean indicating whether that category is currently being included
679 * <p>Unlike the YUI instance configuration's logInclude and logExclude
680 * properties, filtered entries are only hidden from the UI, but
681 * can be made visible again.</p>
683 * @attribute category
688 validator : function (v,k) {
689 return this._validateCategory(k,v);
694 * <p>Map of entry sources to their visibility status. Update a
695 * particular sources's visibility by setting the subattribute to true
696 * (visible) or false (hidden).</p>
698 * <p>For example, yconsole.filter.set('sources.slider', false) to hide
699 * log entries originating from Y.Slider.</p>
706 validator : function (v,k) {
707 return this._validateSource(k,v);
712 * Maximum number of entries to store in the message cache. Use this to
713 * limit the memory footprint in environments with heavy log usage.
714 * By default, there is no limit (Number.POSITIVE_INFINITY).
716 * @attribute cacheLimit
718 * @default Number.POSITIVE_INFINITY
721 value : Number.POSITIVE_INFINITY,
722 setter : function (v) {
723 return this._setCacheLimit(v);
730 }, '3.3.0' ,{requires:['console','plugin']});