1
0
mirror of synced 2025-12-14 02:38:29 +08:00

Add: Destroy api #35

This commit is contained in:
surunzi
2017-10-05 19:01:05 +08:00
parent 77d577076e
commit c1550c01d9
11 changed files with 112 additions and 6 deletions

View File

@@ -16,3 +16,8 @@ function exports(css)
container.appendChild(style);
}
exports.reset = function ()
{
mark = [];
};

View File

@@ -15,8 +15,8 @@
},
"eustia": {
"eruda": {
"files": "src/**/*.es6",
"ignore": "**/Info/defInfo.es6",
"files": "src/**/*.js",
"ignore": "**/Info/defInfo.js",
"output": "src/lib/util.js",
"exclude": [
"createCfg"

View File

@@ -78,6 +78,13 @@ export default class Console extends Tool
return this;
}
destroy()
{
super.destroy();
this.ignoreGlobalErr();
this.restoreConsole();
}
_appendTpl()
{
let $el = this._$el;

View File

@@ -56,7 +56,7 @@ export default class DevTools extends util.Emitter
let name = tool.name;
if (!name) return logger.error('You must specify a name for a tool');
name = name.toLowerCase();
if (this._tools[name]) return logger.warn('Tool ' + name + ' already exists' );
if (this._tools[name]) return logger.warn(`Tool ${name} already exists`);
this._$tools.prepend(`<div class="eruda-${name} eruda-tool"></div>`);
tool.init(this._$tools.find(`.eruda-${name}`), this);
@@ -67,6 +67,31 @@ export default class DevTools extends util.Emitter
return this;
}
remove(name)
{
let tools = this._tools;
if (!tools[name]) return logger.warn(`Tool ${name} doesn't exist`);
this._navBar.remove(name);
let tool = tools[name];
delete tools[name];
if (tool.active)
{
let keys = util.keys(tools);
if (keys.length > 0) this.showTool(tools[util.last(keys)].name);
}
tool.destroy();
return this;
}
removeAll()
{
util.each(this._tools, tool => this.remove(tool.name));
return this;
}
get(name)
{
let tool = this._tools[name];
@@ -146,6 +171,12 @@ export default class DevTools extends util.Emitter
this._$el.css('paddingTop', height);
this._navBar.setHeight(height);
}
destroy()
{
this.removeAll();
this._navBar.destroy();
this._$el.remove();
}
_setTransparency(opacity)
{
if (!util.isNum(opacity)) return;

View File

@@ -22,6 +22,15 @@ export default class NavBar extends util.Emitter
this._$el.prepend(`<div class="eruda-nav-bar-item">${name}</div>`);
this.resetStyle();
}
remove(name)
{
this._len--;
this._$el.find('.eruda-nav-bar-item').each(function ()
{
let $this = util.$(this);
if ($this.text().toLowerCase() === name.toLowerCase()) $this.remove();
});
}
setHeight(height)
{
this._height = height;
@@ -49,6 +58,10 @@ export default class NavBar extends util.Emitter
}
});
}
destroy()
{
this._$el.remove();
}
_resetBottomBar()
{
let $bottomBar = this._$bottomBar;

View File

@@ -16,4 +16,8 @@ export default class Tool
return this;
}
destroy()
{
this._$el.remove();
}
}

View File

@@ -83,6 +83,13 @@ export default class Elements extends Tool
if (this._origAddEvent) winEventProto.addEventListener = this._origAddEvent;
if (this._origRmEvent) winEventProto.removeEventListener = this._origRmEvent;
}
destroy()
{
super.destroy();
this._select.disable();
this.restoreEventTarget();
}
_back()
{
if (this._curEl === this._htmlEl) return;

View File

@@ -22,6 +22,10 @@ export default class EntryBtn extends util.Emitter
{
this._$el.show();
}
destroy()
{
this._$el.remove();
}
_appendTpl()
{
let $parent = this._$parent;

View File

@@ -260,6 +260,12 @@ export default class Network extends Tool
});
this._performanceTiming = performanceTiming;
}
destroy()
{
super.destroy();
this.restoreXhr();
}
_getResourceTimingData()
{
if (!this._hasResourceTiming) return;

View File

@@ -50,6 +50,12 @@ module.exports = {
return this;
},
remove(name)
{
this._devTools.remove(name);
return this;
},
show(name)
{
if (!this._checkInit()) return;
@@ -68,10 +74,28 @@ module.exports = {
return this;
},
destroy()
{
this._devTools.destroy();
delete this._devTools;
this.entryBtn.destroy();
delete this.entryBtn;
this._unregisterListener();
this._$el.remove();
util.evalCss.reset();
},
_registerListener()
{
emitter.on(emitter.ADD, (...args) => this.add(...args));
emitter.on(emitter.SHOW, (...args) => this.show(...args));
this._addListener = (...args) => this.add(...args);
this._showListener = (...args) => this.show(...args);
emitter.on(emitter.ADD, this._addListener);
emitter.on(emitter.SHOW, this._showListener);
},
_unregisterListener()
{
emitter.off(emitter.ADD, this._addListener);
emitter.off(emitter.SHOW, this._showListener);
},
_checkInit()
{

View File

@@ -758,6 +758,11 @@ module.exports = (function ()
container.appendChild(style);
}
exports.reset = function ()
{
mark = [];
};
return exports;
})();