Skip to content

Commit

Permalink
add vConsole.removePlugin() method; add remove plugin event
Browse files Browse the repository at this point in the history
  • Loading branch information
Maizify committed Sep 26, 2016
1 parent 761c8a3 commit 61177d7
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
English | [简体中文](./CHANGELOG_CN.md)

#### V2.x.0 (2016-09-xx)

- [FEATURE] Add `vConsole.removePlugin()` method, see [Public Properties & Methods](./doc/public_properties_methods.md).
- [FEATURE] Add `remove` plugin event, see [Plugin: Event List](./doc/plugin_event_list.md).
- [IMPROVE] Disable page scrolling while vConsole is scrolling.
- [FIX] Fix `window.onerror()` typo.


#### V2.4.0 (2016-08-31)

- [FEATURE] Add `addTopBar` plugin event, see [Plugin: Event List](./doc/plugin_event_list.md).
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
[English](./CHANGELOG.md) | 简体中文

#### V2.x.0 (2016-09-xx)

- 【特性】新增 `vConsole.removePlugin()` 方法,请查阅[Public Properties & Methods](./doc/public_properties_methods_CN.md)
- 【特性】新增 `remove` 插件事件,请查阅[插件:Event 事件列表](./doc/plugin_event_list_CN.md)
- 【优化】页面不会随着 vConsole 的滚动而滚动。
- 【修复】修正 `window.onerror()` 内的函数调用笔误。


#### V2.4.0 (2016-08-31)

