Commit 3229aaf5 by cahrens

Call polyfill at right time.

parent aa41aed9
...@@ -29,9 +29,6 @@ class CMS.Views.ModuleEdit extends Backbone.View ...@@ -29,9 +29,6 @@ class CMS.Views.ModuleEdit extends Backbone.View
model: new Backbone.Model(metadataEditor.data('metadata')) model: new Backbone.Model(metadataEditor.data('metadata'))
}); });
#Manually runs polyfill for input number types to correct for Firefox non-support
#numberPolyfill()
# Need to update set "active" class on data editor if there is one. # Need to update set "active" class on data editor if there is one.
# If we are only showing settings, hide the data editor controls and update settings accordingly. # If we are only showing settings, hide the data editor controls and update settings accordingly.
if @hasDataEditor() if @hasDataEditor()
......
...@@ -100,12 +100,15 @@ CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({ ...@@ -100,12 +100,15 @@ CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({
showClearButton: function() { showClearButton: function() {
if (!this.$el.hasClass('is-set')) { if (!this.$el.hasClass('is-set')) {
this.$el.addClass('is-set'); this.$el.addClass('is-set');
// TODO: can we use toggleclass? this.getClearButton().removeClass('inactive');
this.$el.find('.setting-clear').removeClass('inactive'); this.getClearButton().addClass('active');
this.$el.find('.setting-clear').addClass('active');
} }
}, },
getClearButton: function () {
return this.$el.find('.setting-clear');
},
render: function () { render: function () {
if (!this.template) return; if (!this.template) return;
...@@ -116,9 +119,8 @@ CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({ ...@@ -116,9 +119,8 @@ CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({
} }
else { else {
this.$el.removeClass('is-set'); this.$el.removeClass('is-set');
// TODO: can we use toggleclass? this.getClearButton().addClass('inactive');
this.$el.find('.setting-clear').addClass('inactive'); this.getClearButton().removeClass('active');
this.$el.find('.setting-clear').removeClass('active');
} }
} }
}); });
...@@ -154,7 +156,7 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({ ...@@ -154,7 +156,7 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({
render: function () { render: function () {
CMS.Views.Metadata.AbstractEditor.prototype.render.apply(this); CMS.Views.Metadata.AbstractEditor.prototype.render.apply(this);
if (!this.inputAttributesSet) { if (!this.initialized) {
var min = "min"; var min = "min";
var max = "max"; var max = "max";
var step = "step"; var step = "step";
...@@ -175,7 +177,11 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({ ...@@ -175,7 +177,11 @@ CMS.Views.Metadata.Number = CMS.Views.Metadata.AbstractEditor.extend({
if (stepValue !== undefined) { if (stepValue !== undefined) {
this.$el.find('input').attr(step, stepValue); this.$el.find('input').attr(step, stepValue);
} }
this.inputAttributesSet = true;
// Manually runs polyfill for input number types to correct for Firefox non-support
this.$el.find('.setting-input-number').inputNumber();
this.initialized = true;
} }
}, },
......
...@@ -3,299 +3,296 @@ ...@@ -3,299 +3,296 @@
/* /*
HTML5 Number polyfill | Jonathan Stipe | https://github.com/jonstipe/number-polyfill HTML5 Number polyfill | Jonathan Stipe | https://github.com/jonstipe/number-polyfill
*/ */
function numberPolyfill() {
(function() { (function() {
(function($) { (function($) {
var i; var i;
i = document.createElement("input"); i = document.createElement("input");
i.setAttribute("type", "number"); i.setAttribute("type", "number");
if (i.type === "text") { if (i.type === "text") {
$.fn.inputNumber = function() { $.fn.inputNumber = function() {
var clipValues, decrement, domMouseScrollHandler, extractNumDecimalDigits, getParams, increment, matchStep, mouseWheelHandler; var clipValues, decrement, domMouseScrollHandler, extractNumDecimalDigits, getParams, increment, matchStep, mouseWheelHandler;
getParams = function(elem) { getParams = function(elem) {
var $elem, max, min, step, val; var $elem, max, min, step, val;
$elem = $(elem); $elem = $(elem);
step = $elem.attr('step'); step = $elem.attr('step');
min = $elem.attr('min'); min = $elem.attr('min');
max = $elem.attr('max'); max = $elem.attr('max');
val = parseFloat($elem.val()); val = parseFloat($elem.val());
step = /^-?\d+(?:\.\d+)?$/.test(step) ? parseFloat(step) : null; step = /^-?\d+(?:\.\d+)?$/.test(step) ? parseFloat(step) : null;
min = /^-?\d+(?:\.\d+)?$/.test(min) ? parseFloat(min) : null; min = /^-?\d+(?:\.\d+)?$/.test(min) ? parseFloat(min) : null;
max = /^-?\d+(?:\.\d+)?$/.test(max) ? parseFloat(max) : null; max = /^-?\d+(?:\.\d+)?$/.test(max) ? parseFloat(max) : null;
if (isNaN(val)) { if (isNaN(val)) {
val = min || 0; val = min || 0;
} }
return { return {
min: min, min: min,
max: max, max: max,
step: step, step: step,
val: val val: val
};
};
clipValues = function(value, min, max) {
if ((max != null) && value > max) {
return max;
} else if ((min != null) && value < min) {
return min;
} else {
return value;
}
}; };
extractNumDecimalDigits = function(input) { };
var num, raisedNum; clipValues = function(value, min, max) {
if (input != null) { if ((max != null) && value > max) {
num = 0; return max;
raisedNum = input; } else if ((min != null) && value < min) {
while (raisedNum !== Math.round(raisedNum)) { return min;
num += 1; } else {
raisedNum = input * Math.pow(10, num); return value;
} }
return num; };
} else { extractNumDecimalDigits = function(input) {
return 0; var num, raisedNum;
if (input != null) {
num = 0;
raisedNum = input;
while (raisedNum !== Math.round(raisedNum)) {
num += 1;
raisedNum = input * Math.pow(10, num);
} }
}; return num;
matchStep = function(value, min, max, step) { } else {
var mod, raiseTo, raisedMod, raisedStep, raisedStepDown, raisedStepUp, raisedValue, stepDecimalDigits, stepDown, stepUp; return 0;
stepDecimalDigits = extractNumDecimalDigits(step); }
if (step == null) { };
matchStep = function(value, min, max, step) {
var mod, raiseTo, raisedMod, raisedStep, raisedStepDown, raisedStepUp, raisedValue, stepDecimalDigits, stepDown, stepUp;
stepDecimalDigits = extractNumDecimalDigits(step);
if (step == null) {
return value;
} else if (stepDecimalDigits === 0) {
mod = (value - (min || 0)) % step;
if (mod === 0) {
return value; return value;
} else if (stepDecimalDigits === 0) { } else {
mod = (value - (min || 0)) % step; stepDown = value - mod;
if (mod === 0) { stepUp = stepDown + step;
return value; if ((stepUp > max) || ((value - stepDown) < (stepUp - value))) {
return stepDown;
} else { } else {
stepDown = value - mod; return stepUp;
stepUp = stepDown + step;
if ((stepUp > max) || ((value - stepDown) < (stepUp - value))) {
return stepDown;
} else {
return stepUp;
}
} }
}
} else {
raiseTo = Math.pow(10, stepDecimalDigits);
raisedStep = step * raiseTo;
raisedMod = (value - (min || 0)) * raiseTo % raisedStep;
if (raisedMod === 0) {
return value;
} else { } else {
raiseTo = Math.pow(10, stepDecimalDigits); raisedValue = value * raiseTo;
raisedStep = step * raiseTo; raisedStepDown = raisedValue - raisedMod;
raisedMod = (value - (min || 0)) * raiseTo % raisedStep; raisedStepUp = raisedStepDown + raisedStep;
if (raisedMod === 0) { if (((raisedStepUp / raiseTo) > max) || ((raisedValue - raisedStepDown) < (raisedStepUp - raisedValue))) {
return value; return raisedStepDown / raiseTo;
} else { } else {
raisedValue = value * raiseTo; return raisedStepUp / raiseTo;
raisedStepDown = raisedValue - raisedMod;
raisedStepUp = raisedStepDown + raisedStep;
if (((raisedStepUp / raiseTo) > max) || ((raisedValue - raisedStepDown) < (raisedStepUp - raisedValue))) {
return raisedStepDown / raiseTo;
} else {
return raisedStepUp / raiseTo;
}
} }
} }
}; }
increment = function(elem) { };
var newVal, params, raiseTo; increment = function(elem) {
if (!$(elem).is(":disabled")) { var newVal, params, raiseTo;
params = getParams(elem); if (!$(elem).is(":disabled")) {
raiseTo = Math.pow(10, Math.max(extractNumDecimalDigits(params['val']), extractNumDecimalDigits(params['step']))); params = getParams(elem);
newVal = (Math.round(params['val'] * raiseTo) + Math.round((params['step'] || 1) * raiseTo)) / raiseTo; raiseTo = Math.pow(10, Math.max(extractNumDecimalDigits(params['val']), extractNumDecimalDigits(params['step'])));
if ((params['max'] != null) && newVal > params['max']) { newVal = (Math.round(params['val'] * raiseTo) + Math.round((params['step'] || 1) * raiseTo)) / raiseTo;
newVal = params['max']; if ((params['max'] != null) && newVal > params['max']) {
} newVal = params['max'];
newVal = matchStep(newVal, params['min'], params['max'], params['step']);
$(elem).val(newVal).change();
} }
return null; newVal = matchStep(newVal, params['min'], params['max'], params['step']);
}; $(elem).val(newVal).change();
decrement = function(elem) { }
var newVal, params, raiseTo; return null;
if (!$(elem).is(":disabled")) { };
params = getParams(elem); decrement = function(elem) {
raiseTo = Math.pow(10, Math.max(extractNumDecimalDigits(params['val']), extractNumDecimalDigits(params['step']))); var newVal, params, raiseTo;
newVal = (Math.round(params['val'] * raiseTo) - Math.round((params['step'] || 1) * raiseTo)) / raiseTo; if (!$(elem).is(":disabled")) {
if ((params['min'] != null) && newVal < params['min']) { params = getParams(elem);
newVal = params['min']; raiseTo = Math.pow(10, Math.max(extractNumDecimalDigits(params['val']), extractNumDecimalDigits(params['step'])));
} newVal = (Math.round(params['val'] * raiseTo) - Math.round((params['step'] || 1) * raiseTo)) / raiseTo;
newVal = matchStep(newVal, params['min'], params['max'], params['step']); if ((params['min'] != null) && newVal < params['min']) {
$(elem).val(newVal).change(); newVal = params['min'];
} }
return null; newVal = matchStep(newVal, params['min'], params['max'], params['step']);
}; $(elem).val(newVal).change();
domMouseScrollHandler = function(e) { }
e.preventDefault(); return null;
if (e.originalEvent.detail < 0) { };
increment(this); domMouseScrollHandler = function(e) {
} else { e.preventDefault();
decrement(this); if (e.originalEvent.detail < 0) {
increment(this);
} else {
decrement(this);
}
return null;
};
mouseWheelHandler = function(e) {
e.preventDefault();
if (e.originalEvent.wheelDelta > 0) {
increment(this);
} else {
decrement(this);
}
return null;
};
$(this).filter('input[type="number"]').each(function() {
var $downBtn, $elem, $upBtn, attrMutationCallback, attrObserver, btnContainer, downBtn, elem, halfHeight, upBtn;
elem = this;
$elem = $(elem);
halfHeight = ($elem.outerHeight() / 2) + 'px';
upBtn = document.createElement('div');
downBtn = document.createElement('div');
$upBtn = $(upBtn);
$downBtn = $(downBtn);
btnContainer = document.createElement('div');
$upBtn.addClass('number-spin-btn number-spin-btn-up').css('height', halfHeight);
$downBtn.addClass('number-spin-btn number-spin-btn-down').css('height', halfHeight);
btnContainer.appendChild(upBtn);
btnContainer.appendChild(downBtn);
$(btnContainer).addClass('number-spin-btn-container').insertAfter(elem);
$elem.on({
focus: function(e) {
$elem.on({
DOMMouseScroll: domMouseScrollHandler,
mousewheel: mouseWheelHandler
});
return null;
},
blur: function(e) {
$elem.off({
DOMMouseScroll: domMouseScrollHandler,
mousewheel: mouseWheelHandler
});
return null;
},
keypress: function(e) {
var _ref, _ref1;
if (e.keyCode === 38) {
increment(this);
} else if (e.keyCode === 40) {
decrement(this);
} else if (((_ref = e.keyCode) !== 8 && _ref !== 9 && _ref !== 35 && _ref !== 36 && _ref !== 37 && _ref !== 39) && ((_ref1 = e.which) !== 45 && _ref1 !== 46 && _ref1 !== 48 && _ref1 !== 49 && _ref1 !== 50 && _ref1 !== 51 && _ref1 !== 52 && _ref1 !== 53 && _ref1 !== 54 && _ref1 !== 55 && _ref1 !== 56 && _ref1 !== 57)) {
e.preventDefault();
}
return null;
},
change: function(e) {
var newVal, params;
if (e.originalEvent != null) {
params = getParams(this);
newVal = clipValues(params['val'], params['min'], params['max']);
newVal = matchStep(newVal, params['min'], params['max'], params['step'], params['stepDecimal']);
$(this).val(newVal);
}
return null;
} }
});
$upBtn.on("mousedown", function(e) {
var releaseFunc, timeoutFunc;
increment(elem);
timeoutFunc = function(elem, incFunc) {
incFunc(elem);
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 10, elem, incFunc));
return null;
};
releaseFunc = function(e) {
window.clearTimeout($elem.data("timeoutID"));
$(document).off('mouseup', releaseFunc);
$upBtn.off('mouseleave', releaseFunc);
return null;
};
$(document).on('mouseup', releaseFunc);
$upBtn.on('mouseleave', releaseFunc);
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 700, elem, increment));
return null; return null;
}; });
mouseWheelHandler = function(e) { $downBtn.on("mousedown", function(e) {
e.preventDefault(); var releaseFunc, timeoutFunc;
if (e.originalEvent.wheelDelta > 0) { decrement(elem);
increment(this); timeoutFunc = function(elem, decFunc) {
} else { decFunc(elem);
decrement(this); $elem.data("timeoutID", window.setTimeout(timeoutFunc, 10, elem, decFunc));
} return null;
};
releaseFunc = function(e) {
window.clearTimeout($elem.data("timeoutID"));
$(document).off('mouseup', releaseFunc);
$downBtn.off('mouseleave', releaseFunc);
return null;
};
$(document).on('mouseup', releaseFunc);
$downBtn.on('mouseleave', releaseFunc);
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 700, elem, decrement));
return null; return null;
}; });
$(this).filter('input[type="number"]').each(function() { $elem.css("textAlign", 'right');
var $downBtn, $elem, $upBtn, attrMutationCallback, attrObserver, btnContainer, downBtn, elem, halfHeight, upBtn; if ($elem.css("opacity") !== "1") {
elem = this; $(btnContainer).css("opacity", $elem.css("opacity"));
$elem = $(elem); }
halfHeight = ($elem.outerHeight() / 2) + 'px'; if ($elem.css("visibility") !== "visible") {
upBtn = document.createElement('div'); $(btnContainer).css("visibility", $elem.css("visibility"));
downBtn = document.createElement('div'); }
$upBtn = $(upBtn); if (elem.style.display !== "") {
$downBtn = $(downBtn); $(btnContainer).css("display", $elem.css("display"));
btnContainer = document.createElement('div'); }
$upBtn.addClass('number-spin-btn number-spin-btn-up').css('height', halfHeight); if ((typeof WebKitMutationObserver !== "undefined" && WebKitMutationObserver !== null) || (typeof MutationObserver !== "undefined" && MutationObserver !== null)) {
$downBtn.addClass('number-spin-btn number-spin-btn-down').css('height', halfHeight); attrMutationCallback = function(mutations, observer) {
btnContainer.appendChild(upBtn); var mutation, _i, _len;
btnContainer.appendChild(downBtn); for (_i = 0, _len = mutations.length; _i < _len; _i++) {
$(btnContainer).addClass('number-spin-btn-container').insertAfter(elem); mutation = mutations[_i];
$elem.on({ if (mutation.type === "attributes") {
focus: function(e) { if (mutation.attributeName === "class") {
$elem.on({ $(btnContainer).removeClass(mutation.oldValue).addClass(elem.className);
DOMMouseScroll: domMouseScrollHandler, } else if (mutation.attributeName === "style") {
mousewheel: mouseWheelHandler $(btnContainer).css({
}); "opacity": elem.style.opacity,
return null; "visibility": elem.style.visibility,
}, "display": elem.style.display
blur: function(e) { });
$elem.off({ }
DOMMouseScroll: domMouseScrollHandler,
mousewheel: mouseWheelHandler
});
return null;
},
keypress: function(e) {
var _ref, _ref1;
if (e.keyCode === 38) {
increment(this);
} else if (e.keyCode === 40) {
decrement(this);
} else if (((_ref = e.keyCode) !== 8 && _ref !== 9 && _ref !== 35 && _ref !== 36 && _ref !== 37 && _ref !== 39) && ((_ref1 = e.which) !== 45 && _ref1 !== 46 && _ref1 !== 48 && _ref1 !== 49 && _ref1 !== 50 && _ref1 !== 51 && _ref1 !== 52 && _ref1 !== 53 && _ref1 !== 54 && _ref1 !== 55 && _ref1 !== 56 && _ref1 !== 57)) {
e.preventDefault();
}
return null;
},
change: function(e) {
var newVal, params;
if (e.originalEvent != null) {
params = getParams(this);
newVal = clipValues(params['val'], params['min'], params['max']);
newVal = matchStep(newVal, params['min'], params['max'], params['step'], params['stepDecimal']);
$(this).val(newVal);
} }
return null;
} }
});
$upBtn.on("mousedown", function(e) {
var releaseFunc, timeoutFunc;
increment(elem);
timeoutFunc = function(elem, incFunc) {
incFunc(elem);
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 10, elem, incFunc));
return null;
};
releaseFunc = function(e) {
window.clearTimeout($elem.data("timeoutID"));
$(document).off('mouseup', releaseFunc);
$upBtn.off('mouseleave', releaseFunc);
return null;
};
$(document).on('mouseup', releaseFunc);
$upBtn.on('mouseleave', releaseFunc);
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 700, elem, increment));
return null; return null;
};
attrObserver = (typeof WebKitMutationObserver !== "undefined" && WebKitMutationObserver !== null) ? new WebKitMutationObserver(attrMutationCallback) : ((typeof MutationObserver !== "undefined" && MutationObserver !== null) ? new MutationObserver(attrMutationCallback) : null);
attrObserver.observe(elem, {
attributes: true,
attributeOldValue: true,
attributeFilter: ["class", "style"]
}); });
$downBtn.on("mousedown", function(e) { } else if (typeof MutationEvent !== "undefined" && MutationEvent !== null) {
var releaseFunc, timeoutFunc; $elem.on("DOMAttrModified", function(evt) {
decrement(elem); if (evt.originalEvent.attrName === "class") {
timeoutFunc = function(elem, decFunc) { $(btnContainer).removeClass(evt.originalEvent.prevValue).addClass(evt.originalEvent.newValue);
decFunc(elem); } else if (evt.originalEvent.attrName === "style") {
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 10, elem, decFunc)); $(btnContainer).css({
return null; "display": elem.style.display,
}; "visibility": elem.style.visibility,
releaseFunc = function(e) { "opacity": elem.style.opacity
window.clearTimeout($elem.data("timeoutID")); });
$(document).off('mouseup', releaseFunc); }
$downBtn.off('mouseleave', releaseFunc);
return null;
};
$(document).on('mouseup', releaseFunc);
$downBtn.on('mouseleave', releaseFunc);
$elem.data("timeoutID", window.setTimeout(timeoutFunc, 700, elem, decrement));
return null; return null;
}); });
$elem.css("textAlign", 'right'); }
if ($elem.css("opacity") !== "1") {
$(btnContainer).css("opacity", $elem.css("opacity"));
}
if ($elem.css("visibility") !== "visible") {
$(btnContainer).css("visibility", $elem.css("visibility"));
}
if (elem.style.display !== "") {
$(btnContainer).css("display", $elem.css("display"));
}
if ((typeof WebKitMutationObserver !== "undefined" && WebKitMutationObserver !== null) || (typeof MutationObserver !== "undefined" && MutationObserver !== null)) {
attrMutationCallback = function(mutations, observer) {
var mutation, _i, _len;
for (_i = 0, _len = mutations.length; _i < _len; _i++) {
mutation = mutations[_i];
if (mutation.type === "attributes") {
if (mutation.attributeName === "class") {
$(btnContainer).removeClass(mutation.oldValue).addClass(elem.className);
} else if (mutation.attributeName === "style") {
$(btnContainer).css({
"opacity": elem.style.opacity,
"visibility": elem.style.visibility,
"display": elem.style.display
});
}
}
}
return null;
};
attrObserver = (typeof WebKitMutationObserver !== "undefined" && WebKitMutationObserver !== null) ? new WebKitMutationObserver(attrMutationCallback) : ((typeof MutationObserver !== "undefined" && MutationObserver !== null) ? new MutationObserver(attrMutationCallback) : null);
attrObserver.observe(elem, {
attributes: true,
attributeOldValue: true,
attributeFilter: ["class", "style"]
});
} else if (typeof MutationEvent !== "undefined" && MutationEvent !== null) {
$elem.on("DOMAttrModified", function(evt) {
if (evt.originalEvent.attrName === "class") {
$(btnContainer).removeClass(evt.originalEvent.prevValue).addClass(evt.originalEvent.newValue);
} else if (evt.originalEvent.attrName === "style") {
$(btnContainer).css({
"display": elem.style.display,
"visibility": elem.style.visibility,
"opacity": elem.style.opacity
});
}
return null;
});
}
return null;
});
return $(this);
};
$(function() {
$('input[type="number"]').inputNumber();
return null; return null;
}); });
null; return $(this);
} else { };
$.fn.inputNumber = function() { $(function() {
return $(this); $('input[type="number"]').inputNumber();
}; return null;
null; });
} null;
return null; } else {
})(jQuery); $.fn.inputNumber = function() {
return $(this);
}).call(this); };
null;
}
return null;
})(jQuery);
}; }).call(this);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment