2010-06-16 17:19:11 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is utils.js.
|
|
|
|
*
|
2010-08-10 16:20:05 -07:00
|
|
|
* The Initial Developer of the Original Code is the Mozilla Foundation.
|
2010-06-16 17:19:11 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2010-08-10 16:20:05 -07:00
|
|
|
* Aza Raskin <aza@mozilla.com>
|
2010-06-16 17:19:11 -07:00
|
|
|
* Ian Gilman <ian@iangilman.com>
|
2010-06-19 13:58:51 -07:00
|
|
|
* Michael Yoshitaka Erlewine <mitcho@mitcho.com>
|
2011-01-21 15:15:22 -08:00
|
|
|
* Tim Taubert <tim.taubert@gmx.de>
|
2010-06-16 17:19:11 -07:00
|
|
|
*
|
2010-08-02 13:38:29 -07:00
|
|
|
* This file incorporates work from:
|
|
|
|
* jQuery JavaScript Library v1.4.2: http://code.jquery.com/jquery-1.4.2.js
|
|
|
|
* This incorporated work is covered by the following copyright and
|
|
|
|
* permission notice:
|
2010-07-23 14:33:02 -07:00
|
|
|
* Copyright 2010, John Resig
|
|
|
|
* Dual licensed under the MIT or GPL Version 2 licenses.
|
|
|
|
* http://jquery.org/license
|
2010-07-28 22:21:34 -07:00
|
|
|
*
|
2010-06-16 17:19:11 -07:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-07-23 08:07:39 -07:00
|
|
|
"use strict";
|
|
|
|
|
2010-06-16 17:19:11 -07:00
|
|
|
// **********
|
2010-04-16 15:09:23 -07:00
|
|
|
// Title: utils.js
|
2010-06-16 17:19:11 -07:00
|
|
|
|
2011-09-05 22:04:38 -07:00
|
|
|
let EXPORTED_SYMBOLS = ["Point", "Rect", "Range", "Subscribable", "Utils", "MRUList"];
|
2010-02-24 01:47:55 -08:00
|
|
|
|
2010-07-26 17:15:19 -07:00
|
|
|
// #########
|
2010-02-24 01:47:55 -08:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2010-07-22 12:34:13 -07:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ##########
|
2010-04-26 16:48:46 -07:00
|
|
|
// Class: Point
|
2010-07-18 08:58:10 -07:00
|
|
|
// A simple point.
|
2010-04-26 16:48:46 -07:00
|
|
|
//
|
|
|
|
// Constructor: Point
|
2010-07-18 08:58:10 -07:00
|
|
|
// If a is a Point, creates a copy of it. Otherwise, expects a to be x,
|
|
|
|
// and creates a Point with it along with y. If either a or y are omitted,
|
|
|
|
// 0 is used in their place.
|
2010-07-28 22:21:34 -07:00
|
|
|
function Point(a, y) {
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isPoint(a)) {
|
2010-04-26 16:48:46 -07:00
|
|
|
this.x = a.x;
|
|
|
|
this.y = a.y;
|
|
|
|
} else {
|
2010-05-11 12:03:31 -07:00
|
|
|
this.x = (Utils.isNumber(a) ? a : 0);
|
|
|
|
this.y = (Utils.isNumber(y) ? y : 0);
|
2010-04-26 16:48:46 -07:00
|
|
|
}
|
2010-05-11 12:03:31 -07:00
|
|
|
};
|
2010-07-26 17:15:19 -07:00
|
|
|
|
2010-07-28 22:21:34 -07:00
|
|
|
Point.prototype = {
|
2011-02-26 06:28:00 -08:00
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [Point (x,y)] for debug use
|
|
|
|
toString: function Point_toString() {
|
|
|
|
return "[Point (" + this.x + "," + this.y + ")]";
|
|
|
|
},
|
|
|
|
|
2010-07-18 08:58:10 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: distance
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns the distance from this point to the given <Point>.
|
2010-09-08 10:02:08 -07:00
|
|
|
distance: function Point_distance(point) {
|
2010-07-22 12:46:51 -07:00
|
|
|
var ax = this.x - point.x;
|
|
|
|
var ay = this.y - point.y;
|
2010-04-30 17:24:03 -07:00
|
|
|
return Math.sqrt((ax * ax) + (ay * ay));
|
2010-04-16 17:21:03 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-18 08:58:10 -07:00
|
|
|
// ##########
|
2010-04-08 16:36:11 -07:00
|
|
|
// Class: Rect
|
2010-07-18 08:58:10 -07:00
|
|
|
// A simple rectangle. Note that in addition to the left and width, it also has
|
|
|
|
// a right property; changing one affects the others appropriately. Same for the
|
|
|
|
// vertical properties.
|
2010-04-08 16:36:11 -07:00
|
|
|
//
|
|
|
|
// Constructor: Rect
|
2010-07-18 08:58:10 -07:00
|
|
|
// If a is a Rect, creates a copy of it. Otherwise, expects a to be left,
|
|
|
|
// and creates a Rect with it along with top, width, and height.
|
2010-07-28 22:21:34 -07:00
|
|
|
function Rect(a, top, width, height) {
|
2010-04-08 16:36:11 -07:00
|
|
|
// Note: perhaps 'a' should really be called 'rectOrLeft'
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isRect(a)) {
|
2010-03-26 11:34:09 -07:00
|
|
|
this.left = a.left;
|
|
|
|
this.top = a.top;
|
|
|
|
this.width = a.width;
|
|
|
|
this.height = a.height;
|
|
|
|
} else {
|
|
|
|
this.left = a;
|
|
|
|
this.top = top;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
}
|
2010-05-11 12:03:31 -07:00
|
|
|
};
|
|
|
|
|
2010-07-28 22:21:34 -07:00
|
|
|
Rect.prototype = {
|
2011-02-26 06:28:00 -08:00
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [Rect (left,top,width,height)] for debug use
|
|
|
|
toString: function Rect_toString() {
|
|
|
|
return "[Rect (" + this.left + "," + this.top + "," +
|
|
|
|
this.width + "," + this.height + ")]";
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-28 20:33:43 -07:00
|
|
|
get right() this.left + this.width,
|
2010-03-24 16:54:48 -07:00
|
|
|
set right(value) {
|
2010-07-11 16:48:05 -07:00
|
|
|
this.width = value - this.left;
|
2010-03-24 16:54:48 -07:00
|
|
|
},
|
|
|
|
|
2010-07-28 20:33:43 -07:00
|
|
|
get bottom() this.top + this.height,
|
2010-03-24 16:54:48 -07:00
|
|
|
set bottom(value) {
|
2010-07-11 16:48:05 -07:00
|
|
|
this.height = value - this.top;
|
2010-06-19 15:47:26 -07:00
|
|
|
},
|
|
|
|
|
2010-06-23 17:10:16 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Variable: xRange
|
|
|
|
// Gives you a new <Range> for the horizontal dimension.
|
2010-07-28 21:40:55 -07:00
|
|
|
get xRange() new Range(this.left, this.right),
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-23 17:10:16 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Variable: yRange
|
|
|
|
// Gives you a new <Range> for the vertical dimension.
|
2010-07-28 21:40:55 -07:00
|
|
|
get yRange() new Range(this.top, this.bottom),
|
2010-06-19 15:47:26 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: intersects
|
|
|
|
// Returns true if this rectangle intersects the given <Rect>.
|
2010-09-08 10:02:08 -07:00
|
|
|
intersects: function Rect_intersects(rect) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return (rect.right > this.left &&
|
2010-08-11 10:06:49 -07:00
|
|
|
rect.left < this.right &&
|
|
|
|
rect.bottom > this.top &&
|
|
|
|
rect.top < this.bottom);
|
2010-03-25 17:22:45 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-23 17:10:16 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: intersection
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns a new <Rect> with the intersection of this rectangle and the give <Rect>,
|
2010-07-16 17:27:01 -07:00
|
|
|
// or null if they don't intersect.
|
2010-09-08 10:02:08 -07:00
|
|
|
intersection: function Rect_intersection(rect) {
|
2010-06-23 17:10:16 -07:00
|
|
|
var box = new Rect(Math.max(rect.left, this.left), Math.max(rect.top, this.top), 0, 0);
|
|
|
|
box.right = Math.min(rect.right, this.right);
|
|
|
|
box.bottom = Math.min(rect.bottom, this.bottom);
|
2010-07-11 17:54:42 -07:00
|
|
|
if (box.width > 0 && box.height > 0)
|
2010-06-23 17:10:16 -07:00
|
|
|
return box;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-23 17:10:16 -07:00
|
|
|
return null;
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-05-06 16:28:10 -07:00
|
|
|
// ----------
|
|
|
|
// Function: contains
|
2011-01-21 15:15:22 -08:00
|
|
|
// Returns a boolean denoting if the <Rect> or <Point> is contained inside
|
2011-02-14 17:11:25 -08:00
|
|
|
// this rectangle.
|
2010-05-06 16:28:10 -07:00
|
|
|
//
|
2011-01-21 15:15:22 -08:00
|
|
|
// Parameters
|
|
|
|
// - A <Rect> or a <Point>
|
|
|
|
contains: function Rect_contains(a) {
|
|
|
|
if (Utils.isPoint(a))
|
|
|
|
return (a.x > this.left &&
|
|
|
|
a.x < this.right &&
|
|
|
|
a.y > this.top &&
|
|
|
|
a.y < this.bottom);
|
|
|
|
|
|
|
|
return (a.left >= this.left &&
|
|
|
|
a.right <= this.right &&
|
|
|
|
a.top >= this.top &&
|
|
|
|
a.bottom <= this.bottom);
|
2010-05-06 16:28:10 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: center
|
|
|
|
// Returns a new <Point> with the center location of this rectangle.
|
2010-09-08 10:02:08 -07:00
|
|
|
center: function Rect_center() {
|
2010-03-25 17:22:45 -07:00
|
|
|
return new Point(this.left + (this.width / 2), this.top + (this.height / 2));
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-23 17:10:16 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: size
|
|
|
|
// Returns a new <Point> with the dimensions of this rectangle.
|
2010-09-08 10:02:08 -07:00
|
|
|
size: function Rect_size() {
|
2010-06-22 16:42:06 -07:00
|
|
|
return new Point(this.width, this.height);
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-22 16:42:06 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: position
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns a new <Point> with the top left of this rectangle.
|
2010-09-08 10:02:08 -07:00
|
|
|
position: function Rect_position() {
|
2010-06-22 16:42:06 -07:00
|
|
|
return new Point(this.left, this.top);
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-06-23 17:10:16 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: area
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns the area of this rectangle.
|
2010-09-08 10:02:08 -07:00
|
|
|
area: function Rect_area() {
|
2010-06-23 17:10:16 -07:00
|
|
|
return this.width * this.height;
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ----------
|
2010-06-22 16:50:37 -07:00
|
|
|
// Function: inset
|
|
|
|
// Makes the rect smaller (if the arguments are positive) as if a margin is added all around
|
|
|
|
// the initial rect, with the margin widths (symmetric) being specified by the arguments.
|
|
|
|
//
|
|
|
|
// Paramaters
|
|
|
|
// - A <Point> or two arguments: x and y
|
2010-09-08 10:02:08 -07:00
|
|
|
inset: function Rect_inset(a, b) {
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isPoint(a)) {
|
2010-07-18 08:58:10 -07:00
|
|
|
b = a.y;
|
2010-03-26 11:34:09 -07:00
|
|
|
a = a.x;
|
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
this.left += a;
|
|
|
|
this.width -= a * 2;
|
|
|
|
this.top += b;
|
|
|
|
this.height -= b * 2;
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ----------
|
2010-06-22 16:50:37 -07:00
|
|
|
// Function: offset
|
|
|
|
// Moves (translates) the rect by the given vector.
|
|
|
|
//
|
|
|
|
// Paramaters
|
|
|
|
// - A <Point> or two arguments: x and y
|
2010-09-08 10:02:08 -07:00
|
|
|
offset: function Rect_offset(a, b) {
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isPoint(a)) {
|
2010-03-26 11:34:09 -07:00
|
|
|
this.left += a.x;
|
|
|
|
this.top += a.y;
|
|
|
|
} else {
|
|
|
|
this.left += a;
|
|
|
|
this.top += b;
|
|
|
|
}
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: equals
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns true if this rectangle is identical to the given <Rect>.
|
2010-09-08 10:02:08 -07:00
|
|
|
equals: function Rect_equals(rect) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return (rect.left == this.left &&
|
2010-08-11 10:06:49 -07:00
|
|
|
rect.top == this.top &&
|
|
|
|
rect.width == this.width &&
|
|
|
|
rect.height == this.height);
|
2010-04-01 17:19:21 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-05-07 15:49:54 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: union
|
|
|
|
// Returns a new <Rect> with the union of this rectangle and the given <Rect>.
|
2010-09-08 10:02:08 -07:00
|
|
|
union: function Rect_union(a) {
|
2010-04-01 17:19:21 -07:00
|
|
|
var newLeft = Math.min(a.left, this.left);
|
|
|
|
var newTop = Math.min(a.top, this.top);
|
|
|
|
var newWidth = Math.max(a.right, this.right) - newLeft;
|
|
|
|
var newHeight = Math.max(a.bottom, this.bottom) - newTop;
|
2010-07-18 08:58:10 -07:00
|
|
|
var newRect = new Rect(newLeft, newTop, newWidth, newHeight);
|
|
|
|
|
2010-04-01 17:19:21 -07:00
|
|
|
return newRect;
|
2010-04-01 17:59:17 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-04-01 17:20:59 -07:00
|
|
|
// ----------
|
2010-07-16 17:27:01 -07:00
|
|
|
// Function: copy
|
|
|
|
// Copies the values of the given <Rect> into this rectangle.
|
2010-09-08 10:02:08 -07:00
|
|
|
copy: function Rect_copy(a) {
|
2010-04-01 17:20:59 -07:00
|
|
|
this.left = a.left;
|
|
|
|
this.top = a.top;
|
|
|
|
this.width = a.width;
|
|
|
|
this.height = a.height;
|
2010-03-31 17:24:16 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-18 08:58:10 -07:00
|
|
|
// ##########
|
2010-06-19 13:58:51 -07:00
|
|
|
// Class: Range
|
|
|
|
// A physical interval, with a min and max.
|
|
|
|
//
|
|
|
|
// Constructor: Range
|
|
|
|
// Creates a Range with the given min and max
|
2010-07-28 22:21:34 -07:00
|
|
|
function Range(min, max) {
|
2010-07-22 12:46:51 -07:00
|
|
|
if (Utils.isRange(min) && !max) { // if the one variable given is a range, copy it.
|
2010-07-11 16:48:05 -07:00
|
|
|
this.min = min.min;
|
|
|
|
this.max = min.max;
|
|
|
|
} else {
|
|
|
|
this.min = min || 0;
|
|
|
|
this.max = max || 0;
|
|
|
|
}
|
2010-06-19 13:58:51 -07:00
|
|
|
};
|
|
|
|
|
2010-07-28 22:21:34 -07:00
|
|
|
Range.prototype = {
|
2011-02-26 06:28:00 -08:00
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [Range (min,max)] for debug use
|
|
|
|
toString: function Range_toString() {
|
|
|
|
return "[Range (" + this.min + "," + this.max + ")]";
|
|
|
|
},
|
|
|
|
|
2010-07-11 16:48:05 -07:00
|
|
|
// Variable: extent
|
|
|
|
// Equivalent to max-min
|
|
|
|
get extent() {
|
|
|
|
return (this.max - this.min);
|
|
|
|
},
|
2010-06-19 14:37:36 -07:00
|
|
|
|
2010-07-11 16:48:05 -07:00
|
|
|
set extent(extent) {
|
|
|
|
this.max = extent - this.min;
|
|
|
|
},
|
2010-06-19 14:37:36 -07:00
|
|
|
|
2010-06-19 13:58:51 -07:00
|
|
|
// ----------
|
|
|
|
// Function: contains
|
2010-07-22 12:46:51 -07:00
|
|
|
// Whether the <Range> contains the given <Range> or value or not.
|
2010-06-19 13:58:51 -07:00
|
|
|
//
|
2011-10-10 00:04:27 -07:00
|
|
|
// Parameters
|
2010-06-19 14:37:36 -07:00
|
|
|
// - a number or <Range>
|
2010-09-08 10:02:08 -07:00
|
|
|
contains: function Range_contains(value) {
|
2010-08-11 10:06:49 -07:00
|
|
|
if (Utils.isNumber(value))
|
|
|
|
return value >= this.min && value <= this.max;
|
|
|
|
if (Utils.isRange(value))
|
|
|
|
return value.min >= this.min && value.max <= this.max;
|
|
|
|
return false;
|
2010-09-08 18:48:51 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: overlaps
|
2011-10-10 00:04:27 -07:00
|
|
|
// Whether the <Range> overlaps with the given <Range> value or not.
|
2010-09-08 18:48:51 -07:00
|
|
|
//
|
2011-10-10 00:04:27 -07:00
|
|
|
// Parameters
|
2010-09-08 18:48:51 -07:00
|
|
|
// - a number or <Range>
|
2011-10-10 00:04:27 -07:00
|
|
|
overlaps: function Range_overlaps(value) {
|
2010-09-08 18:48:51 -07:00
|
|
|
if (Utils.isNumber(value))
|
|
|
|
return this.contains(value);
|
|
|
|
if (Utils.isRange(value))
|
|
|
|
return !(value.max < this.min || this.max < value.min);
|
|
|
|
return false;
|
2010-06-21 19:32:07 -07:00
|
|
|
},
|
2010-07-22 21:35:07 -07:00
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: proportion
|
|
|
|
// Maps the given value to the range [0,1], so that it returns 0 if the value is <= the min,
|
|
|
|
// returns 1 if the value >= the max, and returns an interpolated "proportion" in (min, max).
|
|
|
|
//
|
2011-10-10 00:04:27 -07:00
|
|
|
// Parameters
|
2010-07-22 21:35:07 -07:00
|
|
|
// - a number
|
|
|
|
// - (bool) smooth? If true, a smooth tanh-based function will be used instead of the linear.
|
2010-09-08 10:02:08 -07:00
|
|
|
proportion: function Range_proportion(value, smooth) {
|
2010-07-22 21:35:07 -07:00
|
|
|
if (value <= this.min)
|
|
|
|
return 0;
|
|
|
|
if (this.max <= value)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
var proportion = (value - this.min) / this.extent;
|
|
|
|
|
|
|
|
if (smooth) {
|
|
|
|
// The ease function ".5+.5*Math.tanh(4*x-2)" is a pretty
|
|
|
|
// little graph. It goes from near 0 at x=0 to near 1 at x=1
|
|
|
|
// smoothly and beautifully.
|
|
|
|
// http://www.wolframalpha.com/input/?i=.5+%2B+.5+*+tanh%28%284+*+x%29+-+2%29
|
2011-07-23 08:07:39 -07:00
|
|
|
let tanh = function tanh(x) {
|
2010-07-22 21:35:07 -07:00
|
|
|
var e = Math.exp(x);
|
|
|
|
return (e - 1/e) / (e + 1/e);
|
2011-07-23 08:07:39 -07:00
|
|
|
};
|
|
|
|
|
2010-07-22 21:35:07 -07:00
|
|
|
return .5 - .5 * tanh(2 - 4 * proportion);
|
|
|
|
}
|
|
|
|
|
|
|
|
return proportion;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: scale
|
|
|
|
// Takes the given value in [0,1] and maps it to the associated value on the Range.
|
|
|
|
//
|
2011-10-10 00:04:27 -07:00
|
|
|
// Parameters
|
2010-07-22 21:35:07 -07:00
|
|
|
// - a number in [0,1]
|
2010-09-08 10:02:08 -07:00
|
|
|
scale: function Range_scale(value) {
|
2010-07-22 21:35:07 -07:00
|
|
|
if (value > 1)
|
|
|
|
value = 1;
|
|
|
|
if (value < 0)
|
|
|
|
value = 0;
|
|
|
|
return this.min + this.extent * value;
|
|
|
|
}
|
2010-06-19 13:58:51 -07:00
|
|
|
};
|
|
|
|
|
2010-03-31 17:24:16 -07:00
|
|
|
// ##########
|
2010-04-16 15:09:23 -07:00
|
|
|
// Class: Subscribable
|
2010-07-18 08:58:10 -07:00
|
|
|
// A mix-in for allowing objects to collect subscribers for custom events.
|
2010-07-28 22:21:34 -07:00
|
|
|
function Subscribable() {
|
2010-07-14 17:24:03 -07:00
|
|
|
this.subscribers = null;
|
2010-03-31 17:24:16 -07:00
|
|
|
};
|
|
|
|
|
2010-07-28 22:21:34 -07:00
|
|
|
Subscribable.prototype = {
|
2010-05-03 15:32:11 -07:00
|
|
|
// ----------
|
|
|
|
// Function: addSubscriber
|
|
|
|
// The given callback will be called when the Subscribable fires the given event.
|
2011-07-14 10:54:25 -07:00
|
|
|
addSubscriber: function Subscribable_addSubscriber(eventName, callback) {
|
2010-07-14 17:24:03 -07:00
|
|
|
try {
|
2010-08-10 11:13:10 -07:00
|
|
|
Utils.assertThrow(typeof callback == "function", "callback must be a function");
|
2010-08-10 16:20:05 -07:00
|
|
|
Utils.assertThrow(eventName && typeof eventName == "string",
|
2010-08-10 11:13:10 -07:00
|
|
|
"eventName must be a non-empty string");
|
2010-08-11 10:06:49 -07:00
|
|
|
} catch(e) {
|
|
|
|
Utils.log(e);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-08-11 10:06:49 -07:00
|
|
|
if (!this.subscribers)
|
|
|
|
this.subscribers = {};
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-08-11 10:06:49 -07:00
|
|
|
if (!this.subscribers[eventName])
|
|
|
|
this.subscribers[eventName] = [];
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2011-07-14 10:54:25 -07:00
|
|
|
let subscribers = this.subscribers[eventName];
|
|
|
|
if (subscribers.indexOf(callback) == -1)
|
|
|
|
subscribers.push(callback);
|
2010-05-03 15:32:11 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-05-03 15:32:11 -07:00
|
|
|
// ----------
|
|
|
|
// Function: removeSubscriber
|
2011-07-14 10:54:25 -07:00
|
|
|
// Removes the subscriber associated with the event for the given callback.
|
|
|
|
removeSubscriber: function Subscribable_removeSubscriber(eventName, callback) {
|
2010-07-14 17:24:03 -07:00
|
|
|
try {
|
2011-07-14 10:54:25 -07:00
|
|
|
Utils.assertThrow(typeof callback == "function", "callback must be a function");
|
2010-08-10 16:20:05 -07:00
|
|
|
Utils.assertThrow(eventName && typeof eventName == "string",
|
2010-08-10 11:13:10 -07:00
|
|
|
"eventName must be a non-empty string");
|
2010-07-14 17:24:03 -07:00
|
|
|
} catch(e) {
|
|
|
|
Utils.log(e);
|
2010-08-11 10:06:49 -07:00
|
|
|
return;
|
2010-07-14 17:24:03 -07:00
|
|
|
}
|
2010-08-11 10:06:49 -07:00
|
|
|
|
|
|
|
if (!this.subscribers || !this.subscribers[eventName])
|
|
|
|
return;
|
|
|
|
|
2011-07-14 10:54:25 -07:00
|
|
|
let subscribers = this.subscribers[eventName];
|
|
|
|
let index = subscribers.indexOf(callback);
|
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
subscribers.splice(index, 1);
|
2010-05-03 15:32:11 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-05-03 15:32:11 -07:00
|
|
|
// ----------
|
|
|
|
// Function: _sendToSubscribers
|
|
|
|
// Internal routine. Used by the Subscribable to fire events.
|
2010-09-08 10:02:08 -07:00
|
|
|
_sendToSubscribers: function Subscribable__sendToSubscribers(eventName, eventInfo) {
|
2010-07-14 17:24:03 -07:00
|
|
|
try {
|
2010-08-10 16:20:05 -07:00
|
|
|
Utils.assertThrow(eventName && typeof eventName == "string",
|
2010-08-10 11:13:10 -07:00
|
|
|
"eventName must be a non-empty string");
|
2010-07-14 17:24:03 -07:00
|
|
|
} catch(e) {
|
|
|
|
Utils.log(e);
|
2010-08-11 10:06:49 -07:00
|
|
|
return;
|
2010-07-14 17:24:03 -07:00
|
|
|
}
|
2010-08-11 10:06:49 -07:00
|
|
|
|
|
|
|
if (!this.subscribers || !this.subscribers[eventName])
|
|
|
|
return;
|
|
|
|
|
2011-07-14 10:54:25 -07:00
|
|
|
let subsCopy = this.subscribers[eventName].concat();
|
|
|
|
subsCopy.forEach(function (callback) {
|
2010-08-11 10:06:49 -07:00
|
|
|
try {
|
2011-07-14 10:54:25 -07:00
|
|
|
callback(this, eventInfo);
|
2010-08-11 10:06:49 -07:00
|
|
|
} catch(e) {
|
|
|
|
Utils.log(e);
|
|
|
|
}
|
|
|
|
}, this);
|
2010-03-24 16:54:48 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-26 11:34:09 -07:00
|
|
|
// ##########
|
2010-05-11 17:22:06 -07:00
|
|
|
// Class: Utils
|
|
|
|
// Singelton with common utility functions.
|
2010-07-28 22:21:34 -07:00
|
|
|
let Utils = {
|
2011-02-26 06:28:00 -08:00
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [Utils] for debug use
|
|
|
|
toString: function Utils_toString() {
|
|
|
|
return "[Utils]";
|
|
|
|
},
|
|
|
|
|
2010-09-10 02:50:14 -07:00
|
|
|
// ___ Logging
|
2010-09-01 13:04:12 -07:00
|
|
|
useConsole: true, // as opposed to dump
|
|
|
|
showTime: false,
|
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: log
|
2010-07-18 08:58:10 -07:00
|
|
|
// Prints the given arguments to the JavaScript error console as a message.
|
|
|
|
// Pass as many arguments as you want, it'll print them all.
|
2010-09-08 10:02:08 -07:00
|
|
|
log: function Utils_log() {
|
2010-03-05 15:14:10 -08:00
|
|
|
var text = this.expandArgumentsForLog(arguments);
|
2010-09-01 13:04:12 -07:00
|
|
|
var prefix = this.showTime ? Date.now() + ': ' : '';
|
|
|
|
if (this.useConsole)
|
|
|
|
Services.console.logStringMessage(prefix + text);
|
|
|
|
else
|
|
|
|
dump(prefix + text + '\n');
|
2010-07-18 08:58:10 -07:00
|
|
|
},
|
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: error
|
2010-07-18 08:58:10 -07:00
|
|
|
// Prints the given arguments to the JavaScript error console as an error.
|
|
|
|
// Pass as many arguments as you want, it'll print them all.
|
2010-09-08 10:02:08 -07:00
|
|
|
error: function Utils_error() {
|
2010-03-05 15:14:10 -08:00
|
|
|
var text = this.expandArgumentsForLog(arguments);
|
2010-09-01 13:04:12 -07:00
|
|
|
var prefix = this.showTime ? Date.now() + ': ' : '';
|
|
|
|
if (this.useConsole)
|
|
|
|
Cu.reportError(prefix + "tabview error: " + text);
|
|
|
|
else
|
|
|
|
dump(prefix + "TABVIEW ERROR: " + text + '\n');
|
2010-07-18 08:58:10 -07:00
|
|
|
},
|
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: trace
|
2010-07-18 08:58:10 -07:00
|
|
|
// Prints the given arguments to the JavaScript error console as a message,
|
|
|
|
// along with a full stack trace.
|
|
|
|
// Pass as many arguments as you want, it'll print them all.
|
2010-09-08 10:02:08 -07:00
|
|
|
trace: function Utils_trace() {
|
2010-03-05 15:14:10 -08:00
|
|
|
var text = this.expandArgumentsForLog(arguments);
|
2011-07-27 21:45:27 -07:00
|
|
|
|
|
|
|
// cut off the first line of the stack trace, because that's just this function.
|
2011-07-27 21:54:48 -07:00
|
|
|
let stack = Error().stack.split("\n").slice(1);
|
2011-07-27 21:45:27 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// if the caller was assert, cut out the line for the assert function as well.
|
2011-07-27 21:45:27 -07:00
|
|
|
if (stack[0].indexOf("Utils_assert(") == 0)
|
|
|
|
stack.splice(0, 1);
|
|
|
|
|
|
|
|
this.log('trace: ' + text + '\n' + stack.join("\n"));
|
2010-07-18 08:58:10 -07:00
|
|
|
},
|
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: assert
|
2010-07-18 08:58:10 -07:00
|
|
|
// Prints a stack trace along with label (as a console message) if condition is false.
|
2010-08-10 11:13:10 -07:00
|
|
|
assert: function Utils_assert(condition, label) {
|
2010-07-11 17:54:42 -07:00
|
|
|
if (!condition) {
|
2010-07-27 09:07:17 -07:00
|
|
|
let text;
|
2010-08-10 16:20:05 -07:00
|
|
|
if (typeof label != 'string')
|
2010-04-30 17:24:03 -07:00
|
|
|
text = 'badly formed assert';
|
|
|
|
else
|
2010-07-29 12:37:25 -07:00
|
|
|
text = "tabview assert: " + label;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-01 17:05:46 -07:00
|
|
|
this.trace(text);
|
2010-03-15 17:15:27 -07:00
|
|
|
}
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
2010-07-07 17:04:01 -07:00
|
|
|
// Function: assertThrow
|
|
|
|
// Throws label as an exception if condition is false.
|
2010-09-08 10:02:08 -07:00
|
|
|
assertThrow: function Utils_assertThrow(condition, label) {
|
2010-07-11 17:54:42 -07:00
|
|
|
if (!condition) {
|
2010-07-27 09:07:17 -07:00
|
|
|
let text;
|
2010-08-10 16:20:05 -07:00
|
|
|
if (typeof label != 'string')
|
2010-07-07 17:04:01 -07:00
|
|
|
text = 'badly formed assert';
|
|
|
|
else
|
2010-07-29 12:37:25 -07:00
|
|
|
text = "tabview assert: " + label;
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2011-07-27 21:45:27 -07:00
|
|
|
// cut off the first line of the stack trace, because that's just this function.
|
2011-07-27 21:54:48 -07:00
|
|
|
let stack = Error().stack.split("\n").slice(1);
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2011-07-27 21:45:27 -07:00
|
|
|
throw text + "\n" + stack.join("\n");
|
2010-07-07 17:04:01 -07:00
|
|
|
}
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: expandObject
|
2010-07-18 08:58:10 -07:00
|
|
|
// Prints the given object to a string, including all of its properties.
|
2010-09-08 10:02:08 -07:00
|
|
|
expandObject: function Utils_expandObject(obj) {
|
2010-07-27 09:07:17 -07:00
|
|
|
var s = obj + ' = {';
|
|
|
|
for (let prop in obj) {
|
|
|
|
let value;
|
|
|
|
try {
|
|
|
|
value = obj[prop];
|
|
|
|
} catch(e) {
|
|
|
|
value = '[!!error retrieving property]';
|
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-27 09:07:17 -07:00
|
|
|
s += prop + ': ';
|
2010-08-10 16:20:05 -07:00
|
|
|
if (typeof value == 'string')
|
2010-07-27 09:07:17 -07:00
|
|
|
s += '\'' + value + '\'';
|
2010-08-10 16:20:05 -07:00
|
|
|
else if (typeof value == 'function')
|
2010-07-27 09:07:17 -07:00
|
|
|
s += 'function';
|
|
|
|
else
|
|
|
|
s += value;
|
2010-03-24 16:54:48 -07:00
|
|
|
|
2010-07-27 09:07:17 -07:00
|
|
|
s += ', ';
|
|
|
|
}
|
|
|
|
return s + '}';
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: expandArgumentsForLog
|
2010-07-18 08:58:10 -07:00
|
|
|
// Expands all of the given args (an array) into a single string.
|
2010-09-08 10:02:08 -07:00
|
|
|
expandArgumentsForLog: function Utils_expandArgumentsForLog(args) {
|
2010-07-27 09:07:17 -07:00
|
|
|
var that = this;
|
|
|
|
return Array.map(args, function(arg) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return typeof arg == 'object' ? that.expandObject(arg) : arg;
|
2010-07-27 09:07:17 -07:00
|
|
|
}).join('; ');
|
2010-03-05 15:14:10 -08:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ___ Misc
|
|
|
|
|
2011-01-25 13:13:21 -08:00
|
|
|
// ----------
|
|
|
|
// Function: isLeftClick
|
|
|
|
// Given a DOM mouse event, returns true if it was for the left mouse button.
|
|
|
|
isLeftClick: function Utils_isLeftClick(event) {
|
|
|
|
return event.button == 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: isMiddleClick
|
|
|
|
// Given a DOM mouse event, returns true if it was for the middle mouse button.
|
|
|
|
isMiddleClick: function Utils_isMiddleClick(event) {
|
|
|
|
return event.button == 1;
|
|
|
|
},
|
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isRightClick
|
|
|
|
// Given a DOM mouse event, returns true if it was for the right mouse button.
|
2010-09-08 10:02:08 -07:00
|
|
|
isRightClick: function Utils_isRightClick(event) {
|
2010-07-27 09:07:17 -07:00
|
|
|
return event.button == 2;
|
2010-03-11 16:14:50 -08:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-16 17:27:01 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isDOMElement
|
|
|
|
// Returns true if the given object is a DOM element.
|
2010-09-08 10:02:08 -07:00
|
|
|
isDOMElement: function Utils_isDOMElement(object) {
|
2010-07-29 13:15:21 -07:00
|
|
|
return object instanceof Ci.nsIDOMElement;
|
2010-05-11 12:03:31 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2012-04-10 11:13:36 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isValidXULTab
|
|
|
|
// A xulTab is valid if it has not been closed,
|
|
|
|
// and it has not been removed from the DOM
|
|
|
|
// Returns true if the tab is valid.
|
|
|
|
isValidXULTab: function Utils_isValidXULTab(xulTab) {
|
|
|
|
return !xulTab.closing && xulTab.parentNode;
|
|
|
|
},
|
|
|
|
|
2010-05-11 12:03:31 -07:00
|
|
|
// ----------
|
2010-05-11 17:22:06 -07:00
|
|
|
// Function: isNumber
|
2010-07-18 08:58:10 -07:00
|
|
|
// Returns true if the argument is a valid number.
|
2010-09-08 10:02:08 -07:00
|
|
|
isNumber: function Utils_isNumber(n) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return typeof n == 'number' && !isNaN(n);
|
2010-05-11 17:22:06 -07:00
|
|
|
},
|
2010-07-22 21:35:07 -07:00
|
|
|
|
2010-07-22 12:46:51 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isRect
|
|
|
|
// Returns true if the given object (r) looks like a <Rect>.
|
2010-09-08 10:02:08 -07:00
|
|
|
isRect: function Utils_isRect(r) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return (r &&
|
2010-08-11 10:06:49 -07:00
|
|
|
this.isNumber(r.left) &&
|
|
|
|
this.isNumber(r.top) &&
|
|
|
|
this.isNumber(r.width) &&
|
|
|
|
this.isNumber(r.height));
|
2010-07-22 12:46:51 -07:00
|
|
|
},
|
2010-07-22 21:35:07 -07:00
|
|
|
|
2010-07-22 12:46:51 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isRange
|
|
|
|
// Returns true if the given object (r) looks like a <Range>.
|
2010-09-08 10:02:08 -07:00
|
|
|
isRange: function Utils_isRange(r) {
|
2010-08-10 16:20:05 -07:00
|
|
|
return (r &&
|
2010-08-11 10:06:49 -07:00
|
|
|
this.isNumber(r.min) &&
|
|
|
|
this.isNumber(r.max));
|
2010-07-22 12:46:51 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: isPoint
|
|
|
|
// Returns true if the given object (p) looks like a <Point>.
|
2010-09-08 10:02:08 -07:00
|
|
|
isPoint: function Utils_isPoint(p) {
|
2010-07-22 12:46:51 -07:00
|
|
|
return (p && this.isNumber(p.x) && this.isNumber(p.y));
|
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isPlainObject
|
|
|
|
// Check to see if an object is a plain object (created using "{}" or "new Object").
|
2010-09-08 10:02:08 -07:00
|
|
|
isPlainObject: function Utils_isPlainObject(obj) {
|
2010-07-23 14:33:02 -07:00
|
|
|
// Must be an Object.
|
|
|
|
// Make sure that DOM nodes and window objects don't pass through, as well
|
2010-08-10 16:20:05 -07:00
|
|
|
if (!obj || Object.prototype.toString.call(obj) !== "[object Object]" ||
|
|
|
|
obj.nodeType || obj.setInterval) {
|
2010-07-23 14:33:02 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not own constructor property must be Object
|
2010-07-26 17:15:19 -07:00
|
|
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-08-10 16:20:05 -07:00
|
|
|
if (obj.constructor &&
|
|
|
|
!hasOwnProperty.call(obj, "constructor") &&
|
|
|
|
!hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) {
|
2010-07-23 14:33:02 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Own properties are enumerated firstly, so to speed up,
|
|
|
|
// if last one is own, then all properties are own.
|
|
|
|
|
|
|
|
var key;
|
2010-07-28 21:40:55 -07:00
|
|
|
for (key in obj) {}
|
2010-07-23 14:33:02 -07:00
|
|
|
|
2010-07-28 21:40:55 -07:00
|
|
|
return key === undefined || hasOwnProperty.call(obj, key);
|
2010-07-23 14:33:02 -07:00
|
|
|
},
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// ----------
|
|
|
|
// Function: isEmptyObject
|
|
|
|
// Returns true if the given object has no members.
|
2010-09-08 10:02:08 -07:00
|
|
|
isEmptyObject: function Utils_isEmptyObject(obj) {
|
2010-07-28 21:40:55 -07:00
|
|
|
for (let name in obj)
|
2010-07-23 14:33:02 -07:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2010-05-11 17:22:06 -07:00
|
|
|
// ----------
|
|
|
|
// Function: copy
|
|
|
|
// Returns a copy of the argument. Note that this is a shallow copy; if the argument
|
|
|
|
// has properties that are themselves objects, those properties will be copied by reference.
|
2010-09-08 10:02:08 -07:00
|
|
|
copy: function Utils_copy(value) {
|
2010-08-10 16:20:05 -07:00
|
|
|
if (value && typeof value == 'object') {
|
2010-07-26 11:02:52 -07:00
|
|
|
if (Array.isArray(value))
|
2010-07-23 14:33:02 -07:00
|
|
|
return this.extend([], value);
|
|
|
|
return this.extend({}, value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// ----------
|
|
|
|
// Function: merge
|
2010-07-29 13:02:51 -07:00
|
|
|
// Merge two array-like objects into the first and return it.
|
2010-09-08 10:02:08 -07:00
|
|
|
merge: function Utils_merge(first, second) {
|
2010-07-29 13:02:51 -07:00
|
|
|
Array.forEach(second, function(el) Array.push(first, el));
|
2010-07-23 14:33:02 -07:00
|
|
|
return first;
|
|
|
|
},
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// ----------
|
|
|
|
// Function: extend
|
|
|
|
// Pass several objects in and it will combine them all into the first object and return it.
|
2010-09-08 10:02:08 -07:00
|
|
|
extend: function Utils_extend() {
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-08-10 16:20:05 -07:00
|
|
|
// copy reference to target object
|
|
|
|
let target = arguments[0] || {};
|
2010-07-23 14:33:02 -07:00
|
|
|
// Deep copy is not supported
|
2010-07-28 21:40:55 -07:00
|
|
|
if (typeof target === "boolean") {
|
2010-08-10 11:13:10 -07:00
|
|
|
this.assert(false, "The first argument of extend cannot be a boolean." +
|
|
|
|
"Deep copy is not supported.");
|
2010-07-23 14:33:02 -07:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Back when this was in iQ + iQ.fn, so you could extend iQ objects with it.
|
|
|
|
// This is no longer supported.
|
2010-08-10 16:20:05 -07:00
|
|
|
let length = arguments.length;
|
2010-07-28 21:40:55 -07:00
|
|
|
if (length === 1) {
|
2010-08-10 11:13:10 -07:00
|
|
|
this.assert(false, "Extending the iQ prototype using extend is not supported.");
|
2010-07-23 14:33:02 -07:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle case when target is a string or something
|
2010-07-23 19:29:32 -07:00
|
|
|
if (typeof target != "object" && typeof target != "function") {
|
2010-07-23 14:33:02 -07:00
|
|
|
target = {};
|
|
|
|
}
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-08-10 16:20:05 -07:00
|
|
|
for (let i = 1; i < length; i++) {
|
2010-07-23 14:33:02 -07:00
|
|
|
// Only deal with non-null/undefined values
|
2010-08-10 16:20:05 -07:00
|
|
|
let options = arguments[i];
|
|
|
|
if (options != null) {
|
2010-07-23 14:33:02 -07:00
|
|
|
// Extend the base object
|
2010-08-10 16:20:05 -07:00
|
|
|
for (let name in options) {
|
|
|
|
let copy = options[name];
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// Prevent never-ending loop
|
2010-07-28 21:40:55 -07:00
|
|
|
if (target === copy)
|
2010-07-23 14:33:02 -07:00
|
|
|
continue;
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-07-28 21:40:55 -07:00
|
|
|
if (copy !== undefined)
|
|
|
|
target[name] = copy;
|
2010-07-23 14:33:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-27 09:07:17 -07:00
|
|
|
|
2010-07-23 14:33:02 -07:00
|
|
|
// Return the modified object
|
|
|
|
return target;
|
2011-08-21 07:29:43 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: attempt
|
|
|
|
// Tries to execute a number of functions. Returns immediately the return
|
|
|
|
// value of the first non-failed function without executing successive
|
|
|
|
// functions, or null.
|
2011-10-10 00:04:27 -07:00
|
|
|
attempt: function Utils_attempt() {
|
2011-08-21 07:29:43 -07:00
|
|
|
let args = arguments;
|
|
|
|
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
|
|
try {
|
|
|
|
return args[i]();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2010-05-11 12:03:31 -07:00
|
|
|
}
|
2010-03-05 15:14:10 -08:00
|
|
|
};
|
2011-09-05 22:04:38 -07:00
|
|
|
|
|
|
|
// ##########
|
|
|
|
// Class: MRUList
|
|
|
|
// A most recently used list.
|
|
|
|
//
|
|
|
|
// Constructor: MRUList
|
|
|
|
// If a is an array of entries, creates a copy of it.
|
|
|
|
function MRUList(a) {
|
|
|
|
if (Array.isArray(a))
|
|
|
|
this._list = a.concat();
|
|
|
|
else
|
|
|
|
this._list = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
MRUList.prototype = {
|
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [List (entry1, entry2, ...)] for debug use
|
|
|
|
toString: function MRUList_toString() {
|
|
|
|
return "[List (" + this._list.join(", ") + ")]";
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: update
|
|
|
|
// Updates/inserts the given entry as the most recently used one in the list.
|
|
|
|
update: function MRUList_update(entry) {
|
|
|
|
this.remove(entry);
|
|
|
|
this._list.unshift(entry);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: remove
|
|
|
|
// Removes the given entry from the list.
|
|
|
|
remove: function MRUList_remove(entry) {
|
|
|
|
let index = this._list.indexOf(entry);
|
|
|
|
if (index > -1)
|
|
|
|
this._list.splice(index, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
|
|
|
// Function: peek
|
|
|
|
// Returns the most recently used entry. If a filter exists, gets the most
|
|
|
|
// recently used entry which matches the filter.
|
|
|
|
peek: function MRUList_peek(filter) {
|
|
|
|
let match = null;
|
|
|
|
if (filter && typeof filter == "function")
|
|
|
|
this._list.some(function MRUList_peek_getEntry(entry) {
|
|
|
|
if (filter(entry)) {
|
|
|
|
match = entry
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
else
|
|
|
|
match = this._list.length > 0 ? this._list[0] : null;
|
|
|
|
|
|
|
|
return match;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|