Use Object.create to create subclasses

This commit is contained in:
YUKI Hiroshi 2014-04-02 19:25:51 +09:00
parent e0bb0bd8c8
commit fd6c08937a
8 changed files with 67 additions and 30 deletions

View File

@ -2,8 +2,9 @@ Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this,
'TreeStyleTabUtils', 'resource://treestyletab-modules/utils.js');
var TreeStyleTabBookmarksService = {
__proto__ : TreeStyleTabService,
(function() {
let { inherit } = Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm', {});
var TreeStyleTabBookmarksService = inherit(TreeStyleTabService, {
get BookmarksService() {
if (!this._BookmarksService) {
@ -500,6 +501,9 @@ var TreeStyleTabBookmarksService = {
}
}
};
});
TreeStyleTabBookmarksService.preInit();
window.TreeStyleTabBookmarksService = TreeStyleTabBookmarksService;
)();

View File

@ -2,8 +2,9 @@ Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this,
'TreeStyleTabUtils', 'resource://treestyletab-modules/utils.js');
var TreeStyleTabBookmarksServiceEditable = {
__proto__ : TreeStyleTabBookmarksService,
(function() {
let { inherit } = Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm', {});
var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService, {
instantApply : false,
canceled : false,
@ -356,6 +357,9 @@ var TreeStyleTabBookmarksServiceEditable = {
}
}
};
});
window.addEventListener('DOMContentLoaded', TreeStyleTabBookmarksServiceEditable, false);
window.TreeStyleTabBookmarksServiceEditable = TreeStyleTabBookmarksServiceEditable;
})();

View File

@ -43,6 +43,7 @@ const Cu = Components.utils;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/Timer.jsm');
Cu.import('resource://treestyletab-modules/lib/inherit.jsm');
Cu.import('resource://treestyletab-modules/constants.js');
XPCOMUtils.defineLazyGetter(this, 'window', function() {
@ -83,8 +84,7 @@ else {
this.AeroPeek = null;
}
var TreeStyleTabBase = {
__proto__ : TreeStyleTabConstants,
var TreeStyleTabBase = inherit(TreeStyleTabConstants, {
tabsHash : null,
inWindowDestoructionProcess : false,
@ -2567,7 +2567,7 @@ var TreeStyleTabBase = {
}
}
};
});
TreeStyleTabBase.init();

View File

@ -44,6 +44,7 @@ const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://treestyletab-modules/lib/inherit.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Services', 'resource://gre/modules/Services.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'utils', 'resource://treestyletab-modules/utils.js', 'TreeStyleTabUtils');
@ -87,8 +88,7 @@ function TreeStyleTabBrowser(aWindowService, aTabBrowser)
this._treeViewEnabled = true;
}
TreeStyleTabBrowser.prototype = {
__proto__ : TreeStyleTabWindow.prototype,
TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, {
kMENUITEM_RELOADSUBTREE : 'context-item-reloadTabSubtree',
kMENUITEM_RELOADCHILDREN : 'context-item-reloadDescendantTabs',
@ -5279,29 +5279,26 @@ TreeStyleTabBrowser.prototype = {
this.detachTab(tab, aInfo);
if (i == 0) {
if (parentTab) {
this.attachTabTo(tab, parentTab, {
__proto__ : aInfo,
this.attachTabTo(tab, parentTab, inherit(aInfo, {
dontExpand : true,
dontMove : true
});
}));
}
this.collapseExpandSubtree(tab, false);
this.deleteTabValue(tab, this.kSUBTREE_COLLAPSED);
}
else {
this.attachTabTo(tab, children[0], {
__proto__ : aInfo,
this.attachTabTo(tab, children[0], inherit(aInfo, {
dontExpand : true,
dontMove : true
});
}));
}
}
else if (aInfo.behavior == this.kCLOSE_PARENT_BEHAVIOR_PROMOTE_ALL_CHILDREN && parentTab) {
this.attachTabTo(tab, parentTab, {
__proto__ : aInfo,
this.attachTabTo(tab, parentTab, inherit(aInfo, {
dontExpand : true,
dontMove : true
});
}));
}
else { // aInfo.behavior == this.kCLOSE_PARENT_BEHAVIOR_SIMPLY_DETACH_ALL_CHILDREN
this.detachTab(tab, aInfo);
@ -6737,5 +6734,5 @@ TreeStyleTabBrowser.prototype = {
delayedShowTabbarForFeedback : function TSTBrowser_delayedShowTabbarForFeedback() { this.autoHide.delayedShowForFeedback(); },
cancelHideTabbarForFeedback : function TSTBrowser_cancelHideTabbarForFeedback() { this.autoHide.cancelHideForFeedback(); }
};
});

View File

@ -41,6 +41,7 @@ const Ci = Components.interfaces;
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm');
Components.utils.import('resource://treestyletab-modules/base.js');
Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js');
@ -53,8 +54,7 @@ function FullTooltipManager(aOwner)
{
this.init(aOwner);
}
FullTooltipManager.prototype = {
__proto__ : TreeStyleTabBase,
FullTooltipManager.prototype = inherit(TreeStyleTabBase, {
kTOOLTIP_MODE_DEFAULT : 0,
kTOOLTIP_MODE_COLLAPSED : 1,
@ -477,4 +477,4 @@ FullTooltipManager.prototype = {
range.deleteContents();
range.detach();
}
};
});

View File

@ -39,6 +39,7 @@ const EXPORTED_SYMBOLS = ['GroupTab'];
const Cc = Components.classes;
const Ci = Components.interfaces;
Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm');
Components.utils.import('resource://treestyletab-modules/base.js');
Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js');
@ -48,8 +49,7 @@ function GroupTab(aWindow)
this.init();
}
GroupTab.prototype = {
__proto__ : TreeStyleTabBase,
GroupTab.prototype = inherit(TreeStyleTabBase, {
initialized : false,
shouldUpdate : false,
@ -427,4 +427,4 @@ GroupTab.prototype = {
this.checkUpdateTreeNow();
}
};
});

