2

I am using a stocks chart from HighStocks together with gridster. Each individual block in gridster is freely draggable. However, the stocks time slider gadget is also draggable and resizable. Since it sits on top of a gridster widget, whenever I drag the slider around, the entire widget also moves. I included a JSFiddle to demonstrate my point. http://jsfiddle.net/faPrd/

It's not as pronounced in this Fiddle because there are not that many elements, but you can already see that when you drag the slider around, the entire gadget shifts somewhat. How can I prevent this?

My HTML:

<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>

<div class="gridster ready">
    <ul id = "gridlist" style="height: 550px; width: 550px; position: relative">
        <li id = "gridlist1" class="gs-w" data-row="1" data-col="1" data-sizex="3" data-sizey="3">
            <div id="container" style="height: 400px; min-width: 310px"></div>
        </li>
        <li id = "gridlist2" class="gs-w" data-row="1" data-col="4" data-sizex="3" data-sizey="3">
        </li>
    </ul>
</div>

My Javascript for gridster:

var gridster;
$(function(){
  gridster = $(".gridster ul").gridster({
    widget_base_dimensions: [150, 150],
    widget_margins: [5, 5],

    serialize_params: function($w, wgd) {
      return {
        x: wgd.col,
        y: wgd.row,
        width: wgd.size_x,
        height: wgd.size_y,
        id: $($w).attr('id')
      };
    },
    draggable: {
      stop: function(event, ui) {
        var positions = "[";
        for (var i = 0; i < this.serialize().length; i++) {
          positions += JSON.stringify(this.serialize()[i]);
          if (i !== this.serialize().length - 1) { 
            positions += ",";
          }
        }
        positions += "]";
        localStorage.setItem('positions', positions);
      }
    },
    helper: 'clone',
    resize: {
      enabled: true,
      stop: function(e, ui, $widget) {
        var positions = "[";
        for (var i = 0; i < this.serialize().length; i++) {
          positions += JSON.stringify(this.serialize()[i]);
          if (i !== this.serialize().length - 1) { 
            positions += ",";
          }
        }
        positions += "]";
        localStorage.setItem('positions', positions);
      }
    }
  }).data('gridster');
});
user3758133
  • 228
  • 4
  • 13

1 Answers1

1

One general jQuery solution might be detecting mousedown on the chart-container to disable gridster-dragging, and detecting mouseup in general to enable gridster-dragging.

For example using the following code (JSFiddle example):

$('#container').mousedown(function() {
    gridster.disable();
});

$(document).mouseup(function(){
    gridster.enable();
}); 

This would require you to have some area that is not part of the chart-container, but is in the grid (otherwise you would have nowhere to grab the gridster-grid). Or you could be more specific with what elements that should disable the gridster-dragging, when clicked.

Seing how .highcharts-navigator doesn't work very well as a selector (and neither does any amount of selectors in that region), I found that this might be one of the easier ways to only disable gridster for the navigator (JSFiddle example):

$('#container').mousedown(function(event) {
    var chart = $('#container').highcharts();
    var navTop = $('.highcharts-navigator').offset().top;
    var navHeight = chart.options.navigator.height + chart.options.scrollbar.height;

    if(event.pageY > navTop &&
            event.pageY < navTop + navHeight) {
        gridster.disable();
    }
});

$(document).mouseup(function(){
    gridster.enable();
}); 
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
  • Thanks for your answer, I can see that it works great. I am trying to be more specific with what elements should disable the dragging. I am using $(".highcharts-navigator").mousedown as that is the class name of the navigator gadget when I inspect element. However, that's not working, as you can see here: http://jsfiddle.net/faPrd/2/ Why is that? – user3758133 Jul 25 '14 at 19:29
  • I've updated the answer to reflect your problems. You could try with multiple selectors, but in the end it would still be possible to click the chart background, which covered a much larger area, so selectors alone weren't sufficient, from my experience. – Halvor Holsten Strand Jul 25 '14 at 21:49
  • Ah, calculating the size of the navigator and checking for event.pageY. Very clever and works pretty well as far as I can see. Thanks a lot for the great answer! – user3758133 Jul 25 '14 at 22:06