Skip to content

Commit

Permalink
Overview map hotfix (#451)
Browse files Browse the repository at this point in the history
* overview map control

* add "set" template tag

* set template tag tests
  • Loading branch information
sdc50 authored and swainn committed Jul 22, 2019
1 parent d7362ab commit 8c096f7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ def test_HighchartsDateEncoder_no_dt(self):
# Check Result
self.assertEqual('2018-01-01', result)

def test_SetVarNode_template_error(self):
node = gizmos_templatetags.SetVarNode('test', 'test')
result = node.render({})
self.assertEqual('', result)

@mock.patch('tethys_gizmos.templatetags.tethys_gizmos.template.Variable')
def test_SetVarNode_render(self, mock_template_var):
mock_Variable = mock.MagicMock()
mock_Variable.resolve.return_value = 'foo'
mock_template_var.return_value = mock_Variable
context = {'test1': {}}
node = gizmos_templatetags.SetVarNode('test1.test', 'test')
result = node.render(context)
self.assertDictEqual(context, {'test1': {'test': 'foo'}})

self.assertEqual('', result)

@mock.patch('tethys_gizmos.templatetags.tethys_gizmos.SetVarNode')
def test_set_var(self, _):
token = mock.MagicMock()
token.split_contents.return_value = ['set', 'test', '=', 'tests']
gizmos_templatetags.set_var(None, token)

def test_set_var_error(self):
token = mock.MagicMock()
self.assertRaises(TemplateSyntaxError, gizmos_templatetags.set_var, None, token)

def test_isstring(self):
result = gizmos_templatetags.isstring(type('string'))

Expand Down
9 changes: 8 additions & 1 deletion tethys_gizmos/static/tethys_gizmos/js/tethys_map_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ var TETHYS_MAP_VIEW = (function() {
ZOOM_EXTENT = 'ZoomToExtent',
FULL_SCREEN = 'FullScreen',
MOUSE_POSITION = 'MousePosition',
SCALE_LINE = 'ScaleLine';
SCALE_LINE = 'ScaleLine',
OVERVIEW_MAP = 'OverviewMap';

var controls;

Expand Down Expand Up @@ -356,6 +357,9 @@ var TETHYS_MAP_VIEW = (function() {
else if (current_control === SCALE_LINE) {
m_map.addControl(new ol.control.ScaleLine());
}
else if (current_control === OVERVIEW_MAP) {
m_map.addControl(new ol.control.OverviewMap());
}

// Handle object case
} else if (typeof current_control === 'object') {
Expand All @@ -374,6 +378,9 @@ var TETHYS_MAP_VIEW = (function() {
else if (SCALE_LINE in current_control){
m_map.addControl(new ol.control.ScaleLine(current_control[SCALE_LINE]));
}
else if (OVERVIEW_MAP in current_control){
m_map.addControl(new ol.control.OverviewMap(current_control[OVERVIEW_MAP]));
}
else if (ZOOM_EXTENT in current_control){
var control_obj = current_control[ZOOM_EXTENT];

Expand Down
33 changes: 33 additions & 0 deletions tethys_gizmos/templatetags/tethys_gizmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,39 @@ def default(self, obj):
return super(HighchartsDateEncoder, self).default(obj)


class SetVarNode(template.Node):

def __init__(self, var_name, var_value):
self.var_names = var_name.split('.')
self.var_name = self.var_names.pop()
self.var_value = var_value

def render(self, context):
try:
value = template.Variable(self.var_value).resolve(context)
except template.VariableDoesNotExist:
value = ''

for name in self.var_names:
context = context[name]

context[self.var_name] = value

return ''


@register.tag(name='set')
def set_var(parser, token):
"""
{% set some_var = '123' %}
"""
parts = token.split_contents()
if len(parts) < 4:
raise template.TemplateSyntaxError("'set' tag must be of the form: {% set <var_name> = <var_value> %}")

return SetVarNode(parts[1], parts[3])


@register.filter(is_safe=True)
def isstring(value):
"""
Expand Down

0 comments on commit 8c096f7

Please sign in to comment.