[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/webroot/rsrc/js/application/aphlict/ -> Aphlict.js (source)

   1  /**
   2   * @provides javelin-aphlict
   3   * @requires javelin-install
   4   *           javelin-util
   5   */
   6  
   7  /**
   8   * Simple JS API for the Flash Aphlict client. Example usage:
   9   *
  10   *   var aphlict = new JX.Aphlict('aphlict_swf', '127.0.0.1', 22280)
  11   *     .setHandler(function(type, message) {
  12   *       JX.log("Got " + type + " event!")
  13   *     })
  14   *     .start();
  15   *
  16   * Your handler will receive these events:
  17   *
  18   *  - `connect` The client initiated a connection to the server.
  19   *  - `connected` The client completed a connection to the server.
  20   *  - `close` The client disconnected from the server.
  21   *  - `error` There was an error.
  22   *  - `receive` Received a message from the server.
  23   *
  24   * You do not have to handle any of them in any specific way.
  25   */
  26  JX.install('Aphlict', {
  27  
  28    construct: function(id, server, port, subscriptions) {
  29      if (__DEV__) {
  30        if (JX.Aphlict._instance) {
  31          JX.$E('Aphlict object is a singleton.');
  32        }
  33      }
  34  
  35      this._id = id;
  36      this._server = server;
  37      this._port = port;
  38      this._subscriptions = subscriptions;
  39      this._setStatus('setup');
  40  
  41      JX.Aphlict._instance = this;
  42    },
  43  
  44    events: ['didChangeStatus'],
  45  
  46    members: {
  47      _id: null,
  48      _server: null,
  49      _port: null,
  50      _subscriptions: null,
  51      _status: null,
  52      _statusCode: null,
  53  
  54      start: function(node, uri) {
  55        this._setStatus('start');
  56  
  57        // NOTE: This is grotesque, but seems to work everywhere.
  58        node.innerHTML =
  59          '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">' +
  60            '<param name="movie" value="' + uri + '" />' +
  61            '<param name="allowScriptAccess" value="always" />' +
  62            '<param name="wmode" value="opaque" />' +
  63            '<embed src="' + uri + '" wmode="opaque"' +
  64              'width="0" height="0" id="' + this._id + '">' +
  65            '</embed>' +
  66          '</object>';
  67      },
  68  
  69      _didStartFlash: function() {
  70        var id = this._id;
  71  
  72        // Flash puts its "objects" into global scope in an inconsistent way,
  73        // because it was written in like 1816 when globals were awesome and IE4
  74        // didn't support other scopes since global scope is the best anyway.
  75        var container = document[id] || window[id];
  76  
  77        this._flashContainer = container;
  78        this._flashContainer.connect(
  79          this._server,
  80          this._port,
  81          this._subscriptions);
  82      },
  83  
  84      getStatus: function() {
  85        return this._status;
  86      },
  87  
  88      getStatusCode: function() {
  89        return this._statusCode;
  90      },
  91  
  92      _setStatus: function(status, code) {
  93        this._status = status;
  94        this._statusCode = code || null;
  95        this.invoke('didChangeStatus');
  96      }
  97  
  98    },
  99  
 100    properties: {
 101      handler: null
 102    },
 103  
 104    statics: {
 105      _instance: null,
 106  
 107      getInstance: function() {
 108        var self = JX.Aphlict;
 109        if (!self._instance) {
 110          return null;
 111        }
 112        return self._instance;
 113      },
 114  
 115      didReceiveEvent: function(type, message) {
 116        var client = JX.Aphlict.getInstance();
 117        if (!client) {
 118          return;
 119        }
 120  
 121        if (type == 'status') {
 122          client._setStatus(message.type, message.code);
 123          switch (message.type) {
 124            case 'ready':
 125              client._didStartFlash();
 126              break;
 127          }
 128        }
 129  
 130        var handler = client.getHandler();
 131        if (handler) {
 132          handler(type, message);
 133        }
 134      }
 135    }
 136  
 137  });


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1