1
0
mirror of synced 2025-12-09 07:08:17 +08:00

Dev: Plugin initialization

This commit is contained in:
surunzi
2017-01-02 12:09:37 +08:00
parent b224c116fb
commit 6a8894e6fe
4 changed files with 32 additions and 4 deletions

View File

@@ -45,9 +45,13 @@ export default class DevTools extends util.Emitter
} }
add(tool) add(tool)
{ {
util.defaults(tool, new Tool()); let {init, show, hide} = new Tool();
util.defaults(tool, {init, show, hide});
var name = tool.name; let name = tool.name;
if (!name) throw new Error('You must specify a name for a tool');
name = name.toLowerCase();
if (this._tools[name]) throw new Error('Tool ' + name + ' already exists' );
this._$tools.prepend(`<div class="eruda-${name} eruda-tool"></div>`); this._$tools.prepend(`<div class="eruda-${name} eruda-tool"></div>`);
tool.init(this._$tools.find(`.eruda-${name}`), this); tool.init(this._$tools.find(`.eruda-${name}`), this);

View File

@@ -19,8 +19,11 @@ export default class NavBar extends util.Emitter
} }
add(name) add(name)
{ {
let $bottomBar = this._$bottomBar;
this._len++; this._len++;
this._$ul.prepend(`<li class="${name}" ontouchstart>${name}</li>`); this._$ul.prepend(`<li class="${name}" ontouchstart>${name}</li>`);
$bottomBar.css('left', util.pxToNum($bottomBar.css('left')) + ITEM_WIDTH);
this._resetStyle(); this._resetStyle();
} }
setHeight(height) setHeight(height)
@@ -39,7 +42,7 @@ export default class NavBar extends util.Emitter
if ($this.text() === name) if ($this.text() === name)
{ {
$this.addClass('eruda-active'); $this.addClass('eruda-active');
self._$bottomBar.css({left: ITEM_WIDTH * idx}); self._$bottomBar.css('left', ITEM_WIDTH * idx);
} else } else
{ {
$this.rmClass('eruda-active'); $this.rmClass('eruda-active');
@@ -68,4 +71,4 @@ export default class NavBar extends util.Emitter
} }
} }
const ITEM_WIDTH = 69; const ITEM_WIDTH = 69;

View File

@@ -32,6 +32,8 @@ module.exports = {
}, },
add(tool) add(tool)
{ {
if (util.isFn(tool)) tool = tool(this);
this._devTools.add(tool); this._devTools.add(tool);
return this; return this;

View File

@@ -15,6 +15,9 @@
<li> <li>
<a href="#" id="issue17">#17</a> <a href="#" id="issue17">#17</a>
</li> </li>
<li>
<a href="#" id="plugin">Plugin</a>
</li>
</ul> </ul>
</nav> </nav>
<script> <script>
@@ -37,6 +40,22 @@
var a = new A(); var a = new A();
console.log(a); console.log(a);
}); });
addClickEvent('plugin', function ()
{
eruda.add({name: 'test'});
eruda.add(function (eruda)
{
console.log(eruda);
return {
name: 'test2',
init: function ($el)
{
this._$el = $el;
this._$el.html('This is the new plugin');
}
};
});
});
</script> </script>
<script>boot();</script> <script>boot();</script>
</body> </body>