Skip to content

Commit

Permalink
Merge f37e8ec into ead4aef
Browse files Browse the repository at this point in the history
  • Loading branch information
klikstermkd committed Dec 2, 2019
2 parents ead4aef + f37e8ec commit 679db0a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* This CKAN module integrates the Internet Explorer polyfill for <input type="color">. */

ckan.module('visualize-input-color-picker', function($) {
return {
initialize: function() {
window.nativeColorPicker.init(this.el.attr('id'));
}
};
});
155 changes: 155 additions & 0 deletions ckanext/visualize/fanstatic/js/vendor/native-color-picker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Internet Explorer polyfill for <input type="color">
// https://github.com/dciccale/nativeColorPicker

(function(window) {
var document = window.document,
nativeColorPicker = {
// initialized flag
started: false,

// start color
color: '#000000',

// inputs where plugin was initialized
inputs: {},

// flag to know if color input is supported
hasNativeColorSupport: false,

// inits the plugin on specified input
init: function(inputId) {
// start the plugin
this.start();

if (this.hasNativeColorSupport) {
return;
}

if (typeof inputId !== 'string') {
throw 'inputId have to be a string id selector';
}

// set the input
this.input =
(this.inputs[inputId] = this.inputs[inputId]) ||
document.getElementById(inputId);

if (!this.input) {
throw 'There was no input found with id: "' + inputId + '"';
}

// input defaults
this.input.value = this.color;
this.input.unselectable = 'on';
this.css(this.input, {
backgroundColor: this.color,
borderWidth: '0.4em 0.3em',
width: '3em',
cursor: 'default'
});

// register input event
this.input.onfocus = function() {
nativeColorPicker.onFocus(this.id);
};
},

// initialize once
start: function() {
// is already started
if (this.started) {
return;
}

// test if browser has native support for color input
try {
this.hasNativeColorSupport = !!(document.createElement('input').type =
'color');
} catch (e) {}

// no native support...
if (!this.hasNativeColorSupport) {
// create object element
var object_element = document.createElement('object');
object_element.classid = 'clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b';
// set attributes
object_element.id = 'colorHelperObj';
this.css(object_element, {
width: '0',
height: '0'
});
document.body.appendChild(object_element);
}
// mark as started
this.started = true;
},

// destroys the plugin
destroy: function(inputId) {
var i;
// destroy one input or all the plugin if no input id
if (typeof inputId === 'string') {
this.off(this.inputs[inputId]);
} else {
// remove helper object
document.body.removeChild(document.getElementById('colorHelperObj'));
// remove input events and styles
for (i in this.inputs) {
this.off(this.inputs[i]);
}
// mark not started
this.started = false;
}
},

off: function(input) {
input.onfocus = null;
this.css(input, {
backgroundColor: '',
borderWidth: '',
width: '',
cursor: ''
});
},

// input focus function
onFocus: function(inputId) {
this.input = this.inputs[inputId];
this.color = this.getColor();
this.input.value = this.color;
nativeColorPicker.css(this.input, {
backgroundColor: this.color,
color: this.color
});
this.input.blur();
},

// gets the color from the object
// and normalize it
getColor: function() {
// get decimal color, (passing the previous one)
// and change to hex
var hex = colorHelperObj
.ChooseColorDlg(this.color.replace(/#/, ''))
.toString(16);

// add extra zeroes if hex number is less than 6 digits
if (hex.length < 6) {
var tmpstr = '000000'.substring(0, 6 - hex.length);
hex = tmpstr.concat(hex);
}

return '#' + hex;
},

// set css properties
css: function(el, props) {
for (var prop in props) {
el.style[prop] = props[prop];
}
}
};

// expose to global
window.nativeColorPicker = nativeColorPicker;
})(window);
6 changes: 4 additions & 2 deletions ckanext/visualize/templates/admin/visualize_data.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% resource 'visualize/js/vendor/native-color-picker.js' %}
{% 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' %}
{% resource 'visualize/js/modules/visualize-chart-type.js' %}
{% resource 'visualize/js/modules/visualize-chart-icon-reset.js' %}
{% resource 'visualize/js/modules/visualize-input-color-picker.js' %}

{% extends 'admin/base.html' %}

Expand All @@ -18,7 +20,7 @@ <h5 for="color-pallete">Chart Colors:</h5>
<div id="color-pallete" class="color-pallete" data-module="visualize-colors-settings-remove">
{% for color in data.visualize_colors %}
<div class="color-input">
{{ form.input('color_' ~ loop.index, type='color', label=_('Color #' ~ loop.index), value=color['color_' ~ loop.index], classes=['item-color']) }}
{{ form.input('color_' ~ loop.index, type='color', label=_('Color #' ~ loop.index), value=color['color_' ~ loop.index], classes=['item-color'], attrs={'data-module': 'visualize-input-color-picker', 'class': 'form-control'}) }}
<button class="btn btn-sm btn-default color-remove" type="button">
<i class="fa fa-times" aria-hidden="true"></i>
</button>
Expand Down Expand Up @@ -152,4 +154,4 @@ <h2 class="module-heading">
{% endblock %}
</div>
</div>
{% endblock %}
{% endblock %}

0 comments on commit 679db0a

Please sign in to comment.