Utils.js

Summary

No overview generated for 'Utils.js'


Class Summary
TKPoint  
TKRect  
TKUtils  

Method Summary
static void debug(msg)
          

const KEYBOARD_BACKSPACE = 8;
const KEYBOARD_LEFT = 37;
const KEYBOARD_RIGHT = 39;
const KEYBOARD_UP = 38;
const KEYBOARD_DOWN = 40;
const KEYBOARD_RETURN = 13;

/**
 *  Copyright © 2009 Apple Inc.  All rights reserved.
 *
 *  @class
 *  @name TKUtils
 *
 *  @since TuneKit 1.0
 */

function TKUtils () {
};

/* ==================== TRANSFORMS SHORTHANDS ==================== */

/**
 *  Prints a <code>translate3d()</code> command that can be used as input for a <code>-webkit-transform</code> property.
 *  
 *  @param {int} tx The x coordinate for the translation.
 *  @param {int} ty The y coordinate for the translation
 *
 *  @returns {String} The <code>translate3d()</code> command
 */
TKUtils.t = function (tx, ty) {
  return 'translate3d(' + tx + 'px, ' + ty + 'px, 0)';
};

/**
 *  Creates a CSS string representation for a number in pixels.
 *  
 *  @param {number} value The value to be converted.
 *
 *  @returns {String} A CSS string representation for <code>value</code> in pixels.
 */
TKUtils.px = function (value) {
  return value + 'px';
};

/* ==================== Array ==================== */

/**
 *  Copies all properties from one object onto another.
 *  
 *  @param {Object} sourceObject The object from which we will copy properties.
 *  @param {Object} targetObject The array onto which we will copy properties.
 */
TKUtils.copyPropertiesFromSourceToTarget = function (source, target) {
  for (var property in source) {
    target[property] = source[property];
  }
};

/* ==================== Delegates ==================== */

/**
 *  Indicates whether an object is a <code>Function</code>.
 *  
 *  @param {Object} object The object purported to be a <code>Function</code>.
 *
 *  @returns {bool} Whether the object is a <code>Function</code>.
 */
TKUtils.objectIsFunction = function (object) {
  return (typeof object == 'function');
};

/**
 *  Indicates whether an object is <code>undefined</code>.
 *  
 *  @param {Object} object The object purported to be <code>undefined</code>.
 *
 *  @returns {bool} Whether the object is <code>undefined</code>.
 */
TKUtils.objectIsUndefined = function (object) {
  return (object === undefined);
};

/**
 *  Indicates whether an object is a string literal or a <code>String</code> instance.
 *  
 *  @param {Object} object The object purported to be a string literal or a <code>String</code> instance.
 *
 *  @returns {bool} Whether the object is a string literal or a <code>String</code> instance.
 */
TKUtils.objectIsString = function (object) {
  return (typeof object == 'string' || object instanceof String);
};

/**
 *  Indicates whether an object is an <code>Array</code>.
 *  
 *  @param {Object} object The object purported to be an <code>Array</code>.
 *
 *  @returns {bool} Whether the object is an <code>Array</code>.
 */
TKUtils.objectIsArray = function (object) {
  return (object instanceof Array);
};

/**
 *  Indicates whether an object implements a given method, useful to check if a delegate
 *  object implements a given delegate method.
 *  
 *  @param {Object} object The object purported to implement a given method.
 *  @param {String} methodNameAsString The method name as a <code>String</code>.
 *
 *  @returns {bool} Whether the object implements the given method.
 */
TKUtils.objectHasMethod = function (object, methodNameAsString) {
  return (  object !== null &&
            !this.objectIsUndefined(object) &&
            !this.objectIsUndefined(object[methodNameAsString]) &&
            this.objectIsFunction(object[methodNameAsString])
         );
};

/* ==================== INIT ==================== */

/**
 *  Sets up the .displayNames for all functions defined on the specified class, including its prototype.
 *  
 *  @param {Object} class The class.
 *  @param {String} className The class name as a string, in case it can not be derived from <code>class</code>. Optional.
 */
TKUtils.setupDisplayNames = function (object, className) {
  var class_name = className || object.name;
  for (var i in object) {
    // make sure we don't touch properties that were synthetized
    if (object.__lookupGetter__(i)) {
      continue;
    }
    var prop = object[i];
    if (TKUtils.objectIsFunction(prop)) {
      prop.displayName = TKUtils.createDisplayName(class_name, i);
    }
  }
  for (var i in object.prototype) {
    // make sure we don't touch properties that were synthetized
    if (object.prototype.__lookupGetter__(i)) {
      continue;
    }
    var prop = object.prototype[i];
    if (TKUtils.objectIsFunction(prop)) {
      prop.displayName = TKUtils.createDisplayName(class_name, i);
    }
  }
};

TKUtils.createDisplayName = function (className, methodName) {
  return className + '.' + methodName + '()';
};