32
modules/lib/inherit.jsm Normal file
View File

@ -0,0 +1,32 @@
/**
* @fileOverview inherit, an alternative for __proto__
* @author YUKI "Piro" Hiroshi
* @version 1
*
* @license
* The MIT License, Copyright (c) 2014 YUKI "Piro" Hiroshi.
* https://github.com/piroor/fxaddonlib-inherit/blob/master/LICENSE
* @url http://github.com/piroor/fxaddonlib-inherit
*/
const EXPORTED_SYMBOLS = ['inherit'];
function toPropertyDescriptors(aProperties) {
var descriptors = {};
Object.keys(aProperties).forEach(function(aProperty) {
var description = Object.getOwnPropertyDescriptor(aProperties, aProperty);
descriptors[aProperty] = description;
});
return descriptors;
}
function inherit(aParent, aExtraProperties) {
if (!Object.create) {
aExtraProperties.__proto__ = aParent;
return aExtraProperties;
}
if (aExtraProperties)
return Object.create(aParent, toPropertyDescriptors(aExtraProperties));
else
return Object.create(aParent);
}

View File

@ -41,6 +41,7 @@ const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://treestyletab-modules/lib/inherit.jsm');
XPCOMUtils.defineLazyGetter(this, 'window', function() {
Cu.import('resource://treestyletab-modules/lib/namespace.jsm');
@ -80,10 +81,9 @@ function TreeStyleTabWindow(aWindow)
XPCOMUtils.defineLazyModuleGetter(aWindow, 'TreeStyleTabBrowser', 'resource://treestyletab-modules/browser.js');
}
TreeStyleTabWindow.prototype = {
TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, {
base : TreeStyleTabBase,
__proto__ : TreeStyleTabBase,
window : null,
document : null,
@ -1772,5 +1772,5 @@ TreeStyleTabWindow.prototype = {
}
}
};
});