      1 /** This monkey patch lets you setupWidget with an element rather than a name **/
      2 Mojo.Controller.SceneController.prototype.pockets_originalSetupWidget = Mojo.Controller.SceneController.prototype.setupWidget;
      3 Mojo.Controller.SceneController.prototype.setupWidget = function(name, attributes, model) {
      4   if (Object.isElement(name)) {
      5     name = name.identify();
      6   }
      7   return Mojo.Controller.SceneController.prototype.pockets_originalSetupWidget.call(this, name, attributes, model);
      8 };
      9
     10
     11 /** This monkey patch lets you call showAlertDialog on a StageController **/
     12
     13 Mojo.Controller.StageController.prototype.showAlertDialog = Mojo.Controller.SceneController.prototype.showAlertDialog;
     14
     15 Mojo.Controller.StageController.prototype.showFrameworkDialog = Mojo.Controller.SceneController.prototype.showFrameworkDialog;
     16
     17 Mojo.Controller.StageController.prototype.createDynamicWidget = function createDynamicWidget(widgetType, model, insertBefore) {
     18 	if(insertBefore === undefined)
     19 		{insertBefore = null;}
     20
     21 	var element = this.document.createElement('div');
     22 	element.setAttribute('x-mojo-element', widgetType);
     23 	this.document.body.insertBefore(element, insertBefore);
     24
     25   var genericAssistant = Class.create({});
     26   var sceneArguments = {
     27     name: "generic",
     28     disableSceneScroller: false,
     29     assistantConstructor: genericAssistant
     30   };
     31   var sceneController = new Mojo.Controller.SceneController(this, pockets.dom.create('div'), sceneArguments, []);
     32   return new Mojo.Controller.WidgetController(element, sceneController, model);
     33 };
     34
     35 Mojo.Controller.StageController.prototype.pockets_originalSwapScene = Mojo.Controller.StageController.prototype.swapScene;
     36 Mojo.Controller.StageController.prototype.swapScene = function() {
     37   if (typeof arguments[0] == 'string') {
     38     arguments[0] = {
     39       name: arguments[0],
     40       transition: Mojo.Transition.crossFade
     41     };
     42   }
     43
     44   return this.pockets_originalSwapScene.apply(this, arguments);
     45 };
     46
     47 Mojo.Controller.StageController.prototype.swapToRoot = function(sceneArguments) {
     48   if (typeof sceneArguments == 'string') {
     49     sceneArguments = {
     50       name: sceneArguments,
     51       transition: Mojo.Transition.crossFade
     52     };
     53   }
     54
     55   Mojo.Timing.resetSceneTiming(this.window);
     56   Mojo.Timing.resume('scene#total');
     57   var myArguments;
     58
     59   myArguments = $A(arguments);
     60   myArguments.shift(); // drop sceneArguments
     61   this._deferSceneOperation(this._syncPushOperation.bind(this, 'swapToRoot', sceneArguments, myArguments), false, sceneArguments);
     62 };
     63
     64 Mojo.SceneStack.prototype.swapToRoot = function(sceneController) {
     65   this.popScenesTo();
     66   this._addScene(sceneController);
     67 };
     68
     69
     70 Mojo.Service.Request.enableLogging = false;
     71 Mojo.Service.Request.requestId_ = 0;
     72
     73 Mojo.Service.Request.prototype.pockets_originalRequest = Mojo.Service.Request.prototype.request;
     74 Mojo.Service.Request.prototype.request = function(url, options) {
     75   var requestId = Mojo.Service.Request.requestId_++;
     76
     77   var identifier = '#' + requestId + ' Mojo.Service.Request("' + url + '/' + options.method + '", ' + Object.toJSON(options.parameters) + ')';
     78
     79   if (Mojo.Service.Request.enableLogging) {
     80     Mojo.Log.info('request: ' + identifier);
     81   }
     82
     83   function tryCatchLog(handlerName, originalHandler) {
     84     return function() {
     85       if (Mojo.Service.Request.enableLogging) {
     86         Mojo.Log.info('response: ' + identifier + ' -> ' + handlerName + '(' + Object.toJSON($A(arguments)) + ')');
     87       }
     88
     89       if (!originalHandler) {return;}
     90
     91       try {
     92         return originalHandler.apply(this, arguments);
     93       } catch(e) {
     94         Mojo.Log.error('Error performing ' + handlerName + ' for ' + identifier + ':');
     95         Mojo.Log.error(e);
     96       }
     97     };
     98   }
     99
    100   this.success = tryCatchLog('onSuccess', this.success);
    101   this.onFailure = tryCatchLog('onFailure', this.onFailure);
    102   this.complete = tryCatchLog('onComplete', this.complete);
    103
    104   try {
    105     return this.pockets_originalRequest.apply(this, arguments);
    106   } catch(e) {
    107     Mojo.Log.error('Error making service request ' + identifier + ': ' + e);
    108   }
    109 };