Skip to content

Commit

Permalink
add updateOption plugin event
Browse files Browse the repository at this point in the history
  • Loading branch information
Maizify committed Sep 21, 2017
1 parent caafd8e commit cd9a494
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,24 @@ class VConsole {
this._triggerPluginEvent(this.activedTab, 'show');
}

/**
* update option(s)
* @public
*/
setOption(keyOrObj, value) {
if (tool.isString(keyOrObj)) {
this.option[keyOrObj] = value;
this._triggerPluginsEvent('updateOption');
} else if (tool.isObject(keyOrObj)) {
for (let k in keyOrObj) {
this.option[k] = keyOrObj[k];
}
this._triggerPluginsEvent('updateOption');
} else {
console.debug('The first parameter of vConsole.setOption() must be a string or an object.');
}
}

/**
* uninstall vConsole
* @public
Expand Down
39 changes: 31 additions & 8 deletions src/log/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import tplItem from './item.html';
import tplFold from './item_fold.html';
import tplFoldCode from './item_fold_code.html';

const DEFAULT_MAX_LOG_NUMBER = 1000;

class VConsoleLogTab extends VConsolePlugin {

constructor(...args) {
Expand All @@ -34,7 +36,7 @@ class VConsoleLogTab extends VConsolePlugin {
this.console = {};
this.logList = []; // save logs before ready
this.isInBottom = true; // whether the panel is in the bottom
this.maxLogNumber = 1000;
this.maxLogNumber = DEFAULT_MAX_LOG_NUMBER;
this.logNumber = 0;

this.mockConsole();
Expand All @@ -47,8 +49,7 @@ class VConsoleLogTab extends VConsolePlugin {
*/
onInit() {
this.$tabbox = $.render(this.tplTabbox, {});
this.maxLogNumber = this.vConsole.option.maxLogNumber || this.maxLogNumber;
this.maxLogNumber = Math.max(1, this.maxLogNumber);
this.updateMaxLogNumber();
}

/**
Expand Down Expand Up @@ -175,6 +176,32 @@ class VConsoleLogTab extends VConsolePlugin {
}
}

onUpdateOption() {
if (this.vConsole.option.maxLogNumber != this.maxLogNumber) {
this.updateMaxLogNumber();
this.limitMaxLogs();
}
}

updateMaxLogNumber() {
this.maxLogNumber = this.vConsole.option.maxLogNumber || DEFAULT_MAX_LOG_NUMBER;
this.maxLogNumber = Math.max(1, this.maxLogNumber);
}

limitMaxLogs() {
if (!this.isReady) {
return;
}
while (this.logNumber > this.maxLogNumber) {
let $firstItem = $.one('.vc-item', this.$tabbox);
if (!$firstItem) {
break;
}
$firstItem.parentNode.removeChild($firstItem);
this.logNumber--;
}
}

showLogType(logType) {
let $log = $.one('.vc-log', this.$tabbox);
$.removeClass($log, 'vc-log-partly-log');
Expand Down Expand Up @@ -354,11 +381,7 @@ class VConsoleLogTab extends VConsolePlugin {

// remove overflow logs
this.logNumber++;
if (this.logNumber > this.maxLogNumber) {
let $firstItem = $.one('.vc-item', this.$tabbox);
$firstItem.parentNode.removeChild($firstItem);
this.logNumber--;
}
this.limitMaxLogs();

// scroll to bottom if it is in the bottom before
if (this.isInBottom) {
Expand Down

0 comments on commit cd9a494

Please sign in to comment.