1 // ==========================================================================
  2 // Project:   The M-Project - Mobile HTML5 Application Framework
  3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
  4 //            (c) 2011 panacoda GmbH. All rights reserved.
  5 // Creator:   Dominik
  6 // Date:      02.12.2010
  7 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
  8 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
  9 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
 10 // ==========================================================================
 11 
 12 /**
 13  * @class
 14  *
 15  * M.LoaderView is the prototype for a loader a.k.a. activity indicator. This very simple
 16  * view can be used to show the user that something is happening, e.g. while the application
 17  * is waiting for a request to return some data.
 18  *
 19  * @extends M.View
 20  */
 21 M.LoaderView = M.View.extend(
 22 /** @scope M.LoaderView.prototype */ {
 23 
 24     /**
 25      * The type of this object.
 26      *
 27      * @type String
 28      */
 29     type: 'M.LoaderView',
 30 
 31     /**
 32      * This property states whether the loader has already been initialized or not.
 33      *
 34      * @type Boolean
 35      */
 36     isInitialized: NO,
 37 
 38     /**
 39      * This property counts the loader calls to show
 40      *
 41      * @type Number
 42      */
 43     refCount: 0,
 44 
 45     /**
 46      * This property can be used to specify the default title of a loader.
 47      *
 48      * @type String
 49      */
 50     defaultTitle: 'loading',
 51             
 52     /**
 53      * This method initializes the loader by loading it once.
 54      *
 55      * @private 
 56      */
 57     initialize: function() {
 58         if(!this.isInitialized) {
 59             this.refCount = 0;
 60             this.isInitialized = YES;
 61         }
 62     },
 63 
 64     /**
 65      * This method shows the default loader. You can specify the displayed label with the
 66      * title parameter.
 67      *
 68      * @param {String} title The title for this loader.
 69      */
 70     show: function(title) {
 71         this.refCount++;
 72         var title = title && typeof(title) === 'string' ? title : this.defaultTitle;
 73         if(this.refCount == 1){
 74             $.mobile.showPageLoadingMsg();
 75             this.changeTitle(title);
 76 
 77             /* position alert in the center of the possibly scrolled viewport */
 78             var loader = $('.ui-loader');
 79             var screenSize = M.Environment.getSize();
 80             var scrollYOffset = window.pageYOffset;
 81             var loaderHeight = loader.outerHeight();
 82 
 83             var yPos = scrollYOffset + (screenSize[1]/2);
 84             loader.css('top', yPos + 'px');
 85             loader.css('margin-top', '-' + (loaderHeight/2) + 'px');
 86         }
 87     },
 88 
 89     /**
 90      * This method changes the current title.
 91      *
 92      * @param {String} title The title for this loader.
 93      */
 94 
 95     changeTitle: function(title){
 96         $('.ui-loader h1').html(title);
 97     },
 98 
 99     /**
100      * This method hides the loader.
101      *
102      * @param {Boolean} force Determines whether to force the hide of the loader.
103      */
104     hide: function(force) {
105         if(force || this.refCount <= 0) {
106             this.refCount = 0;
107         } else {
108             this.refCount--;
109         }
110         if(this.refCount == 0){
111             $.mobile.hidePageLoadingMsg();
112         }
113     }
114     
115 });