Move some Utility functions from Zotero to Z.U.Internals

This commit is contained in:
Adomas Venčkauskas 2018-08-01 16:29:20 +03:00
parent 887913de09
commit e6d5be755a
2 changed files with 51 additions and 44 deletions

View file

@ -1293,6 +1293,54 @@ Zotero.Utilities.Internal = {
.join('\n');
},
/**
* Defines property on the object
* More compact way to do Object.defineProperty
*
* @param {Object} obj Target object
* @param {String} prop Property to be defined
* @param {Object} desc Propery descriptor. If not overriden, "enumerable" is true
* @param {Object} opts Options:
* lazy {Boolean} If true, the _getter_ is intended for late
* initialization of the property. The getter is replaced with a simple
* property once initialized.
*/
defineProperty: function(obj, prop, desc, opts) {
if (typeof prop != 'string') throw new Error("Property must be a string");
var d = { __proto__: null, enumerable: true, configurable: true }; // Enumerable by default
for (let p in desc) {
if (!desc.hasOwnProperty(p)) continue;
d[p] = desc[p];
}
if (opts) {
if (opts.lazy && d.get) {
let getter = d.get;
d.configurable = true; // Make sure we can change the property later
d.get = function() {
let val = getter.call(this);
// Redefine getter on this object as non-writable value
delete d.set;
delete d.get;
d.writable = false;
d.value = val;
Object.defineProperty(this, prop, d);
return val;
}
}
}
Object.defineProperty(obj, prop, d);
},
extendClass: function(superClass, newClass) {
newClass._super = superClass;
newClass.prototype = Object.create(superClass.prototype);
newClass.prototype.constructor = newClass;
},
quitZotero: function(restart=false) {
Zotero.debug("Zotero.Utilities.Internal.quitZotero() is deprecated -- use quit()");

View file

@ -1363,52 +1363,11 @@ Services.scriptloader.loadSubScript("resource://zotero/polyfill.js");
}
/**
* Defines property on the object
* More compact way to do Object.defineProperty
*
* @param {Object} obj Target object
* @param {String} prop Property to be defined
* @param {Object} desc Propery descriptor. If not overriden, "enumerable" is true
* @param {Object} opts Options:
* lazy {Boolean} If true, the _getter_ is intended for late
* initialization of the property. The getter is replaced with a simple
* property once initialized.
* @alias Zotero.Utilities.Internal.defineProperty;
*/
this.defineProperty = function(obj, prop, desc, opts) {
if (typeof prop != 'string') throw new Error("Property must be a string");
var d = { __proto__: null, enumerable: true, configurable: true }; // Enumerable by default
for (let p in desc) {
if (!desc.hasOwnProperty(p)) continue;
d[p] = desc[p];
}
if (opts) {
if (opts.lazy && d.get) {
let getter = d.get;
d.configurable = true; // Make sure we can change the property later
d.get = function() {
let val = getter.call(this);
// Redefine getter on this object as non-writable value
delete d.set;
delete d.get;
d.writable = false;
d.value = val;
Object.defineProperty(this, prop, d);
return val;
}
}
}
Object.defineProperty(obj, prop, d);
}
this.defineProperty = (...args) => Zotero.Utilities.Internal.defineProperty(...args);
this.extendClass = function(superClass, newClass) {
newClass._super = superClass;
newClass.prototype = Object.create(superClass.prototype);
newClass.prototype.constructor = newClass;
}
this.extendClass = (...args) => Zotero.Utilities.Internal.extendClass(...args);
this.getLocaleCollation = function () {
return Zotero.Intl.collation;