Commit b1dc9bf8 by Braden MacDonald

Editor: New "Width (%)" field

Also preserve data from old "Width (px)" and "Height (px)" fields, and convert from "Width (px)" to "Width (%)" as needed
parent 1a119b6e
...@@ -196,9 +196,8 @@ ...@@ -196,9 +196,8 @@
margin-right: 1%; margin-right: 1%;
} }
.xblock--drag-and-drop--editor .items-form .item-width, .xblock--drag-and-drop--editor .items-form .item-width {
.xblock--drag-and-drop--editor .items-form .item-height { width: 50px;
width: 40px;
} }
.xblock--drag-and-drop--editor .items-form .item-numerical-value, .xblock--drag-and-drop--editor .items-form .item-numerical-value,
......
...@@ -7,6 +7,13 @@ function DragAndDropEditBlock(runtime, element, params) { ...@@ -7,6 +7,13 @@ function DragAndDropEditBlock(runtime, element, params) {
// Make gettext available in Handlebars templates // Make gettext available in Handlebars templates
Handlebars.registerHelper('i18n', function(str) { return gettext(str); }); Handlebars.registerHelper('i18n', function(str) { return gettext(str); });
// Numeric rounding in Handlebars templates
Handlebars.registerHelper('singleDecimalFloat', function(value) {
if (value === "" || isNaN(Number(value))) {
return "";
}
return Number(value).toFixed(Number(value) == parseInt(value) ? 0 : 1);
});
var $element = $(element); var $element = $(element);
...@@ -331,32 +338,37 @@ function DragAndDropEditBlock(runtime, element, params) { ...@@ -331,32 +338,37 @@ function DragAndDropEditBlock(runtime, element, params) {
}, },
item: { item: {
count: 0, count: 0,
add: function(oldItem) { add: function(itemData) {
var $form = _fn.build.$el.items.form, var $form = _fn.build.$el.items.form,
tpl = _fn.tpl.itemInput, tpl = _fn.tpl.itemInput,
ctx = {}; ctx = {};
if (oldItem) ctx = oldItem; if (itemData) {
ctx = itemData;
ctx.dropdown = _fn.build.form.createDropdown(ctx.zone); if (itemData.size && parseInt(itemData.size.width) > 0) {
// Convert old fixed pixel width setting values (hard to
// Item width/height are ignored in new versions of the block, but // make mobile friendly) to new percentage format.
// preserve the data in case we change back to using those values. // Note itemData.size.width is a string like "380px" (it can
if (oldItem && oldItem.size && oldItem.size.width != 'auto') { // also be "auto" but that's excluded by the if condition above)
ctx.width = oldItem.size.width.substr(0, oldItem.size.width.length - 2); // Remove 'px' var bgImgWidth = _fn.build.$el.targetImage[0].naturalWidth;
} else { if (bgImgWidth > 0 && typeof ctx.widthPercent === "undefined") {
ctx.width = '0'; ctx.widthPercent = parseInt(itemData.size.width) / bgImgWidth * 100;
} }
if (oldItem && oldItem.size && oldItem.size.height != 'auto') { // Preserve the old-style data in case we need it again:
ctx.height = oldItem.size.height.substr(0, oldItem.size.height.length - 2); // Remove 'px' ctx.pixelWidth = itemData.size.width.substr(0, itemData.size.width.length - 2); // Remove 'px'
} else {
ctx.height = '0';
} }
if (itemData.size && parseInt(itemData.size.height) > 0) {
if (oldItem && oldItem.inputOptions) { // Item fixed pixel height is ignored in new versions of the
ctx.numericalValue = oldItem.inputOptions.value; // block, but preserve the data in case we need it again:
ctx.numericalMargin = oldItem.inputOptions.margin; ctx.pixelHeight = itemData.size.height.substr(0, itemData.size.height.length - 2); // Remove 'px'
} }
if (itemData.inputOptions) {
ctx.numericalValue = itemData.inputOptions.value;
ctx.numericalMargin = itemData.inputOptions.margin;
}
}
ctx.dropdown = _fn.build.form.createDropdown(ctx.zone);
_fn.build.form.item.count++; _fn.build.form.item.count++;
$form.append(tpl(ctx)); $form.append(tpl(ctx));
...@@ -399,16 +411,6 @@ function DragAndDropEditBlock(runtime, element, params) { ...@@ -399,16 +411,6 @@ function DragAndDropEditBlock(runtime, element, params) {
imageDescription = $el.find('.item-image-description').val(); imageDescription = $el.find('.item-image-description').val();
if (name.length > 0 || imageURL.length > 0) { if (name.length > 0 || imageURL.length > 0) {
// Item width/height are ignored, but preserve the data:
var width = $el.find('.item-width').val(),
height = $el.find('.item-height').val();
if (height === '0') height = 'auto';
else height = height + 'px';
if (width === '0') width = 'auto';
else width = width + 'px';
var data = { var data = {
displayName: name, displayName: name,
zone: $el.find('.zone-select').val(), zone: $el.find('.zone-select').val(),
...@@ -417,13 +419,18 @@ function DragAndDropEditBlock(runtime, element, params) { ...@@ -417,13 +419,18 @@ function DragAndDropEditBlock(runtime, element, params) {
correct: $el.find('.success-feedback').val(), correct: $el.find('.success-feedback').val(),
incorrect: $el.find('.error-feedback').val() incorrect: $el.find('.error-feedback').val()
}, },
size: { size: {}, // old size data (preserved if present but ignored)
width: width,
height: height
},
imageURL: imageURL, imageURL: imageURL,
imageDescription: imageDescription, imageDescription: imageDescription,
}; };
// Optional preferred width as a percentage of the bg image's width:
var widthPercent = $el.find('.item-width').val();
if (widthPercent && +widthPercent > 0) { data.widthPercent = widthPercent; }
// Item width/height in pixels are now ignored, but preserve the data:
var width = $el.find('.item-pixel-width').val();
if (width && +width > 0) { data.size.width = (+width) + 'px'; }
var height = $el.find('.item-pixel-height').val();
if (height && +height > 0) { data.size.height = (+height) + 'px'; }
var numValue = parseFloat($el.find('.item-numerical-value').val()); var numValue = parseFloat($el.find('.item-numerical-value').val());
var numMargin = parseFloat($el.find('.item-numerical-margin').val()); var numMargin = parseFloat($el.find('.item-numerical-margin').val());
......
...@@ -92,25 +92,15 @@ ...@@ -92,25 +92,15 @@
<textarea id="item-{{id}}-error-feedback" <textarea id="item-{{id}}-error-feedback"
class="error-feedback">{{ feedback.incorrect }}</textarea> class="error-feedback">{{ feedback.incorrect }}</textarea>
</div> </div>
<div class="row" style="display: none;"> <div class="row">
<label for="item-{{id}}-width-percent">{{i18n "Preferred width as a percentage of the background image width (or blank for automatic width):"}}</label>
<input type="number" id="item-{{id}}-width-percent" class="item-width" value="{{ singleDecimalFloat widthPercent }}" step="0.1" min="1" max="99" />%
<!-- <!--
width and height are no longer respected, so they are now hidden, but we are A fixed pixel width/height is no longer used, so they are now hidden, but we are
keeping the HTML and JS code intact so that the existing data will be preserved preserving the existing data:
until we can be sure that removing the width/height is OK for all courses that use
this block.
If we do add them back in, we should only allow setting "width". Height will be
detected automatically based on font/image size requirements.
--> -->
<label for="item-{{id}}-width">{{i18n "Width in pixels (0 for auto)"}}</label> <input type="hidden" class="item-pixel-width" value="{{ pixelWidth }}" />
<input type="text" <input type="hidden" class="item-pixel-height" value="{{ pixelHeight }}" />
id="item-{{id}}-width"
class="item-width"
value="{{ width }}" />
<label for="item-{{id}}-height">{{i18n "Height in pixels (0 for auto)"}}</label>
<input type="text"
id="item-{{id}}-height"
class="item-height"
value="{{ height }}" />
</div> </div>
<div class="row"> <div class="row">
<label for="item-{{id}}-numerical-value"> <label for="item-{{id}}-numerical-value">
......
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