-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
300 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,66 @@ | ||
import json | ||
|
||
from ckan.controllers.admin import AdminController | ||
from ckan.lib.base import render | ||
from ckan.common import request | ||
from ckan import logic | ||
import ckan.lib.helpers as h | ||
from ckan.plugins import toolkit | ||
from ckan.common import config | ||
|
||
DEFAULT_COLORS = [ | ||
'#332288', | ||
'#117733', | ||
'#44aa99', | ||
'#88ccee', | ||
'#ddcc77', | ||
'#cc6677', | ||
'#aa4499', | ||
'#882255', | ||
'#dd4444' | ||
] | ||
|
||
|
||
class AdminController(AdminController): | ||
ctrl = 'ckanext.visualize.controllers.admin:AdminController' | ||
|
||
def visualize_data(self): | ||
return render('admin/visualize_data.html') | ||
data = request.POST | ||
if request.method == 'POST' and 'save' in data: | ||
data_dict = dict(data) | ||
del data_dict['save'] | ||
colors = [] | ||
|
||
for i, v in enumerate(data_dict): | ||
color = {} | ||
keyName = 'color_{0}'.format(i + 1) | ||
color[keyName] = data_dict.get(keyName) | ||
colors.append(color) | ||
|
||
data_dict = { | ||
'visualize_colors': json.dumps(colors) | ||
} | ||
data = toolkit.get_action( | ||
'config_option_update')({}, data_dict) | ||
h.flash_success(toolkit._('Successfully updated.')) | ||
h.redirect_to(controller=self.ctrl, action='visualize_data') | ||
|
||
# Initially set the colors from the default color palette. | ||
if not config.get('visualize_colors'): | ||
visualize_colors = [] | ||
for i, default_color in enumerate(DEFAULT_COLORS): | ||
color = {} | ||
keyName = 'color_{0}'.format(i + 1) | ||
color[keyName] = default_color | ||
visualize_colors.append(color) | ||
else: | ||
visualize_colors = json.loads(config.get('visualize_colors')) | ||
|
||
vars = { | ||
'data': { | ||
'visualize_colors': visualize_colors | ||
}, | ||
'errors': {} | ||
} | ||
|
||
return render('admin/visualize_data.html', extra_vars=vars) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ckanext/visualize/fanstatic/js/modules/visualize-colors-settings-add.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* This CKAN module handles adding a color from the admin settings. */ | ||
|
||
ckan.module('visualize-colors-settings-add', function($) { | ||
return { | ||
initialize: function() { | ||
var colorPallet = $('#color-pallet'); | ||
var inputGroup = [ | ||
'<div class="input-group">', | ||
'<div class="form-group item-color">', | ||
'<label class="control-label" for="color_$id$">Color #$id$</label>', | ||
'<div class="controls">', | ||
'<input id="color_$id$" type="color" name="color_$id$" value="#000000" placeholder="" class="form-control">', | ||
'</div>', | ||
'</div>', | ||
'<div class="input-group-btn">', | ||
'<button class="btn btn-default color-remove" type="button">', | ||
'<i class="fa fa-times"></i>', | ||
'</button>', | ||
'</div>', | ||
'</div>' | ||
].join(''); | ||
var colorsContainer = $('#color-pallet'); | ||
|
||
this.el.on('click', function(e) { | ||
colorsContainer.append( | ||
inputGroup.replace( | ||
new RegExp('\\$id\\$', 'g'), | ||
String(colorsContainer.children().length + 1) | ||
) | ||
); | ||
window.ckan.module.initializeElement(colorPallet[0]); | ||
}); | ||
} | ||
}; | ||
}); |
29 changes: 29 additions & 0 deletions
29
ckanext/visualize/fanstatic/js/modules/visualize-colors-settings-remove.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* This CKAN module handles removing a color from the admin settings. */ | ||
|
||
ckan.module('visualize-colors-settings-remove', function($) { | ||
return { | ||
initialize: function() { | ||
var colorRemoveButton = this.el.find('.color-remove'); | ||
$(colorRemoveButton).on( | ||
'click', | ||
function(e) { | ||
$(e.currentTarget) | ||
.closest('.input-group') | ||
.remove(); | ||
this.normalizeInputs(); | ||
}.bind(this) | ||
); | ||
}, | ||
normalizeInputs: function() { | ||
$.each(this.el.children(), function(i, inputGroup) { | ||
var label = $(inputGroup).find('.control-label'); | ||
label.attr('for', 'color_' + (i + 1)); | ||
label.text('Color #' + (i + 1)); | ||
|
||
var input = $(inputGroup).find('input[type="color"]'); | ||
input.attr('id', 'color_' + (i + 1)); | ||
input.attr('name', 'color_' + (i + 1)); | ||
}); | ||
} | ||
}; | ||
}); |
46 changes: 46 additions & 0 deletions
46
ckanext/visualize/fanstatic/js/modules/visualize-colors-settings-reset.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* This CKAN module handles resetting colors to default from the admin settings. */ | ||
|
||
ckan.module('visualize-colors-settings-reset', function($) { | ||
return { | ||
initialize: function() { | ||
var DEFAULT_COLORS = [ | ||
'#332288', | ||
'#117733', | ||
'#44aa99', | ||
'#88ccee', | ||
'#ddcc77', | ||
'#cc6677', | ||
'#aa4499', | ||
'#882255', | ||
'#dd4444' | ||
]; | ||
var inputGroup = [ | ||
'<div class="input-group">', | ||
'<div class="form-group item-color">', | ||
'<label class="control-label" for="color_$id$">Color #$id$</label>', | ||
'<div class="controls">', | ||
'<input id="color_$id$" type="color" name="color_$id$" value="$color$" placeholder="" class="form-control">', | ||
'</div>', | ||
'</div>', | ||
'<div class="input-group-btn">', | ||
'<button class="btn btn-default color-remove" type="button">', | ||
'<i class="fa fa-times"></i>', | ||
'</button>', | ||
'</div>', | ||
'</div>' | ||
].join(''); | ||
var colorsContainer = $('#color-pallet'); | ||
this.el.on('click', function(e) { | ||
colorsContainer.html(''); | ||
|
||
DEFAULT_COLORS.forEach(function(color, i) { | ||
colorsContainer.append( | ||
inputGroup | ||
.replace(new RegExp('\\$id\\$', 'g'), i + 1) | ||
.replace(new RegExp('\\$color\\$', 'g'), color) | ||
); | ||
}); | ||
}); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,55 @@ | ||
{% resource 'visualize/js/modules/visualize-colors-settings-add.js' %} | ||
{% resource 'visualize/js/modules/visualize-colors-settings-remove.js' %} | ||
{% resource 'visualize/js/modules/visualize-colors-settings-reset.js' %} | ||
|
||
{% extends 'admin/base.html' %} | ||
|
||
{% import 'macros/form.html' as form %} | ||
|
||
{% set action_url = h.url_for('ckanadmin_visualize_data') %} | ||
|
||
{% block primary_content_inner %} | ||
<form method="post" action="{{ action_url }}" class="form-horizontal col-sm-12"> | ||
<div>Form elements go here</div> | ||
<div class="form-actions"> | ||
<button type="submit" class="btn btn-primary" name="save">{{ _('Update') }}</button> | ||
<form method="post" action="{{ action_url }}" class="form-horizontal"> | ||
<div class="color-group"> | ||
<label class="control-label" for="color-pallet">Chart Colors</label> | ||
<div id="color-pallet" data-module="visualize-colors-settings-remove"> | ||
{% for color in data.visualize_colors %} | ||
<div class="input-group"> | ||
{{ form.input('color_' ~ loop.index, type='color', label=_('Color #' ~ loop.index), value=color['color_' ~ loop.index], classes=['item-color']) }} | ||
<div class="input-group-btn"> | ||
<button class="btn btn-default color-remove" type="button"> | ||
<i class="fa fa-times"></i> | ||
</button> | ||
</div> | ||
</div> | ||
</form> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
<div class="form-actions color-actions"> | ||
<button type="button" class="btn btn-default pull-left" data-module="visualize-colors-settings-add"><i | ||
class="fa fa-paint-brush"></i>{{ _('Add') }}</button> | ||
<button type="button" class="btn btn-default pull-right" data-module="visualize-colors-settings-reset"><i | ||
class="fa fa-sliders"></i>{{ _(' Set Default') }}</button> | ||
</div> | ||
<hr> | ||
<div class="form-actions"> | ||
<button type="submit" class="btn btn-danger pull-left" name="reset">{{ _('Reset') }}</button> | ||
<button type="submit" class="btn btn-primary" name="save">{{ _('Save') }}</button> | ||
</div> | ||
</form> | ||
|
||
{% endblock %} | ||
|
||
{% block secondary_content %} | ||
<div class="module module-narrow module-shallow"> | ||
<h2 class="module-heading"> | ||
<i class="fa fa-info-circle"></i> | ||
{{ _('Visualize options') }} | ||
</h2> | ||
<div class="module-content"> | ||
{% block admin_form_help %} | ||
<span>Help tips for options go here</span> | ||
{% endblock %} | ||
</div> | ||
<div class="module module-narrow module-shallow"> | ||
<h2 class="module-heading"> | ||
<i class="fa fa-info-circle"></i> | ||
{{ _('Visualize options') }} | ||
</h2> | ||
<div class="module-content"> | ||
{% block admin_form_help %} | ||
<span>Help tips for options go here</span> | ||
{% endblock %} | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters