jquery.flot.resize.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* eslint-disable */
  2. /* Flot plugin for automatically redrawing plots as the placeholder resizes.
  3. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  4. Licensed under the MIT license.
  5. It works by listening for changes on the placeholder div (through the jQuery
  6. resize event plugin) - if the size changes, it will redraw the plot.
  7. There are no options. If you need to disable the plugin for some plots, you
  8. can just fix the size of their placeholders.
  9. */
  10. /* Inline dependency:
  11. * jQuery resize event - v1.1 - 3/14/2010
  12. * http://benalman.com/projects/jquery-resize-plugin/
  13. *
  14. * Copyright (c) 2010 "Cowboy" Ben Alman
  15. * Dual licensed under the MIT and GPL licenses.
  16. * http://benalman.com/about/license/
  17. */
  18. (function($,e,t){"$:nomunge";var i=[],n=$.resize=$.extend($.resize,{}),a,r=false,s="setTimeout",u="resize",m=u+"-special-event",o="pendingDelay",l="activeDelay",f="throttleWindow";n[o]=200;n[l]=20;n[f]=true;$.event.special[u]={setup:function(){if(!n[f]&&this[s]){return false}var e=$(this);i.push(this);e.data(m,{w:e.width(),h:e.height()});if(i.length===1){a=t;h()}},teardown:function(){if(!n[f]&&this[s]){return false}var e=$(this);for(var t=i.length-1;t>=0;t--){if(i[t]==this){i.splice(t,1);break}}e.removeData(m);if(!i.length){if(r){cancelAnimationFrame(a)}else{clearTimeout(a)}a=null}},add:function(e){if(!n[f]&&this[s]){return false}var i;function a(e,n,a){var r=$(this),s=r.data(m)||{};s.w=n!==t?n:r.width();s.h=a!==t?a:r.height();i.apply(this,arguments)}if($.isFunction(e)){i=e;return a}else{i=e.handler;e.handler=a}}};function h(t){if(r===true){r=t||1}for(var s=i.length-1;s>=0;s--){var l=$(i[s]);if(l[0]==e||l.is(":visible")){var f=l.width(),c=l.height(),d=l.data(m);if(d&&(f!==d.w||c!==d.h)){l.trigger(u,[d.w=f,d.h=c]);r=t||true}}else{d=l.data(m);d.w=0;d.h=0}}if(a!==null){if(r&&(t==null||t-r<1e3)){a=e.requestAnimationFrame(h)}else{a=setTimeout(h,n[o]);r=false}}}if(!e.requestAnimationFrame){e.requestAnimationFrame=function(){return e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(t,i){return e.setTimeout(function(){t((new Date).getTime())},n[l])}}()}if(!e.cancelAnimationFrame){e.cancelAnimationFrame=function(){return e.webkitCancelRequestAnimationFrame||e.mozCancelRequestAnimationFrame||e.oCancelRequestAnimationFrame||e.msCancelRequestAnimationFrame||clearTimeout}()}})(jQuery,this);
  19. /* eslint-enable */
  20. (function ($) {
  21. var options = { }; // no options
  22. function init(plot) {
  23. function onResize() {
  24. var placeholder = plot.getPlaceholder();
  25. // somebody might have hidden us and we can't plot
  26. // when we don't have the dimensions
  27. if (placeholder.width() === 0 || placeholder.height() === 0) return;
  28. plot.resize();
  29. plot.setupGrid();
  30. plot.draw();
  31. }
  32. function bindEvents(plot, eventHolder) {
  33. plot.getPlaceholder().resize(onResize);
  34. }
  35. function shutdown(plot, eventHolder) {
  36. plot.getPlaceholder().unbind("resize", onResize);
  37. }
  38. plot.hooks.bindEvents.push(bindEvents);
  39. plot.hooks.shutdown.push(shutdown);
  40. }
  41. $.plot.plugins.push({
  42. init: init,
  43. options: options,
  44. name: 'resize',
  45. version: '1.0'
  46. });
  47. })(jQuery);