From e6d5be755af2283daad0d09a294547aac025488d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Wed, 1 Aug 2018 16:29:20 +0300 Subject: [PATCH] Move some Utility functions from Zotero to Z.U.Internals --- .../zotero/xpcom/utilities_internal.js | 48 +++++++++++++++++++ chrome/content/zotero/xpcom/zotero.js | 47 ++---------------- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 60afa71948..7915e7f310 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -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()"); diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 56bd8f217c..fac3038f4a 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -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;