TKUtils.buildElement = function (elementData) {
  // nothing to do if we don't have useful data
  if (!elementData || !elementData.type) {
    return null;
  }
  //
  var element = null;
  switch (elementData.type) {
    case "emptyDiv":
      element = document.createElement("div");
      break;
    case "container":
      element = document.createElement("div");
      for (var i=0; i < elementData.children.length; i++) {
        element.appendChild(TKUtils.buildElement(elementData.children[i]));
      }
      break;
    case "image":
      element = document.createElement("img");
      element.src = elementData.src;
      break;
    case "text":
      element = document.createElement("div");
      var p = document.createElement("p");
      p.innerText = elementData.text;
      element.appendChild(p);
      break;
    default:
      element = document.createElement(elementData.type);
      element.innerHTML = elementData.content;
  }
  // add optional id 
  if (elementData.id) {
    element.id = elementData.id;
  }
  // add optional class 
  if (elementData.className) {
    element.className = elementData.className;
  }
  
  // wrap in optional link
  if (elementData.link){
    var subElement = element;
    element = document.createElement("a");
    element.href = elementData.link;
    element.target = "_blank";
    element.appendChild(subElement);
  }
  
  return element;
};

TKUtils.createEvent = function (eventType, relatedTarget) {
  var event = document.createEvent('Event');
  event.initEvent(eventType, true, true);
  event.relatedTarget = relatedTarget;
  return event;
};

TKUtils.isNodeChildOfOtherNode = function (childNode, allegedParentNode) {
  var node = childNode.parentNode;
  while (node !== null) {
    if (node === allegedParentNode) {
      return true;
      break;
    }
    node = node.parentNode;
  }
  return false;
};

TKUtils.setupDisplayNames(TKUtils, 'TKUtils');

/* ==================== TKRect ==================== */

const TKRectTopLeftCorner = 0;
const TKRectMiddleOfTopEdge = 1;
const TKRectTopRightCorner = 2;
const TKRectMiddleOfRightEdge = 3;
const TKRectBottomRightCorner = 4;
const TKRectMiddleOfBottomEdge = 5;
const TKRectBottomLeftCorner = 6;
const TKRectMiddleOfLeftEdge = 7;
const TKRectCenter = 8;

function TKRect (x, y, width, height) {
  this.x = x || 0;
  this.y = y || 0;
  this.width = width || 0;
  this.height = height || 0;
};

TKRect.prototype.pointAtPosition = function (index) {
  var point;
  if (index == TKRectTopLeftCorner) {
    point = new TKPoint(this.x, this.y);
  }
  else if (index == TKRectMiddleOfTopEdge) {
    point = new TKPoint(this.x + this.width / 2, this.y);
  }
  else if (index == TKRectTopRightCorner) {
    point = new TKPoint(this.x + this.width, this.y);
  }
  else if (index == TKRectMiddleOfRightEdge) {
    point = new TKPoint(this.x + this.width, this.y + this.height / 2);
  }
  else if (index == TKRectBottomRightCorner) {
    point = new TKPoint(this.x + this.width, this.y + this.height);
  }
  else if (index == TKRectMiddleOfBottomEdge) {
    point = new TKPoint(this.x + this.width / 2, this.y + this.height);
  }
  else if (index == TKRectBottomLeftCorner) {
    point = new TKPoint(this.x, this.y + this.height);
  }
  else if (index == TKRectMiddleOfLeftEdge) {
    point = new TKPoint(this.x, this.y + this.height / 2);
  }
  else if (index == TKRectCenter) {
    point = new TKPoint(this.x + this.width / 2, this.y + this.height / 2);
  }
  return point;
};

TKRect.rectFromClientRect = function (rect) {
  return new TKRect(rect.left, rect.top, rect.width, rect.height);
};

TKRect.rectFromUnionOfRects = function (rects) {
  if (rects.length < 1) {
    return new TKRect();
  }
  var union = rects[0];
  var rect;
  for (var i = 1; i < rects.length; i++) {
    rect = rects[i];
    union.x = Math.min(union.x, rect.x);
    union.y = Math.min(union.y, rect.y);
    union.width = Math.max(union.width, rect.x + rect.width);
    union.height = Math.max(union.height, rect.y + rect.height);
  }
  return union;
};

/* ==================== TKPoint ==================== */

function TKPoint (x, y) {
  this.x = x || 0;
  this.y = y || 0;
};

TKPoint.prototype.distanceToPoint = function (aPoint) {
  return Math.sqrt(Math.pow(aPoint.x - this.x, 2) + Math.pow(aPoint.y - this.y, 2));
};

/* ==================== TKFocusManager ==================== */

const TKFocusManagerFocusCSSClass = 'tk-focused';

var TKFocusManager = {
  focusedElement: null
};

TKFocusManager.focusElement = function (element) {
  // nothing to do if we're already focusing this element
  if (element === this.focusedElement) {
    return;
  }
  // otherwise, let's see if we care to give this element focus
  if (element._controller !== undefined && element._controller.highlightedElement) {
    element = element._controller.highlightedElement;
  }
  // remove focus from previous element
  if (this.focusedElement !== null) {
    this.focusedElement.removeClassName(TKFocusManagerFocusCSSClass);
  }
  // add focus to the provided element
  if (element !== null) {
    this.focusedElement = element;
    this.focusedElement.addClassName(TKFocusManagerFocusCSSClass);
  }
};

/* ================= Debugging ======================== */

var DEBUG = false;

function debug(msg) {
  if (window.DEBUG !== undefined && window.DEBUG) {
    console.log("DEBUG: " + msg);
  }
};


Documentation generated by JSDoc on Tue Sep 15 21:24:36 2009