root / templates / muncollapse / static / js / dynazoom.js @ 12ded312
Historique | Voir | Annoter | Télécharger (8,99 ko)
| 1 | 12ded312 | JTSage | /* MunCollapse Template DynaZoom JavaScript File
|
|---|---|---|---|
| 2 | *
|
||
| 3 | * Notes:
|
||
| 4 | *
|
||
| 5 | * - No Internet Explorer support (uses "URLSearchParams")
|
||
| 6 | * - This is not even really a fork of the upstream version any more
|
||
| 7 | * - Drops upstream requirement of "QueryString" include (URL.URLSearchParams)
|
||
| 8 | * - Drops upstream requirement of "FormatDate" include (Date.toISOString())
|
||
| 9 | */
|
||
| 10 | |||
| 11 | URLSearchParams.prototype.getDefault = function ( name, value ) { |
||
| 12 | // Overload URLSearchParams to allow "default" values.
|
||
| 13 | return ( this.get( name ) === null ) ? value : this.get( name ); |
||
| 14 | }; |
||
| 15 | |||
| 16 | function refreshZoom( query, form, image ) { |
||
| 17 | //INIT
|
||
| 18 | var qs = new URLSearchParams( query.split( "\?" )[ 1 ] ); |
||
| 19 | |||
| 20 | init(); |
||
| 21 | |||
| 22 | refreshImg(); |
||
| 23 | |||
| 24 | var start_epoch = parseInt( qs.getDefault( "rst_start_epoch", form.start_epoch.value ), 10 ); |
||
| 25 | var stop_epoch = parseInt( qs.getDefault( "rst_stop_epoch", form.stop_epoch.value ), 10 ); |
||
| 26 | |||
| 27 | var highLighter = document.getElementById( "image-overlay" ); |
||
| 28 | var gutterOffsetLeft = 66; |
||
| 29 | var highlightStartX = 0; |
||
| 30 | var clickCounter = 0; |
||
| 31 | var relativeStartX = 0; |
||
| 32 | var graph_shown_width;
|
||
| 33 | var epoch_shown_start;
|
||
| 34 | var epoch_shown_stop;
|
||
| 35 | var eachPixelEpoch;
|
||
| 36 | |||
| 37 | form.plugin_name.onblur = refreshImg; |
||
| 38 | form.start_iso8601.onblur = majDates; |
||
| 39 | form.stop_iso8601.onblur = majDates; |
||
| 40 | form.start_epoch.onblur = function() { refreshImg(); updateStartStop(); }; |
||
| 41 | form.stop_epoch.onblur = function() { refreshImg(); updateStartStop(); }; |
||
| 42 | form.lower_limit.onblur = refreshImg; |
||
| 43 | form.upper_limit.onblur = refreshImg; |
||
| 44 | form.size_x.onblur = refreshImg; |
||
| 45 | form.size_y.onblur = refreshImg; |
||
| 46 | form.btnReset.onclick = reset; |
||
| 47 | form.btnShowDay.onclick = function() { showPeriod( 1 ); }; |
||
| 48 | form.btnShowWeek.onclick = function() { showPeriod( 2 ); }; |
||
| 49 | form.btnShowMonth.onclick = function() { showPeriod( 3 ); }; |
||
| 50 | form.btnShowYear.onclick = function() { showPeriod( 4 ); }; |
||
| 51 | form.onsubmit = function() { document.activeElement.blur(); refreshImg(); return false; }; |
||
| 52 | |||
| 53 | // Sets the onClick handler
|
||
| 54 | image.onclick = click; |
||
| 55 | |||
| 56 | //FUNCTIONS
|
||
| 57 | function init() { |
||
| 58 | form.plugin_name.value = qs.getDefault( "plugin_name", "localdomain/localhost.localdomain/if_eth0" ); |
||
| 59 | form.start_epoch.value = qs.getDefault( "start_epoch", "1236561663" ); |
||
| 60 | form.stop_epoch.value = qs.getDefault( "stop_epoch", "1237561663" ); |
||
| 61 | form.lower_limit.value = qs.getDefault( "lower_limit", "" ); |
||
| 62 | form.upper_limit.value = qs.getDefault( "upper_limit", "" ); |
||
| 63 | form.size_x.value = qs.getDefault( "size_x", "" ); |
||
| 64 | form.size_y.value = qs.getDefault( "size_y", "" ); |
||
| 65 | |||
| 66 | updateStartStop(); |
||
| 67 | } |
||
| 68 | |||
| 69 | function reset( event ) { |
||
| 70 | init(); |
||
| 71 | |||
| 72 | //Can be not the initial ones in case of manual refresh
|
||
| 73 | form.start_epoch.value = start_epoch; |
||
| 74 | form.stop_epoch.value = stop_epoch; |
||
| 75 | updateStartStop(); |
||
| 76 | |||
| 77 | //Redraw
|
||
| 78 | scale = refreshImg(); |
||
| 79 | |||
| 80 | //Reset gui
|
||
| 81 | clickCounter = 0;
|
||
| 82 | image.onmousemove = undefined;
|
||
| 83 | form.start_iso8601.disabled = false;
|
||
| 84 | form.stop_iso8601.disabled = false;
|
||
| 85 | form.start_epoch.disabled = false;
|
||
| 86 | form.stop_epoch.disabled = false;
|
||
| 87 | highLighter.style.left = "0px";
|
||
| 88 | highLighter.style.width = "2px";
|
||
| 89 | highLighter.style.display = "none";
|
||
| 90 | |||
| 91 | document.activeElement.blur(); |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | function refreshImg(event) { |
||
| 96 | image.src = qs.getDefault( "cgiurl_graph", "/munin-cgi/munin-cgi-graph" ) + "/" + |
||
| 97 | form.plugin_name.value + |
||
| 98 | "-pinpoint=" + parseInt( form.start_epoch.value, 10 ) + "," + parseInt( form.stop_epoch.value, 10 ) + |
||
| 99 | ".png" + "?" + |
||
| 100 | "&lower_limit=" + form.lower_limit.value +
|
||
| 101 | "&upper_limit=" + form.upper_limit.value +
|
||
| 102 | "&size_x=" + form.size_x.value +
|
||
| 103 | "&size_y=" + form.size_y.value;
|
||
| 104 | } |
||
| 105 | |||
| 106 | function updateStartStop() { |
||
| 107 | form.start_iso8601.value = new Date( form.start_epoch.value * 1000 ).toISOString(); |
||
| 108 | form.stop_iso8601.value = new Date( form.stop_epoch.value * 1000 ).toISOString(); |
||
| 109 | } |
||
| 110 | |||
| 111 | function majDates( event ) { |
||
| 112 | var lowLimit = new Date(), |
||
| 113 | topLimit = new Date(),
|
||
| 114 | date_parsed = null;
|
||
| 115 | |||
| 116 | lowLimit.setFullYear( lowLimit.getFullYear() - 1 );
|
||
| 117 | |||
| 118 | date_parsed = new Date( Date.parse( form.start_iso8601.value ) || lowLimit.getTime() );
|
||
| 119 | form.start_epoch.value = Math.floor( date_parsed.getTime() / 1000 );
|
||
| 120 | |||
| 121 | date_parsed = new Date( Date.parse( form.stop_iso8601.value) || topLimit.getTime() );
|
||
| 122 | form.stop_epoch.value = Math.floor( date_parsed.getTime() / 1000 );
|
||
| 123 | |||
| 124 | updateStartStop(); |
||
| 125 | |||
| 126 | refreshImg(); |
||
| 127 | } |
||
| 128 | |||
| 129 | function click( event ) { |
||
| 130 | var relativeClickX = getClickLocation( event ),
|
||
| 131 | thisEpoch = null;
|
||
| 132 | |||
| 133 | switch ( ( clickCounter++ ) % 3 ) { |
||
| 134 | case 0: // First click of the displayed graph |
||
| 135 | graph_shown_width = parseInt( form.size_x.value, 10) ;
|
||
| 136 | epoch_shown_start = parseInt( form.start_epoch.value,10 );
|
||
| 137 | epoch_shown_stop = parseInt( form.stop_epoch.value, 10 );
|
||
| 138 | eachPixelEpoch = ( ( epoch_shown_stop - epoch_shown_start ) / graph_shown_width ); |
||
| 139 | relativeStartX = ( relativeClickX < 0 ? 0 : relativeClickX ); |
||
| 140 | |||
| 141 | form.start_iso8601.disabled = true;
|
||
| 142 | form.stop_iso8601.disabled = true;
|
||
| 143 | form.start_epoch.disabled = true;
|
||
| 144 | form.stop_epoch.disabled = true;
|
||
| 145 | |||
| 146 | highlightStartX = event.pageX; |
||
| 147 | highLighter.style.left = ( relativeStartX + gutterOffsetLeft + image.offsetLeft ) + "px";
|
||
| 148 | highLighter.style.display = "block";
|
||
| 149 | |||
| 150 | form.start_epoch.value = offsetEpoch( relativeClickX ); |
||
| 151 | updateStartStop(); |
||
| 152 | |||
| 153 | image.onmousemove = divMouseMove; |
||
| 154 | |||
| 155 | break;
|
||
| 156 | case 1: // Second (end) click of the displayed graph |
||
| 157 | thisEpoch = offsetEpoch( relativeClickX ); |
||
| 158 | |||
| 159 | image.onmousemove = undefined;
|
||
| 160 | form.start_iso8601.disabled = false;
|
||
| 161 | form.stop_iso8601.disabled = false;
|
||
| 162 | form.start_epoch.disabled = false;
|
||
| 163 | form.stop_epoch.disabled = false;
|
||
| 164 | |||
| 165 | // For negative values, assume we want a new start point and the old end point.
|
||
| 166 | // If it's not, set it.
|
||
| 167 | if ( thisEpoch > form.start_epoch.value ) {
|
||
| 168 | form.stop_epoch.value = thisEpoch; |
||
| 169 | } else {
|
||
| 170 | form.stop_epoch.value = Math.floor( epoch_shown_stop ); |
||
| 171 | highLighter.style.width = ( graph_shown_width - relativeStartX ) + "px";
|
||
| 172 | } |
||
| 173 | updateStartStop(); |
||
| 174 | |||
| 175 | break;
|
||
| 176 | case 2: // Nevermind or Do It. |
||
| 177 | thisEpoch = offsetEpoch( relativeClickX ); |
||
| 178 | |||
| 179 | if ( thisEpoch >= form.start_epoch.value && thisEpoch <= form.stop_epoch.value ) {
|
||
| 180 | refreshImg(); |
||
| 181 | } else {
|
||
| 182 | form.start_epoch.value = epoch_shown_start; |
||
| 183 | form.stop_epoch.value = epoch_shown_stop; |
||
| 184 | updateStartStop(); |
||
| 185 | } |
||
| 186 | |||
| 187 | highLighter.style.left = "0px";
|
||
| 188 | highLighter.style.width = "2px";
|
||
| 189 | highLighter.style.display = "none";
|
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | function divMouseMove( event ) { |
||
| 194 | var diff = event.pageX - highlightStartX,
|
||
| 195 | maxDiff = graph_shown_width - relativeStartX, |
||
| 196 | relativeClickX = getClickLocation( event ); |
||
| 197 | |||
| 198 | form.stop_epoch.value = offsetEpoch( relativeClickX ); |
||
| 199 | updateStartStop(); |
||
| 200 | |||
| 201 | highLighter.style.width = ( ( diff < 2 ) ? 2 : ( diff > maxDiff ? maxDiff : diff ) ) + "px"; |
||
| 202 | } |
||
| 203 | |||
| 204 | function offsetEpoch( clickX ) { |
||
| 205 | if ( clickX < 0 ) { return Math.floor( epoch_shown_start ); } |
||
| 206 | |||
| 207 | if ( clickX > graph_shown_width ) { return Math.floor( epoch_shown_stop ); } |
||
| 208 | |||
| 209 | return Math.floor( epoch_shown_start + ( clickX * eachPixelEpoch ) );
|
||
| 210 | } |
||
| 211 | |||
| 212 | function getClickLocation( event ) { |
||
| 213 | return ( event.pageX - image.getBoundingClientRect().x - gutterOffsetLeft );
|
||
| 214 | } |
||
| 215 | |||
| 216 | function showPeriod( period ) { |
||
| 217 | var now = new Date(), |
||
| 218 | past = new Date();
|
||
| 219 | |||
| 220 | switch (period) {
|
||
| 221 | case 1: |
||
| 222 | past.setDate( past.getDate() - 1 ); break; |
||
| 223 | case 2: |
||
| 224 | past.setDate( past.getDate() - 7 ); break; |
||
| 225 | case 3: |
||
| 226 | past.setMonth( past.getMonth() - 1 ); break; |
||
| 227 | case 4: |
||
| 228 | past.setFullYear( past.getFullYear() - 1 ); break; |
||
| 229 | } |
||
| 230 | |||
| 231 | form.start_epoch.value = Math.floor( past.getTime() / 1000 );
|
||
| 232 | form.stop_epoch.value = Math.floor( now.getTime() / 1000 );
|
||
| 233 | |||
| 234 | updateStartStop(); |
||
| 235 | refreshImg(); |
||
| 236 | document.activeElement.blur(); |
||
| 237 | return false; |
||
| 238 | } |
||
| 239 | } |
