app/jquery/jquery-spin-1.0.2.js
author Daniel Bentley <dbentley@google.com>
Sun, 12 Apr 2009 09:06:45 +0000
changeset 2168 ef7222d4847f
parent 1727 718744a10daa
permissions -rw-r--r--
Use offset_linkid instead of offset to scan >1000 entities. this is a first-cut. It works in all the ways I could make earlier versions fail. It passes link_id as URL parameters. It also has a new class LinkCreator which makes the main body of getListContents even easier to write. I wasn't sure if link_id's could have non alphanumeric characters; if so, they need to be URL encoded/decoded. I also need to go and remove any mention of raw offsets now, because we don't use them. I believe I've talked about this approach with a few of you and it sounded reasonable. Feel free to roll-back/fix/amend/comment-for-me-to-fix. This is my first big-logic-change to Melange. Patch by: Dan Bentley

/**
 *  jquery.spin-button
 *  (c) 2008 Semooh (http://semooh.jp/)
 *
 *  Dual licensed under the MIT (MIT-LICENSE.txt)
 *  and GPL (GPL-LICENSE.txt) licenses.
 *
 **/
(function($){
  $.fn.extend({
    spin: function(opt){
      return this.each(function(){
        opt = $.extend({
            imageBasePath: '/soc/content/images/',
            spinBtnImage: 'spin-button.png',
            spinUpImage: 'spin-up.png',
            spinDownImage: 'spin-down.png',
            interval: 1,
            max: null,
            min: null,
            timeInterval: 500,
            timeBlink: 200
          }, opt || {});

        var txt = $(this);

        var spinBtnImage = opt.imageBasePath+opt.spinBtnImage;
        var btnSpin = new Image();
        btnSpin.src = spinBtnImage;
        var spinUpImage = opt.imageBasePath+opt.spinUpImage;
        var btnSpinUp = new Image();
        btnSpinUp.src = spinUpImage;
        var spinDownImage = opt.imageBasePath+opt.spinDownImage;
        var btnSpinDown = new Image();
        btnSpinDown.src = spinDownImage;

        var btn = $(document.createElement('img'));
        btn.attr('src', spinBtnImage);
        btn.css({cursor: 'pointer', verticalAlign: 'bottom', padding: 0, margin: 0});
        txt.after(btn);
        txt.css({marginRight:0, paddingRight:0});

        function spin(vector){
          var val = txt.val();
          if(!isNaN(val)){
            val = parseFloat(val) + (vector*opt.interval);
            if(opt.min!=null && val<opt.min) val=opt.min;
            if(opt.min!=null && val>opt.max) val=opt.max;
            if(val != txt.val()){
              txt.val(val);
              txt.change();
              src = (vector > 0 ? spinUpImage : spinDownImage);
              btn.attr('src', src);
              if(opt.timeBlink<opt.timeInterval)
                setTimeout(function(){btn.attr('src', spinBtnImage);}, opt.timeBlink);
            }
          }
        }

        btn.mousedown(function(e){
          var pos = e.pageY - btn.offset().top;
          var vector = (btn.height()/2 > pos ? 1 : -1);
          (function(){
            spin(vector);
            var tk = setTimeout(arguments.callee, opt.timeInterval);
            $(document).one('mouseup', function(){
              clearTimeout(tk); btn.attr('src', spinBtnImage);
            });
          })();
          return false;
        });
      });
    }
  });
})(jQuery);