2017-04-22 22:49:49 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2017-04-26 22:01:48 +00:00
|
|
|
var o = null // eslint-disable-line no-unused-vars
|
2017-04-22 22:49:49 +00:00
|
|
|
|
2017-04-25 22:21:31 +00:00
|
|
|
function Objects () {
|
|
|
|
this.counter = 0
|
|
|
|
this.container = {}
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Objects.prototype.add = function (category, member) {
|
2017-04-25 22:21:31 +00:00
|
|
|
if (!member) {
|
|
|
|
member = 'o' + this.counter
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
if (!this.has(category)) {
|
2017-04-25 22:21:31 +00:00
|
|
|
this.container[category] = []
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
this.container[category].push({type: category, name: member})
|
|
|
|
++this.counter
|
|
|
|
return this.container[category].slice(-1)[0].name
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.get = function (category, last) {
|
|
|
|
if (!(category in this.container)) {
|
2017-04-25 22:21:31 +00:00
|
|
|
// return {type:null, name:null};
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2017-04-25 22:21:31 +00:00
|
|
|
throw new Error(category + ' is not available.')
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
if (last) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.container[category].slice(-1)[0]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-06-06 05:12:31 +00:00
|
|
|
return random.pick(this.container[category])
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.pick = function (category, last) {
|
|
|
|
try {
|
2017-04-25 22:21:31 +00:00
|
|
|
return this.get(category, last).name
|
2017-04-22 22:49:49 +00:00
|
|
|
} catch (e) {
|
2017-04-25 22:28:31 +00:00
|
|
|
logger.traceback()
|
2017-06-10 04:10:40 +00:00
|
|
|
throw logger.JSError('Error: pick(\'' + category + '\') is undefined.')
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.pop = function (objectName) {
|
2017-05-02 14:09:14 +00:00
|
|
|
let self = this
|
2017-06-06 05:04:43 +00:00
|
|
|
utils.common.getKeysFromHash(this.container).forEach(function (category) {
|
2017-04-22 22:49:49 +00:00
|
|
|
self.container[category].forEach(function (obj) {
|
2017-04-25 15:22:15 +00:00
|
|
|
if (obj.name === objectName) {
|
2017-04-25 22:21:31 +00:00
|
|
|
self.container[category].splice(self.container[category].indexOf(obj), 1)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.contains = function (categoryNames) {
|
2017-05-02 14:09:14 +00:00
|
|
|
let categories = []
|
|
|
|
let self = this
|
2017-04-22 22:49:49 +00:00
|
|
|
categoryNames.forEach(function (name) {
|
|
|
|
if (self.has(name)) {
|
2017-04-25 22:21:31 +00:00
|
|
|
categories.push(name)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
})
|
|
|
|
return (categories.length === 0) ? null : categories
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.show = function (category) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return (category in this.container) ? this.container[category] : this.container
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.count = function (category) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return (category in this.container) ? this.container[category].length : 0
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.has = function (category) {
|
|
|
|
if (category in this.container) {
|
2017-04-25 22:21:31 +00:00
|
|
|
this.check(category)
|
2017-05-02 14:12:49 +00:00
|
|
|
return this.container[category].length > 0
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.valid = function () {
|
2017-05-02 14:09:14 +00:00
|
|
|
let items = []
|
|
|
|
let self = this
|
2017-04-25 22:21:31 +00:00
|
|
|
utils.common.getKeysFromHash(self.container).forEach(function (category) {
|
|
|
|
self.check(category)
|
|
|
|
})
|
|
|
|
utils.common.getKeysFromHash(self.container).forEach(function (category) {
|
2017-05-02 14:09:14 +00:00
|
|
|
for (let i = 0; i < self.container[category].length; i++) {
|
2017-04-25 22:21:31 +00:00
|
|
|
items.push(self.container[category][i].name)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
})
|
|
|
|
return items
|
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
|
|
|
|
Objects.prototype.check = function (category) {
|
2017-05-02 14:09:14 +00:00
|
|
|
let self = this
|
2017-04-22 22:49:49 +00:00
|
|
|
self.container[category].forEach(function (object) {
|
|
|
|
try {
|
2017-04-25 22:21:31 +00:00
|
|
|
let x = /* frame.contentWindow. */ eval(object.name) // eslint-disable-line no-eval
|
2017-04-25 15:22:15 +00:00
|
|
|
if (x === undefined || x === null) {
|
2017-04-25 22:21:31 +00:00
|
|
|
self.pop(object.name)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2017-04-25 22:21:31 +00:00
|
|
|
self.pop(object.name)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
})
|
|
|
|
}
|