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

View File

@ -2,8 +2,9 @@ Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, XPCOMUtils.defineLazyModuleGetter(this,
'TreeStyleTabUtils', 'resource://treestyletab-modules/utils.js'); 'TreeStyleTabUtils', 'resource://treestyletab-modules/utils.js');
var TreeStyleTabBookmarksServiceEditable = { (function() {
__proto__ : TreeStyleTabBookmarksService, let { inherit } = Components.utils.import('resource://treestyletab-modules/lib/inherit.jsm', {});
var TreeStyleTabBookmarksServiceEditable = inherit(TreeStyleTabBookmarksService, {
instantApply : false, instantApply : false,
canceled : false, canceled : false,
@ -356,6 +357,9 @@ var TreeStyleTabBookmarksServiceEditable = {
} }
} }
}; });
window.addEventListener('DOMContentLoaded', TreeStyleTabBookmarksServiceEditable, false); 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/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/Timer.jsm'); Cu.import('resource://gre/modules/Timer.jsm');
Cu.import('resource://treestyletab-modules/lib/inherit.jsm');
Cu.import('resource://treestyletab-modules/constants.js'); Cu.import('resource://treestyletab-modules/constants.js');
XPCOMUtils.defineLazyGetter(this, 'window', function() { XPCOMUtils.defineLazyGetter(this, 'window', function() {
@ -83,8 +84,7 @@ else {
this.AeroPeek = null; this.AeroPeek = null;
} }
var TreeStyleTabBase = { var TreeStyleTabBase = inherit(TreeStyleTabConstants, {
__proto__ : TreeStyleTabConstants,
tabsHash : null, tabsHash : null,
inWindowDestoructionProcess : false, inWindowDestoructionProcess : false,
@ -2567,7 +2567,7 @@ var TreeStyleTabBase = {
} }
} }
}; });
TreeStyleTabBase.init(); TreeStyleTabBase.init();

View File

