3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('console-filters', function(Y) {
10 * <p>Provides Plugin.ConsoleFilters plugin class.</p>
12 * <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>
14 * @module console-filters
16 * @class ConsoleFilters
19 // Some common strings and functions
20 var getCN = Y.ClassNameManager.getClassName,
24 CATEGORY = 'category',
26 CATEGORY_DOT = 'category.',
27 SOURCE_DOT = 'source.',
31 DEF_VISIBILITY = 'defaultVisibility',
36 C_BODY = DOT + Y.Console.CHROME_CLASSES.console_bd_class,
37 C_FOOT = DOT + Y.Console.CHROME_CLASSES.console_ft_class,
39 SEL_CHECK = 'input[type=checkbox].',
41 isString = Y.Lang.isString;
43 function ConsoleFilters() {
44 ConsoleFilters.superclass.constructor.apply(this,arguments);
47 Y.namespace('Plugin').ConsoleFilters = Y.extend(ConsoleFilters, Y.Plugin.Base,
49 // Y.Plugin.ConsoleFilters prototype
52 * Collection of all log messages passed through since the plugin's
53 * instantiation. This holds all messages regardless of filter status.
54 * Used as a single source of truth for repopulating the Console body when
55 * filters are changed.
64 * Maximum number of entries to store in the message cache.
66 * @property _cacheLimit
71 _cacheLimit : Number.POSITIVE_INFINITY,
74 * The container node created to house the category filters.
76 * @property _categories
83 * The container node created to house the source filters.
92 * Initialize entries collection and attach listeners to host events and
98 initializer : function () {
101 this.get(HOST).on("entry", this._onEntry, this);
103 this.doAfter("renderUI", this.renderUI);
104 this.doAfter("syncUI", this.syncUI);
105 this.doAfter("bindUI", this.bindUI);
107 this.doAfter("clearConsole", this._afterClearConsole);
109 if (this.get(HOST).get('rendered')) {
115 this.after("cacheLimitChange", this._afterCacheLimitChange);
119 * Removes the plugin UI and unwires events.
124 destructor : function () {
125 //TODO: grab last {consoleLimit} entries and update the console with
126 //them (no filtering)
129 if (this._categories) {
130 this._categories.remove();
133 this._sources.remove();
138 * Adds the category and source filter sections to the Console footer.
143 renderUI : function () {
144 var foot = this.get(HOST).get('contentBox').one(C_FOOT),
149 ConsoleFilters.CATEGORIES_TEMPLATE,
150 ConsoleFilters.CHROME_CLASSES);
152 this._categories = foot.appendChild(Y.Node.create(html));
155 ConsoleFilters.SOURCES_TEMPLATE,
156 ConsoleFilters.CHROME_CLASSES);
158 this._sources = foot.appendChild(Y.Node.create(html));
163 * Binds to checkbox click events and internal attribute change events to
164 * maintain the UI state.
169 bindUI : function () {
170 this._categories.on('click', Y.bind(this._onCategoryCheckboxClick, this));
172 this._sources.on('click', Y.bind(this._onSourceCheckboxClick, this));
174 this.after('categoryChange',this._afterCategoryChange);
175 this.after('sourceChange', this._afterSourceChange);
179 * Updates the UI to be in accordance with the current state of the plugin.
183 syncUI : function () {
184 Y.each(this.get(CATEGORY), function (v, k) {
185 this._uiSetCheckbox(CATEGORY, k, v);
188 Y.each(this.get(SOURCE), function (v, k) {
189 this._uiSetCheckbox(SOURCE, k, v);
192 this.refreshConsole();
196 * Ensures a filter is set up for any new categories or sources and
197 * collects the messages in _entries. If the message is stamped with a
198 * category or source that is currently being filtered out, the message
199 * will not pass to the Console's print buffer.
202 * @param e {Event} the custom event object
205 _onEntry : function (e) {
206 this._entries.push(e.message);
208 var cat = CATEGORY_DOT + e.message.category,
209 src = SOURCE_DOT + e.message.source,
210 cat_filter = this.get(cat),
211 src_filter = this.get(src),
212 overLimit = this._entries.length - this._cacheLimit,
216 this._entries.splice(0, overLimit);
219 if (cat_filter === undefined) {
220 visible = this.get(DEF_VISIBILITY);
221 this.set(cat, visible);
222 cat_filter = visible;
225 if (src_filter === undefined) {
226 visible = this.get(DEF_VISIBILITY);
227 this.set(src, visible);
228 src_filter = visible;
231 if (!cat_filter || !src_filter) {
237 * Flushes the cached entries after a call to the Console's clearConsole().
239 * @method _afterClearConsole
242 _afterClearConsole : function () {
247 * Triggers the Console to update if a known category filter
248 * changes value (e.g. visible => hidden). Updates the appropriate
249 * checkbox's checked state if necessary.
251 * @method _afterCategoryChange
252 * @param e {Event} the attribute change event object
255 _afterCategoryChange : function (e) {
256 var cat = e.subAttrName.replace(/category\./, EMPTY),
260 // Don't update the console for new categories
261 if (!cat || before[cat] !== undefined) {
262 this.refreshConsole();
264 this._filterBuffer();
267 if (cat && !e.fromUI) {
268 this._uiSetCheckbox(CATEGORY, cat, after[cat]);
273 * Triggers the Console to update if a known source filter
274 * changes value (e.g. visible => hidden). Updates the appropriate
275 * checkbox's checked state if necessary.
277 * @method _afterSourceChange
278 * @param e {Event} the attribute change event object
281 _afterSourceChange : function (e) {
282 var src = e.subAttrName.replace(/source\./, EMPTY),
286 // Don't update the console for new sources
287 if (!src || before[src] !== undefined) {
288 this.refreshConsole();
290 this._filterBuffer();
293 if (src && !e.fromUI) {
294 this._uiSetCheckbox(SOURCE, src, after[src]);
299 * Flushes the Console's print buffer of any entries that have a category
300 * or source that is currently being excluded.
302 * @method _filterBuffer
305 _filterBuffer : function () {
306 var cats = this.get(CATEGORY),
307 srcs = this.get(SOURCE),
308 buffer = this.get(HOST).buffer,
312 for (i = buffer.length - 1; i >= 0; --i) {
313 if (!cats[buffer[i].category] || !srcs[buffer[i].source]) {
316 buffer.splice(i,(start - i));
321 buffer.splice(0,start + 1);
326 * Trims the cache of entries to the appropriate new length.
328 * @method _afterCacheLimitChange
329 * @param e {Event} the attribute change event object
332 _afterCacheLimitChange : function (e) {
333 if (isFinite(e.newVal)) {
334 var delta = this._entries.length - e.newVal;
337 this._entries.splice(0,delta);
343 * Repopulates the Console with entries appropriate to the current filter
346 * @method refreshConsole
348 refreshConsole : function () {
349 var entries = this._entries,
350 host = this.get(HOST),
351 body = host.get('contentBox').one(C_BODY),
352 remaining = host.get('consoleLimit'),
353 cats = this.get(CATEGORY),
354 srcs = this.get(SOURCE),
359 host._cancelPrintLoop();
361 // Evaluate all entries from latest to oldest
362 for (i = entries.length - 1; i >= 0 && remaining >= 0; --i) {
364 if (cats[e.category] && srcs[e.source]) {
370 body.setContent(EMPTY);
371 host.buffer = buffer;
377 * Updates the checked property of a filter checkbox of the specified type.
378 * If no checkbox is found for the input params, one is created.
380 * @method _uiSetCheckbox
381 * @param type {String} 'category' or 'source'
382 * @param item {String} the name of the filter (e.g. 'info', 'event')
383 * @param checked {Boolean} value to set the checkbox's checked property
386 _uiSetCheckbox : function (type, item, checked) {
388 var container = type === CATEGORY ?
391 sel = SEL_CHECK + getCN(CONSOLE,FILTER,item),
392 checkbox = container.one(sel),
396 host = this.get(HOST);
398 this._createCheckbox(container, item);
400 checkbox = container.one(sel);
402 host._uiSetHeight(host.get('height'));
405 checkbox.set(CHECKED, checked);
410 * Passes checkbox clicks on to the category attribute.
412 * @method _onCategoryCheckboxClick
413 * @param e {Event} the DOM event
416 _onCategoryCheckboxClick : function (e) {
417 var t = e.target, cat;
419 if (t.hasClass(ConsoleFilters.CHROME_CLASSES.filter)) {
420 cat = t.get('value');
421 if (cat && cat in this.get(CATEGORY)) {
422 this.set(CATEGORY_DOT + cat, t.get(CHECKED), { fromUI: true });
428 * Passes checkbox clicks on to the source attribute.
430 * @method _onSourceCheckboxClick
431 * @param e {Event} the DOM event
434 _onSourceCheckboxClick : function (e) {
435 var t = e.target, src;
437 if (t.hasClass(ConsoleFilters.CHROME_CLASSES.filter)) {
438 src = t.get('value');
439 if (src && src in this.get(SOURCE)) {
440 this.set(SOURCE_DOT + src, t.get(CHECKED), { fromUI: true });
446 * Hides any number of categories from the UI. Convenience method for
447 * myConsole.filter.set('category.foo', false); set('category.bar', false);
450 * @method hideCategory
451 * @param cat* {String} 1..n categories to filter out of the UI
453 hideCategory : function (cat, multiple) {
454 if (isString(multiple)) {
455 Y.Array.each(arguments, this.hideCategory, this);
457 this.set(CATEGORY_DOT + cat, false);
462 * Shows any number of categories in the UI. Convenience method for
463 * myConsole.filter.set('category.foo', true); set('category.bar', true);
466 * @method showCategory
467 * @param cat* {String} 1..n categories to allow to display in the UI
469 showCategory : function (cat, multiple) {
470 if (isString(multiple)) {
471 Y.Array.each(arguments, this.showCategory, this);
473 this.set(CATEGORY_DOT + cat, true);
478 * Hides any number of sources from the UI. Convenience method for
479 * myConsole.filter.set('source.foo', false); set('source.bar', false);
483 * @param src* {String} 1..n sources to filter out of the UI
485 hideSource : function (src, multiple) {
486 if (isString(multiple)) {
487 Y.Array.each(arguments, this.hideSource, this);
489 this.set(SOURCE_DOT + src, false);
494 * Shows any number of sources in the UI. Convenience method for
495 * myConsole.filter.set('source.foo', true); set('source.bar', true);
499 * @param src* {String} 1..n sources to allow to display in the UI
501 showSource : function (src, multiple) {
502 if (isString(multiple)) {
503 Y.Array.each(arguments, this.showSource, this);
505 this.set(SOURCE_DOT + src, true);
510 * Creates a checkbox and label from the ConsoleFilters.FILTER_TEMPLATE for
511 * the provided type and name. The checkbox and label are appended to the
512 * container node passes as the first arg.
514 * @method _createCheckbox
515 * @param container {Node} the parentNode of the new checkbox and label
516 * @param name {String} the identifier of the filter
519 _createCheckbox : function (container, name) {
520 var info = Y.merge(ConsoleFilters.CHROME_CLASSES, {
522 filter_class : getCN(CONSOLE, FILTER, name)
524 node = Y.Node.create(
525 Y.substitute(ConsoleFilters.FILTER_TEMPLATE, info));
527 container.appendChild(node);
531 * Validates category updates are objects and the subattribute is not too
534 * @method _validateCategory
535 * @param cat {String} the new category:visibility map
536 * @param v {String} the subattribute path updated
540 _validateCategory : function (cat, v) {
541 return Y.Lang.isObject(v,true) && cat.split(/\./).length < 3;
545 * Validates source updates are objects and the subattribute is not too
548 * @method _validateSource
549 * @param cat {String} the new source:visibility map
550 * @param v {String} the subattribute path updated
554 _validateSource : function (src, v) {
555 return Y.Lang.isObject(v,true) && src.split(/\./).length < 3;
559 * Setter method for cacheLimit attribute. Basically a validator to ensure
562 * @method _setCacheLimit
563 * @param v {Number} Maximum number of entries
567 _setCacheLimit: function (v) {
568 if (Y.Lang.isNumber(v)) {
569 this._cacheLimit = v;
572 return Y.Attribute.INVALID_VALUE;
577 // Y.Plugin.ConsoleFilters static properties
585 * @default 'consoleFilters'
587 NAME : 'consoleFilters',
590 * The namespace hung off the host object that this plugin will inhabit.
600 * Markup template used to create the container for the category filters.
602 * @property CATEGORIES_TEMPLATE
606 CATEGORIES_TEMPLATE :
607 '<div class="{categories}"></div>',
610 * Markup template used to create the container for the source filters.
612 * @property SOURCES_TEMPLATE
617 '<div class="{sources}"></div>',
620 * Markup template used to create the category and source filter checkboxes.
622 * @property FILTER_TEMPLATE
627 // IE8 and FF3 don't permit breaking _between_ nowrap elements. IE8
628 // doesn't understand (non spec) wbr tag, nor does it create text nodes
629 // for spaces in innerHTML strings. The thin-space entity suffices to
630 // create a breakable point.
631 '<label class="{filter_label}">'+
632 '<input type="checkbox" value="{filter_name}" '+
633 'class="{filter} {filter_class}"> {filter_name}'+
637 * Classnames used by the templates when creating nodes.
639 * @property CHROME_CLASSES
645 categories : getCN(CONSOLE,FILTERS,'categories'),
646 sources : getCN(CONSOLE,FILTERS,'sources'),
647 category : getCN(CONSOLE,FILTER,CATEGORY),
648 source : getCN(CONSOLE,FILTER,SOURCE),
649 filter : getCN(CONSOLE,FILTER),
650 filter_label : getCN(CONSOLE,FILTER,'label')
655 * Default visibility applied to new categories and sources.
657 * @attribute defaultVisibility
661 defaultVisibility : {
663 validator : Y.Lang.isBoolean
667 * <p>Map of entry categories to their visibility status. Update a
668 * particular category's visibility by setting the subattribute to true
669 * (visible) or false (hidden).</p>
671 * <p>For example, yconsole.filter.set('category.info', false) to hide
672 * log entries with the category/logLevel of 'info'.</p>
674 * <p>Similarly, yconsole.filter.get('category.warn') will return a
675 * boolean indicating whether that category is currently being included
678 * <p>Unlike the YUI instance configuration's logInclude and logExclude
679 * properties, filtered entries are only hidden from the UI, but
680 * can be made visible again.</p>
682 * @attribute category
687 validator : function (v,k) {
688 return this._validateCategory(k,v);
693 * <p>Map of entry sources to their visibility status. Update a
694 * particular sources's visibility by setting the subattribute to true
695 * (visible) or false (hidden).</p>
697 * <p>For example, yconsole.filter.set('sources.slider', false) to hide
698 * log entries originating from Y.Slider.</p>
705 validator : function (v,k) {
706 return this._validateSource(k,v);
711 * Maximum number of entries to store in the message cache. Use this to
712 * limit the memory footprint in environments with heavy log usage.
713 * By default, there is no limit (Number.POSITIVE_INFINITY).
715 * @attribute cacheLimit
717 * @default Number.POSITIVE_INFINITY
720 value : Number.POSITIVE_INFINITY,
721 setter : function (v) {
722 return this._setCacheLimit(v);
729 }, '3.5.1' ,{requires:['console','plugin']});