home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9675 / 9675.xpi / chrome / content / simpletimer-sliderAlert.js < prev    next >
Encoding:
JavaScript  |  2009-08-03  |  7.4 KB  |  204 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Scott MacGregor <mscott@netscape.com>
  23.  *   Jens Bannmann <jens.b@web.de>
  24.  *   George Bradt
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. var SimpleTimerSliderAlert = {
  41.     sliderQueue: [],
  42.     application: null,
  43.     finalHeight: 0,
  44.     slideIncrement: 1,
  45.     slideTime: 10,
  46.     openTime: 0, // total time the alert should stay up once we are done animating.
  47.  
  48.     // Entry, message to be displayed is passed in and added to the queue.
  49.     // Use a queue to prevent overlapping windows.
  50.  
  51.     addMessageToQueue: function(msg) {
  52.         SimpleTimerSliderAlert.application = Components.classes["@mozilla.org/fuel/application;1"].
  53.                 getService(Components.interfaces.fuelIApplication);
  54.  
  55.         if ( !SimpleTimerSliderAlert.application.storage.has("sliderQueue") ) {
  56.             SimpleTimerSliderAlert.setSliderQueue();
  57.         }
  58.  
  59.         SimpleTimerSliderAlert.getSliderQueue();
  60.         SimpleTimerSliderAlert.sliderQueue.push(msg);
  61.         SimpleTimerSliderAlert.setSliderQueue();
  62.  
  63.         if ( SimpleTimerSliderAlert.sliderQueue.length === 1 ) {
  64.             // If length > 1, an alert already in progress.
  65.             SimpleTimerSliderAlert.openSliderWindow();
  66.         }
  67.     },
  68.  
  69.     // Custom slider alert. The message is displayed in a small window that slides up
  70.     // from the bottom of the screen, holds there for a few seconds,
  71.     // then slides down. Allows multi-line messages.
  72.  
  73.     openSliderWindow: function() {
  74.         window.openDialog(
  75.                 "chrome://simpletimer/content/simpletimer-sliderAlert.xul",
  76.                 "",
  77.                 "chrome, dialog, titlebar=no, popup");
  78.     },
  79.  
  80.     // Build the window.
  81.  
  82.     prefillAlertInfo: function() {
  83.         var strbundle = document.getElementById("simtim-strings");
  84.         var rowNode, descrNodeTime, descrNodeDescription;
  85.  
  86.         this.application = Components.classes["@mozilla.org/fuel/application;1"].
  87.                 getService(Components.interfaces.fuelIApplication);
  88.  
  89.         this.getSliderQueue();
  90.  
  91.         var displayItems = this.sliderQueue[0].displayItems;
  92.         document.getElementById("simtim-descMessage").value = this.sliderQueue[0].msg;
  93.  
  94.         var parent = document.getElementById("simtim-gridItems");
  95.         var oldRowsNode = parent.lastChild;
  96.         var newRowsNode = document.createElement("rows");
  97.  
  98.         if ( displayItems !== null ) {
  99.             for ( var i in displayItems ) {
  100.  
  101.                 rowNode = document.createElement("row");
  102.  
  103.                 descrNodeTime = document.createElement("description");
  104.                 descrNodeTime.setAttribute("value", displayItems[i].timeDisplay);
  105.                 rowNode.appendChild(descrNodeTime);
  106.  
  107.                 descrNodeDescription = document.createElement("description");
  108.                 descrNodeDescription.setAttribute("value", displayItems[i].description);
  109.                 rowNode.appendChild(descrNodeDescription);
  110.  
  111.                 newRowsNode.appendChild(rowNode);
  112.  
  113.                 if ( i === "7" && displayItems.length > 8 ) {
  114.                     // Eight items should be enough.
  115.                     rowNode = document.createElement("row");
  116.  
  117.                     descrNodeTime = document.createElement("description");
  118.                     descrNodeTime.setAttribute("value", strbundle.getString("msg.etc"));
  119.                     rowNode.appendChild(descrNodeTime);
  120.  
  121.                     newRowsNode.appendChild(rowNode);
  122.                     break;
  123.                 }
  124.             }
  125.         }
  126.  
  127.         parent.replaceChild(newRowsNode, oldRowsNode);
  128.     },
  129.  
  130.     // Size and position window.
  131.  
  132.     onAlertLoad: function() {
  133.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  134.                 getService(Components.interfaces.nsIPrefService).
  135.                 getBranch("extensions.simpletimer@grbradt.org.");
  136.         var alertSliderMaxHeight;
  137.  
  138.         sizeToContent();
  139.  
  140.         this.finalHeight = window.outerHeight;
  141.         window.outerHeight = 1;
  142.  
  143.         // Be sure to offset the alert by 10 pixels from the far right edge of the screen.
  144.         window.moveTo( (screen.availLeft + screen.availWidth - window.outerWidth) - 10,
  145.                         screen.availTop + screen.availHeight - window.outerHeight);
  146.  
  147.         setTimeout("SimpleTimerSliderAlert.animateAlert()", this.slideTime);
  148.     },
  149.  
  150.     // Moving the window up.
  151.  
  152.     animateAlert: function() {
  153.         if ( window.outerHeight < this.finalHeight ) {
  154.             window.screenY -= this.slideIncrement;
  155.             window.outerHeight += this.slideIncrement;
  156.             setTimeout("SimpleTimerSliderAlert.animateAlert()", this.slideTime);
  157.         }
  158.         else {
  159.             setTimeout("SimpleTimerSliderAlert.closeAlert()", 3000);
  160.         }
  161.     },
  162.  
  163.     // Moving the window down.
  164.  
  165.     closeAlert: function() {
  166.         if ( window.outerHeight > 1 ) {
  167.             window.screenY += this.slideIncrement;
  168.             window.outerHeight -= this.slideIncrement;
  169.             setTimeout("SimpleTimerSliderAlert.closeAlert()", this.slideTime);
  170.         }
  171.         else {
  172.             this.getSliderQueue();
  173.             this.sliderQueue.shift();
  174.             this.setSliderQueue();
  175.  
  176.             if ( this.sliderQueue.length === 0 ) {
  177.                 window.close();
  178.             }
  179.             else {
  180.                 // Process next in queue.
  181.                 this.prefillAlertInfo();
  182.                 this.onAlertLoad();
  183.             }
  184.         }
  185.     },
  186.  
  187.     // Retrieve the global.
  188.  
  189.     getSliderQueue: function() {
  190.         SimpleTimerSliderAlert.sliderQueue = SimpleTimerSliderAlert.application.storage.get("sliderQueue", "ERR");
  191.     },
  192.  
  193.     // Update the global.
  194.  
  195.     setSliderQueue: function() {
  196.         SimpleTimerSliderAlert.application.storage.set("sliderQueue", SimpleTimerSliderAlert.sliderQueue);
  197.     },
  198.  
  199.     // Debug messages to console.
  200.  
  201.     debug: function (aMsg) {
  202.         setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
  203.     }
  204. };