      1 // ==========================================================================
      2 // Project:   The M-Project - Mobile HTML5 Application Framework
      3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
      4 // Creator:   Dominik
      5 // Date:      23.11.2010
      6 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
      7 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
      8 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
      9 // ==========================================================================
     10
     11 /**
     12  * @classx
     13  *
     14  * This is the prototype of any dialog view. It is responsible for showing and later
     15  * hiding a dialog.
     16  *
     17  * @extends M.View
     18  */
     19 M.DialogView = M.View.extend(
     20 /** @scope M.DialogView.prototype */ {
     21
     22     /**
     23      * The type of this object.
     24      *
     25      * @type String
     26      */
     27     type: 'M.DialogView',
     28
     29     /**
     30      * Contains the ids of the button's within a dialog. They are used to later register a click
     31      * event to all of these views.
     32      *
     33      * @type Array
     34      */
     35     buttonIds: [],
     36
     37     /**
     38      * The dialog's callback, split in target / action. It is called once the dialog's closing
     39      * transition did finish.
     40      *
     41      * @type Object
     42      */
     43     callback: {},
     44
     45     /**
     46      * This method controls the process of bringing a dialog to the screen.
     47      *
     48      * @private
     49      */
     50     show: function() {
     51
     52         /* register the onHide event for this dialog */
     53         $('#' + this.id).live('pagehide', this.bindToCaller(this, this.dialogDidHide));
     54
     55         /* call the dialog's render() */
     56         this.render();
     57
     58         /* theme it */
     59         this.theme();
     60
     61         /* register the buttons at the event dispatcher */
     62         for(var i in this.buttonIds) {
     63             M.Application.eventDispatcher.registerEvents(this.buttonIds[i], 'click');
     64         }
     65
     66         /* finally show the dialog on the screen */
     67         M.Controller.switchToPage(this, this.transition, NO, NO);
     68
     69     },
     70
     71     /**
     72      * This method triggers the styling of the dialog and its sub views.
     73      *
     74      * @private
     75      */
     76     theme: function() {
     77         $('#' + this.id).page();
     78     },
     79
     80     /**
     81      * This method creates an alert dialog based on the given customizing parameters and
     82      * initiates its displaying on the screen.
     83      *
     84      * @param {Object} obj The customizing parameters of the alert dialog view.
     85      */
     86     alert: function(obj) {
     87         M.AlertDialogView.design(obj).show();
     88     },
     89
     90     /**
     91      * This method creates an confirm dialog based on the given customizing parameters and
     92      * initiates its displaying on the screen.
     93      *
     94      * @param {Object} obj The customizing parameters of the confirm dialog view.
     95      */
     96     confirm: function(obj) {
     97         M.ConfirmDialogView.design(obj).show();
     98     },
     99
    100     /**
    101      * This method creates an actionSheet dialog based on the given customizing parameters and
    102      * initiates its displaying on the screen.
    103      *
    104      * @param {Object} obj The customizing parameters of the actionSheet dialog view.
    105      */
    106     actionSheet: function(obj) {
    107          M.ActionSheetDialogView.design(obj).show();
    108     },
    109
    110     /**
    111      * This method is automatically called right before the dialog will be closed. It is
    112      * used to store the dialogs specified callbacks, if specified.
    113      *
    114      * @private
    115      * @param {String} id The id of the dialog.
    116      */
    117     dialogWillClose: function(id) {
    118         var button = M.ViewManager.getViewById(id);
    119         if(this[button.role] && this[button.role].target && this[button.role].action) {
    120             this.callback.target = this[button.role].target;
    121             this.callback.action = this[button.role].action;
    122         } else if (this.buttons && this.buttons[button.role] && this.buttons[button.role].target && this.buttons[button.role].action) {
    123             this.callback.target = this.buttons[button.role].target;
    124             this.callback.action = this.buttons[button.role].action;
    125         }
    126     },
    127
    128     /**
    129      * This method is automatically called right after the dialog was closed. It is used to
    130      * call the dialog's specified callback and to destroy it by calling M.Object's destroy
    131      * method.
    132      *
    133      * @private
    134      */
    135     dialogDidHide: function() {
    136         if(this) {
    137             if(this.callback && this.callback.target && this.callback.action) {
    138                 this.callback.target[this.callback.action]();
    139             }
    140             this.destroy();
    141         }
    142     }
    143
    144 });