@ -44,6 +44,7 @@ const Ci = Components.interfaces;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import('resource://gre/modules/XPCOMUtils.jsm'); 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, 'Services', 'resource://gre/modules/Services.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'utils', 'resource://treestyletab-modules/utils.js', 'TreeStyleTabUtils'); XPCOMUtils.defineLazyModuleGetter(this, 'utils', 'resource://treestyletab-modules/utils.js', 'TreeStyleTabUtils');
@ -87,8 +88,7 @@ function TreeStyleTabBrowser(aWindowService, aTabBrowser)
this._treeViewEnabled = true; this._treeViewEnabled = true;
} }
TreeStyleTabBrowser.prototype = { TreeStyleTabBrowser.prototype = inherit(TreeStyleTabWindow.prototype, {
__proto__ : TreeStyleTabWindow.prototype,
kMENUITEM_RELOADSUBTREE : 'context-item-reloadTabSubtree', kMENUITEM_RELOADSUBTREE : 'context-item-reloadTabSubtree',
kMENUITEM_RELOADCHILDREN : 'context-item-reloadDescendantTabs', kMENUITEM_RELOADCHILDREN : 'context-item-reloadDescendantTabs',
@ -5279,29 +5279,26 @@ TreeStyleTabBrowser.prototype = {
this.detachTab(tab, aInfo); this.detachTab(tab, aInfo);
if (i == 0) { if (i == 0) {
if (parentTab) { if (parentTab) {
this.attachTabTo(tab, parentTab, { this.attachTabTo(tab, parentTab, inherit(aInfo, {
__proto__ : aInfo,
dontExpand : true, dontExpand : true,
dontMove : true dontMove : true
}); }));
} }
this.collapseExpandSubtree(tab, false); this.collapseExpandSubtree(tab, false);
this.deleteTabValue(tab, this.kSUBTREE_COLLAPSED); this.deleteTabValue(tab, this.kSUBTREE_COLLAPSED);
} }
else { else {
this.attachTabTo(tab, children[0], { this.attachTabTo(tab, children[0], inherit(aInfo, {
__proto__ : aInfo,
dontExpand : true, dontExpand : true,
dontMove : true dontMove : true
}); }));
} }
} }
else if (aInfo.behavior == this.kCLOSE_PARENT_BEHAVIOR_PROMOTE_ALL_CHILDREN && parentTab) { else if (aInfo.behavior == this.kCLOSE_PARENT_BEHAVIOR_PROMOTE_ALL_CHILDREN && parentTab) {
this.attachTabTo(tab, parentTab, { this.attachTabTo(tab, parentTab, inherit(aInfo, {
__proto__ : aInfo,
dontExpand : true, dontExpand : true,
dontMove : true dontMove : true
}); }));
} }
else { // aInfo.behavior == this.kCLOSE_PARENT_BEHAVIOR_SIMPLY_DETACH_ALL_CHILDREN else { // aInfo.behavior == this.kCLOSE_PARENT_BEHAVIOR_SIMPLY_DETACH_ALL_CHILDREN
this.detachTab(tab, aInfo); this.detachTab(tab, aInfo);
@ -6737,5 +6734,5 @@ TreeStyleTabBrowser.prototype = {
delayedShowTabbarForFeedback : function TSTBrowser_delayedShowTabbarForFeedback() { this.autoHide.delayedShowForFeedback(); }, delayedShowTabbarForFeedback : function TSTBrowser_delayedShowTabbarForFeedback() { this.autoHide.delayedShowForFeedback(); },
cancelHideTabbarForFeedback : function TSTBrowser_cancelHideTabbarForFeedback() { this.autoHide.cancelHideForFeedback(); } 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://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/base.js');
Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js'); Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js');
@ -53,8 +54,7 @@ function FullTooltipManager(aOwner)
{ {
this.init(aOwner); this.init(aOwner);
} }
FullTooltipManager.prototype = { FullTooltipManager.prototype = inherit(TreeStyleTabBase, {
__proto__ : TreeStyleTabBase,
kTOOLTIP_MODE_DEFAULT : 0, kTOOLTIP_MODE_DEFAULT : 0,
kTOOLTIP_MODE_COLLAPSED : 1, kTOOLTIP_MODE_COLLAPSED : 1,
@ -477,4 +477,4 @@ FullTooltipManager.prototype = {
range.deleteContents(); range.deleteContents();
range.detach(); range.detach();
} }
}; });

View File

@ -39,6 +39,7 @@ const EXPORTED_SYMBOLS = ['GroupTab'];
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; 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/base.js');
Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js'); Components.utils.import('resource://treestyletab-modules/pseudoTreeBuilder.js');
@ -48,8 +49,7 @@ function GroupTab(aWindow)
this.init(); this.init();
} }
GroupTab.prototype = { GroupTab.prototype = inherit(TreeStyleTabBase, {
__proto__ : TreeStyleTabBase,
initialized : false, initialized : false,
shouldUpdate : false, shouldUpdate : false,
@ -427,4 +427,4 @@ GroupTab.prototype = {
this.checkUpdateTreeNow(); 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; const Cu = Components.utils;
Cu.import('resource://gre/modules/XPCOMUtils.jsm'); Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://treestyletab-modules/lib/inherit.jsm');
XPCOMUtils.defineLazyGetter(this, 'window', function() { XPCOMUtils.defineLazyGetter(this, 'window', function() {
Cu.import('resource://treestyletab-modules/lib/namespace.jsm'); Cu.import('resource://treestyletab-modules/lib/namespace.jsm');
@ -80,10 +81,9 @@ function TreeStyleTabWindow(aWindow)
XPCOMUtils.defineLazyModuleGetter(aWindow, 'TreeStyleTabBrowser', 'resource://treestyletab-modules/browser.js'); XPCOMUtils.defineLazyModuleGetter(aWindow, 'TreeStyleTabBrowser', 'resource://treestyletab-modules/browser.js');
} }
TreeStyleTabWindow.prototype = { TreeStyleTabWindow.prototype = inherit(TreeStyleTabBase, {
base : TreeStyleTabBase, base : TreeStyleTabBase,
__proto__ : TreeStyleTabBase,
window : null, window : null,
document : null, document : null,
@ -1772,5 +1772,5 @@ TreeStyleTabWindow.prototype = {
} }
} }
}; });