home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2009 March / ME_03_2009.iso / Software / Internet / Cyberduck-3.1.2.dmg / Cyberduck.wdgt / Upload.js < prev   
Encoding:
JavaScript  |  2009-02-01  |  10.0 KB  |  380 lines

  1. /**
  2.  *    Cyberduck Dashboard widget
  3.  *    Copyright (c) 2006 Claudio Procida & David V. Kocher. All rights reserved.
  4.  *    http://cyberduck.ch/
  5.  *
  6.  *    This program is free software; you can redistribute it and/or modify
  7.  *    it under the terms of the GNU General Public License as published by
  8.  *    the Free Software Foundation; either version 2 of the License, or
  9.  *    (at your option) any later version.
  10.  *
  11.  *    This program is distributed in the hope that it will be useful,
  12.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *    GNU General Public License for more details.
  15.  *
  16.  *    Bug fixes, suggestions and comments should be sent to:
  17.  *    dkocher@cyberduck.ch
  18.  */
  19.  
  20. var CONFIGURE_ME;
  21.  
  22. var flipShown = false,
  23.     configured = false,
  24.     animation = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, firstElement:null, timer:null},
  25.     bouncer = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, firstElement:null, timer:null, times:1};    
  26.  
  27. function setup()
  28. {
  29.     CONFIGURE_ME =  '<span id="catcheye" onclick="showPreferences()">' + getLocalizedString("Please configure me!") + '</span>';
  30.     createGenericButton(document.getElementById("done"), getLocalizedString("Done"), hidePreferences);
  31.     if (window.widget)
  32.     {
  33.         widget.onremove = clearPreferences;
  34.     }
  35.     document.getElementById("server").setAttribute("placeholder", getLocalizedString("Server"));
  36.     document.getElementById("user").setAttribute("placeholder", getLocalizedString("Username"));
  37.     document.getElementById("path").setAttribute("placeholder", getLocalizedString("Path"));
  38.     loadPreferences();
  39. }
  40.  
  41. function getLocalizedString (key) {
  42.     try {
  43.         var ret = localizedStrings[key];
  44.         if (ret === undefined) {
  45.             ret = key;
  46.         }
  47.         return ret;
  48.     }
  49.     catch (ex) {
  50.         //
  51.     }
  52.     return key;
  53. }
  54.  
  55. function bookmarkSelectionChanged(popup) 
  56. {
  57.     if (window.Plugin) {
  58.         document.getElementById("nickname").value = Plugin.nicknameAtIndex(popup.options[popup.selectedIndex].value);
  59.         document.getElementById("server").value = Plugin.hostnameAtIndex(popup.options[popup.selectedIndex].value);
  60.         document.getElementById("user").value = Plugin.usernameAtIndex(popup.options[popup.selectedIndex].value);
  61.         document.getElementById("path").value = Plugin.pathAtIndex(popup.options[popup.selectedIndex].value);
  62.         document.getElementById("protocol").value = Plugin.protocolAtIndex(popup.options[popup.selectedIndex].value);
  63.     }
  64. }
  65.  
  66. function savePreferences()
  67. {
  68.     var nickname =   document.getElementById("nickname").value || null;
  69.     var server =   document.getElementById("server").value || null;
  70.     var user =     document.getElementById("user").value || null;
  71.     var path =     document.getElementById("path").value || null;
  72.     var protocol = document.getElementById("protocol").value || null;
  73.     
  74.     if (verifyAccount())
  75.     {    
  76.         widget.setPreferenceForKey(nickname, widget.identifier + "@nickname");
  77.         widget.setPreferenceForKey(server, widget.identifier + "@server");
  78.         widget.setPreferenceForKey(user, widget.identifier + "@user");
  79.         widget.setPreferenceForKey(path, widget.identifier + "@path");
  80.         widget.setPreferenceForKey(protocol, widget.identifier + "@protocol");
  81.     }
  82. }
  83.  
  84. function loadPreferences()
  85. {
  86.     var nickname = widget.preferenceForKey(widget.identifier + "@nickname");
  87.     var server = widget.preferenceForKey(widget.identifier + "@server");
  88.     var user = widget.preferenceForKey(widget.identifier + "@user");
  89.     var path = widget.preferenceForKey(widget.identifier + "@path");
  90.     var protocol = widget.preferenceForKey(widget.identifier + "@protocol");
  91.  
  92.     document.getElementById("nickname").value = nickname || null;
  93.     document.getElementById("server").value = server || null;
  94.     document.getElementById("user").value = user || null;
  95.     document.getElementById("path").value = path || null;
  96.     document.getElementById("protocol").value = protocol || null;
  97.  
  98.     verifyAccount();
  99. }
  100.  
  101. function clearPreferences()
  102. {
  103.     widget.setPreferenceForKey(null, widget.identifier + "@server");
  104.     widget.setPreferenceForKey(null, widget.identifier + "@user");
  105.     widget.setPreferenceForKey(null, widget.identifier + "@path");
  106.     widget.setPreferenceForKey(null, widget.identifier + "@protocol");
  107. }
  108.  
  109. function showPreferences() 
  110. {
  111.     var front = document.getElementById("front");
  112.     var back = document.getElementById("back");
  113.  
  114.     widget.prepareForTransition("ToBack");
  115.        
  116.     front.style.display="none";
  117.     back.style.display="block";
  118.  
  119.     if (window.widget)
  120.         setTimeout ("widget.performTransition();", 0);
  121. }
  122.  
  123. function hidePreferences() 
  124. {
  125.     savePreferences();
  126.     hideObj("fliprollie");
  127.  
  128.     var front = document.getElementById("front");
  129.     var back = document.getElementById("back");
  130.  
  131.     widget.prepareForTransition("ToFront");
  132.  
  133.     back.style.display="none";
  134.     front.style.display="block";
  135.  
  136.     if (window.widget) {
  137.         setTimeout ("widget.performTransition();", 0);
  138.     }
  139. }
  140.  
  141. function mousemove (event) 
  142. {
  143.     if (!flipShown) {
  144.         if (animation.timer != null) {
  145.             clearInterval (animation.timer);
  146.             animation.timer  = null;
  147.         }
  148.  
  149.         var starttime = (new Date).getTime() - 13;
  150.  
  151.         animation.duration = 500;
  152.         animation.starttime = starttime;
  153.         animation.firstElement = document.getElementById ("flip");
  154.         animation.timer = setInterval ("animate();", 13);
  155.         animation.from = animation.now;
  156.         animation.to = 1.0;
  157.         animate();
  158.         flipShown = true;
  159.     }
  160. }
  161.  
  162. function mouseexit (event) 
  163. {
  164.     if (flipShown) {
  165.         // fade in the info button
  166.         if (animation.timer != null) {
  167.             clearInterval (animation.timer);
  168.             animation.timer  = null;
  169.         }
  170.  
  171.         var starttime = (new Date).getTime() - 13;
  172.  
  173.         animation.duration = 500;
  174.         animation.starttime = starttime;
  175.         animation.firstElement = document.getElementById ("flip");
  176.         animation.timer = setInterval ("animate();", 13);
  177.         animation.from = animation.now;
  178.         animation.to = 0.0;
  179.         animate();
  180.         flipShown = false;
  181.     }
  182. }
  183.  
  184. function animate() 
  185. {
  186.     var T;
  187.     var ease;
  188.     var time = (new Date).getTime();
  189.    
  190.  
  191.     T = limit_3(time-animation.starttime, 0, animation.duration);
  192.  
  193.     if (T >= animation.duration) {
  194.         clearInterval (animation.timer);
  195.         animation.timer = null;
  196.         animation.now = animation.to;
  197.     }
  198.     else {
  199.         ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
  200.         animation.now = computeNextFloat (animation.from, animation.to, ease);
  201.     }
  202.  
  203.     animation.firstElement.style.opacity = animation.now;
  204. }
  205.  
  206. function bounce() 
  207. {
  208.     var T;
  209.     var ease;
  210.     var time = (new Date).getTime();
  211.  
  212.     T = limit_3(time-bouncer.starttime, 0, bouncer.duration * bouncer.times);
  213.  
  214.     if (T >= bouncer.duration * bouncer.times) {
  215.         // The duration of animation has reached the total time
  216.         // needed by the required bounces. We reposition the duck
  217.         // to its rest position.
  218.  
  219.         clearInterval (bouncer.timer);
  220.         bouncer.timer = null;
  221.         bouncer.now = bouncer.from;
  222.     }
  223.     else if (bouncer.shouldStop) {
  224.         // We adjust the number of bounces, bouncer.times, to finish
  225.         // the current bounce
  226.  
  227.         bouncer.times = Math.ceil(T / bouncer.duration);
  228.         bouncer.shouldStop = false;
  229.     }
  230.     else {
  231.         ease = Math.abs(Math.sin(Math.PI * T / bouncer.duration));
  232.         bouncer.now = computeNextInt (bouncer.from, bouncer.to, ease);
  233.     }
  234.  
  235.     bouncer.firstElement.style.backgroundPositionY = bouncer.now + "px";
  236. }
  237.  
  238. function bounceDuckStop()
  239. {
  240.     // Simply signals that this is the last bounce
  241.     bouncer.shouldStop = true;
  242. }
  243.  
  244. function bounceDuckStart(times) 
  245. {
  246.     if (bouncer.timer) {
  247.         clearInterval (bouncer.timer);
  248.         bouncer.timer = null;
  249.     }
  250.  
  251.     var starttime = (new Date).getTime() - 11;
  252.  
  253.     bouncer.shouldStop = false;
  254.     bouncer.duration = 650;
  255.     bouncer.starttime = starttime;
  256.     bouncer.firstElement = document.getElementById ("duck");
  257.     bouncer.timer = setInterval ("bounce();", 11);
  258.     bouncer.from = 30;
  259.     bouncer.to = 0;
  260.     bouncer.times = times || 1;
  261.     bounce();
  262. }
  263.  
  264. function showObj(id) { // shows an element
  265.     document.getElementById(id).style.display = "block";
  266. }
  267.  
  268. function hideObj(id) { // hides an element
  269.     document.getElementById(id).style.display = "none";
  270. }
  271.  
  272. function limit_3 (a, b, c) {
  273.     return a < b ? b : (a > c ? c : a);
  274. }
  275.  
  276. function computeNextFloat (from, to, ease) {
  277.     return from + (to - from) * ease;
  278. }
  279.  
  280. function computeNextInt (from, to, ease) {
  281.     return Math.round(from + (to - from) * ease);
  282. }
  283.  
  284. function enterflip(event) {
  285.     showObj("fliprollie");
  286. }
  287.  
  288. function exitflip(event) {
  289.     hideObj("fliprollie");
  290. }
  291.  
  292. function verifyAccount()
  293. {
  294.     configured = 
  295.         (document.getElementById("server").value != null
  296.         && document.getElementById("server").value != ""
  297.         && document.getElementById("user").value != null
  298.         && document.getElementById("user").value != "");
  299.     if(configured) {
  300.         document.getElementById("serverlabel").innerHTML = document.getElementById("nickname").value;
  301.     }
  302.     else {
  303.         document.getElementById("serverlabel").innerHTML = CONFIGURE_ME;
  304.     }
  305.     return configured;
  306. }
  307.  
  308. /**
  309.  *    Drag and drop handlers
  310.  */
  311.  
  312. function dragdrop (event) {
  313.     var uri = null;
  314.  
  315.     try {
  316.         uri = event.dataTransfer.getData("text/uri-list");    // attempt to load the URL
  317.     } 
  318.     catch (e) {
  319.         alert(e);
  320.     }
  321.  
  322.     if (uri && configured)
  323.     {
  324.         var droppedfilesURI = uri.split("\n");
  325.         for(var i = 0; i < droppedfilesURI.length; i++) {
  326.             droppedfilesURI[i] = droppedfilesURI[i].toPosixPath();
  327.         }
  328.         var droppedfilesLocal = droppedfilesURI.join(" ");
  329.         transfer(droppedfilesLocal);
  330.     }
  331.     else
  332.     {
  333.         bounceDuckStart(1);
  334.     }
  335.     
  336.     event.stopPropagation();
  337.     event.preventDefault();
  338. }
  339.  
  340. // The dragenter, dragover, and dragleave functions are implemented but not used.  They
  341. // can be used if you want to change the image when it enters the widget.
  342.  
  343. function dragenter (event) {
  344.     event.stopPropagation();
  345.     event.preventDefault();
  346. }
  347.  
  348. function dragover (event) {
  349.     event.stopPropagation();
  350.     event.preventDefault();
  351. }
  352.  
  353. function dragleave (event) {
  354.     event.stopPropagation();
  355.     event.preventDefault();
  356. }
  357.  
  358. function transfer(file)
  359. {
  360.     var server =   document.getElementById("server").value || null;
  361.     var user =     document.getElementById("user").value || null;
  362.     var path =     document.getElementById("path").value || null;
  363.     var protocol = document.getElementById("protocol").value || null;
  364.  
  365.     bounceDuckStart(1000);    
  366.  
  367.     var command = "/usr/bin/osascript Scripts/Upload.scpt"
  368.         + " " + protocol
  369.         + " " + server
  370.         + " " + user
  371.         + " " + path
  372.         + " " + file;
  373.     widget.system(command, bounceDuckStop);
  374. }
  375.  
  376. String.prototype.toPosixPath = function()
  377. {
  378.     var tmp = unescape(this.substr(this.indexOf("localhost") + 9));
  379.     return tmp.replace(/([ \[\]\(\)\$&%:=\?\!])/g, "\\$1");
  380. }