- 【特性】新增 `addTopBar` 插件事件,请查阅[插件:Event 事件列表](./doc/plugin_event_list_CN.md)
Expand Down
6 changes: 3 additions & 3 deletions dist/vconsole.min.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions doc/plugin_event_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ myPlugin.on('ready', function() {
```


## remove

Trigger before starting to uninitialize a plugin. This event will only be triggered once.

Note that this event may be called before `init` event if you remove a plugin before vConsole is ready.

##### Callback Arguments:

- none

##### Example:

```javascript
myPlugin.on('remove', function() {
// do something...
});
```


## show

Trigger when a tab is shown. Only the plugin binded with `renderTab` will receive this event.
Expand Down
19 changes: 19 additions & 0 deletions doc/plugin_event_list_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ myPlugin.on('ready', function() {
```


## remove

当插件即将卸载前触发。这个事件只会触发一次。

需要注意的是,如果在 vConsole ready 之前就卸载插件,那么此事件会在 `init` 之前就被调用。

##### Callback 参数:

-

##### 例子:

```javascript
myPlugin.on('remove', function() {
// do something...
});
```


## show

当插件的 tab 被显示时触发。只有绑定了 `renderTab` 事件的插件才会收到此事件。
Expand Down
17 changes: 17 additions & 0 deletions doc/public_properties_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ vConsole.addPlugin(myPlugin);
```


### vConsole.removePlugin(pluginID)

Remove an existing plugin.

##### Parameters:
- (required) pluginID: A string, plugin's id.

##### Return:
- Boolean: `true` for success, `false` for failure.

##### Example:

```javascript
vConsole.removePlugin('my_plugin');
```


### vConsole.showTab(pluginID)

Activating a tab according to its plugin id.
Expand Down
17 changes: 17 additions & 0 deletions doc/public_properties_methods_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ vConsole.addPlugin(myPlugin);
```


### vConsole.removePlugin(pluginID)

卸载一个插件。

##### 参数:
- (required) pluginID: 插件的 plugin id。

##### 返回:
- Boolean: 成功为 `true`,失败为 `false`

##### 例子:

```javascript
vConsole.removePlugin('my_plugin');
```


### vConsole.showTab(pluginID)

根据 plugin id 激活显示一个 tab。
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vconsole",
"version": "2.4.1-pre",
"version": "2.5.0-pre",
"description": "A lightweight, extendable front-end developer tool for mobile web page.",
"homepage": "https://github.com/WechatFE/vConsole",
"main": "dist/vconsole.min.js",
Expand Down
55 changes: 55 additions & 0 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ class VConsole {
* add a new plugin
* @public
* @param object VConsolePlugin object
* @return boolean
*/
addPlugin(plugin) {
// ignore this plugin if it has already been installed
Expand All @@ -453,6 +454,60 @@ class VConsole {
return true;
}

/**
* remove a plugin
* @public
* @param string pluginID
* @return boolean
*/
removePlugin(pluginID) {
pluginID = (pluginID + '').toLowerCase();
let plugin = this.pluginList[pluginID];
// skip if is has not been installed
if (plugin === undefined) {
console.warn('Plugin ' + pluginID + ' does not exist.');
return false;
}
// trigger `remove` event before uninstall
plugin.trigger('remove');
// the plugin will not be initialized before vConsole is ready,
// so the plugin does not need to handle DOM-related actions in this case
if (this.isReady) {
let $tabbar = $.one('#__vc_tab_' + pluginID);
$tabbar && $tabbar.parentNode.removeChild($tabbar);
// remove topbar
let $topbar = $.all('.vc-topbar-' + pluginID, this.$dom);
for (let i=0; i<$topbar.length; i++) {
$topbar[i].parentNode.removeChild($topbar[i]);
}
// remove content
let $content = $.one('#__vc_log_' + pluginID);
$content && $content.parentNode.removeChild($content);
// remove tool bar
let $toolbar = $.all('.vc-tool-' + pluginID, this.$dom);
for (let i=0; i<$toolbar.length; i++) {
$toolbar[i].parentNode.removeChild($toolbar[i]);
}
}
// remove plugin from list
let index = this.tabList.indexOf(pluginID);
if (index > -1) {
this.tabList.splice(index, 1);
}
try {
delete this.pluginList[pluginID];
} catch (e) {
this.pluginList[pluginID] = undefined;
}
// show the first plugin by default
if (this.activedTab == pluginID) {
if (this.tabList.length > 0) {
this.showTab(this.tabList[0]);
}
}
return true;
}

/**
* show console panel
* @public
Expand Down
14 changes: 14 additions & 0 deletions src/log/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ class VConsoleLogTab extends VConsolePlugin {
});
}

/**
* before remove
* @public
*/
onRemove() {
// recover original console
window.console.log = this.console.log;
window.console.info = this.console.info;
window.console.warn = this.console.warn;
window.console.debug = this.console.debug;
window.console.error = this.console.error;
this.console = {};
}

onShow() {
this.isShow = true;
if (this.isInBottom == true) {
Expand Down
14 changes: 14 additions & 0 deletions src/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class VConsoleNetworkTab extends VConsolePlugin {
this.domList = {}; // URL as key, dom item as value
this.isShow = false;
this.isInBottom = true; // whether the panel is in the bottom
this._open = undefined; // the origin function
this._send = undefined;

this.mockAjax();
}
Expand Down Expand Up @@ -73,6 +75,16 @@ class VConsoleNetworkTab extends VConsolePlugin {

}

onRemove() {
// recover original functions
if (window.XMLHttpRequest) {
window.XMLHttpRequest.prototype.open = this._open;
window.XMLHttpRequest.prototype.send = this._send;
this._open = undefined;
this._send = undefined;
}
}

onShow() {
this.isShow = true;
if (this.isInBottom == true) {
Expand Down Expand Up @@ -191,6 +203,8 @@ class VConsoleNetworkTab extends VConsolePlugin {
let that = this;
let _open = window.XMLHttpRequest.prototype.open,
_send = window.XMLHttpRequest.prototype.send;
that._open = _open;
that._send = _send;

// mock open()
window.XMLHttpRequest.prototype.open = function() {
Expand Down

0 comments on commit 61177d7

Please sign in to comment.