Commit e703b3aa by Valera Rozuvan Committed by Alexander Kryklia

Refactoring GST.

parent 6bbcadb4
......@@ -141,25 +141,18 @@ define('Graph', ['logme'], function (logme) {
'end': 10,
'step': 0.1
};
logme('Default xrange:', xrange);
// The 'xrange' is a string containing two floating point numbers
// separated by a comma. The first number is the starting
// x-coordinate , the second number is the ending x-coordinate
if (typeof config.plot['xrange'] === 'string') {
logme('xrange is a string; xrange = "' + config.plot['xrange'] + '".');
xRangeStr = config.plot['xrange'];
xRangeBlobs = xRangeStr.split(',');
if (xRangeBlobs.length === 2) {
logme('xrange contains 2 blobs; 1 -> "' + xRangeBlobs[0] + '", 2 -> "' + xRangeBlobs[1] + '".');
tempNum = parseFloat(xRangeBlobs[0]);
if (isNaN(tempNum) === false) {
xrange.start = tempNum;
logme('First blob was parsed as a float. xrange.start = "' + xrange.start + '".');
} else {
logme('ERROR: First blob was parsed as a NaN.');
}
......@@ -167,8 +160,6 @@ define('Graph', ['logme'], function (logme) {
tempNum = parseFloat(xRangeBlobs[1]);
if (isNaN(tempNum) === false) {
xrange.end = tempNum;
logme('Second blob was parsed as a float. xrange.end = "' + xrange.end + '".');
} else {
logme('ERROR: Second blob was parsed as a NaN.');
}
......@@ -176,8 +167,6 @@ define('Graph', ['logme'], function (logme) {
if (xrange.start >= xrange.end) {
xrange.start = 0;
xrange.end = 10;
logme('xrange.start is greater than xrange.end - will set defaults. xrange.start = "' + xrange.start + '". xrange.end = "' + xrange.end + '".');
}
} else {
......@@ -191,17 +180,13 @@ define('Graph', ['logme'], function (logme) {
// we will use it to generate a 'step' - i.e. the distance (on
// x-axis) between two adjacent points.
if (typeof config.plot['num_points'] === 'string') {
logme('num_points is a string. num_points = "' + config.plot['num_points'] + '".');
tempNum = parseInt(config.plot['num_points'], 10);
if (
(isNaN(tempNum) === false) ||
(tempNum >= 2) &&
(tempNum <= 500)
) {
logme('num_points was parsed as a number. num_points = "' + tempNum + '".');
xrange.step = (xrange.end - xrange.start) / (tempNum - 1);
logme('xrange.step = "' + xrange.step + '".');
} else {
logme('ERROR: num_points was not parsed as a number, or num_points < 2, or num_points > 500.');
}
......
......@@ -16,23 +16,47 @@ define(
function GstMain(gstId) {
var config, gstClass, state;
// Get the JSON configuration, and parse it, and store as an object.
config = JSON.parse($('#' + gstId + '_json').html()).root;
// Get the JSON configuration, parse it, and store as an object.
try {
config = JSON.parse($('#' + gstId + '_json').html()).root;
} catch (err) {
logme('ERROR: could not parse config JSON.');
logme('$("#" + gstId + "_json").html() = ', $('#' + gstId + '_json').html());
logme('JSON.parse(...) = ', JSON.parse($('#' + gstId + '_json').html()));
logme('config = ', config);
return;
}
// Get the class name of the GST. All elements are assigned a class
// name that is based on the class name of the GST. For example, inputs
// are assigned a class name '{GST class name}_input'.
if (typeof config['@class'] !== 'string') {
logme('ERROR: Could not get the class name of GST.');
logme('config["@class"] = ', config['@class']);
return;
}
gstClass = config['@class'];
logme('gstClass: ' + gstClass);
// Parse the configuration settings for sliders and text inputs, and
// extract all of the defined constants (their names along with their
// initial values).
state = State(gstId, gstClass, config);
// Parse the configuration settings for parameters, and store them in a
// state object.
state = State(gstId, config);
// It is possible that something goes wrong while extracting parameters
// from the JSON config object. In this case, we will not continue.
if (state === undefined) {
logme('ERROR: The state object was not initialized properly.');
return;
}
// Create the sliders and the text inputs, attaching them to
// approriate constants.
Sliders(gstId, gstClass, state);
// appropriate parameters.
Sliders(gstId, state);
Inputs(gstId, gstClass, state);
// Configure and display the loop. Attach event for the graph to be
// Configure and display the graph. Attach event for the graph to be
// updated on any change of a slider or a text input.
Graph(gstId, config, state);
}
......
......@@ -10,14 +10,11 @@ define('Inputs', ['logme'], function (logme) {
allParamNames = state.getAllParameterNames();
console.log(allParamNames);
for (c1 = 0; c1 < allParamNames.length; c1 += 1) {
$('#' + gstId).children('.' + gstClass + '_input').each(function (index, value) {
var inputDiv, paramName;
paramName = allParamNames[c1];
inputDiv = $(value);
if (paramName === inputDiv.data('var')) {
......@@ -29,23 +26,17 @@ define('Inputs', ['logme'], function (logme) {
return;
function createInput(inputDiv, paramName) {
var paramObj, inputWidth, readOnly;
var paramObj, readOnly;
paramObj = state.getParamObj(paramName);
// We will define the width of the slider to a sensible default.
inputWidth = 400;
// Check that the retrieval went OK.
if (paramObj === undefined) {
logme('ERROR: Could not get a paramObj for parameter "' + paramName + '".');
// See if it was specified by the user.
if (isFinite(parseInt(inputDiv.data('el_width'))) === true) {
inputWidth = parseInt(inputDiv.data('el_width'));
return;
}
// Set the width of the element.
inputDiv.width(inputWidth);
inputDiv.css('display', 'inline-block');
readOnly = false;
if (inputDiv.attr('data-el_readonly').toLowerCase() === 'true') {
readOnly = true;
......@@ -79,9 +70,11 @@ define('Inputs', ['logme'], function (logme) {
'outline': 'none',
'cursor': 'text',
'height': '15px'
// 'width': '50px'
});
// Tell the parameter object from state that we are attaching a
// text input to it. Next time the parameter will be updated with
// a new value, tis input will also be updated.
paramObj.inputDivs.push(inputDiv);
return;
......
......@@ -5,7 +5,7 @@
define('Sliders', ['logme'], function (logme) {
return Sliders;
function Sliders(gstId, gstClass, state) {
function Sliders(gstId, state) {
var c1, paramName, allParamNames, sliderDiv;
allParamNames = state.getAllParameterNames();
......@@ -13,38 +13,33 @@ define('Sliders', ['logme'], function (logme) {
for (c1 = 0; c1 < allParamNames.length; c1 += 1) {
paramName = allParamNames[c1];
logme('Looking for slider with ID: ' + gstId + '_slider_' + paramName);
sliderDiv = $('#' + gstId + '_slider_' + paramName);
if (sliderDiv.length === 1) {
logme('Found one slider DIV with such an ID.');
createSlider(sliderDiv, paramName);
} else if (sliderDiv.length > 1) {
logme('ERROR: Found more than one slider for the parameter "' + paramName + '".');
logme('sliderDiv.length = ', sliderDiv.length);
} else {
logme('Did not find such a slider.');
logme('MESSAGE: Did not find a slider for the parameter "' + paramName + '".');
}
}
function createSlider(sliderDiv, paramName) {
var paramObj, sliderWidth;
var paramObj;
paramObj = state.getParamObj(paramName);
// We will define the width of the slider to a sensible default.
sliderWidth = 400;
// Check that the retrieval went OK.
if (paramObj === undefined) {
logme('ERROR: Could not get a paramObj for parameter "' + paramName + '".');
// See if it was specified by the user.
if (isFinite(parseInt(sliderDiv.data('el_width'))) === true) {
sliderWidth = parseInt(sliderDiv.data('el_width'));
return;
}
// Set the width of the element.
sliderDiv.width(sliderWidth);
sliderDiv.css('display', 'inline-block');
// Create a jQuery UI slider from the slider DIV. We will set
// starting parameters, and will also attach a handler to update
// the 'state' on the 'change' event.
// the 'state' on the 'slide' event.
sliderDiv.slider({
'min': paramObj.min,
'max': paramObj.max,
......@@ -55,6 +50,9 @@ define('Sliders', ['logme'], function (logme) {
'slide': sliderOnSlide
});
// Tell the parameter object stored in state that we have a slider
// that is attached to it. Next time when the parameter changes, it
// will also update the value of this slider.
paramObj.sliderDiv = sliderDiv;
return;
......@@ -65,7 +63,15 @@ define('Sliders', ['logme'], function (logme) {
// This will cause the plot to be redrawn each time after the user
// drags the slider handle and releases it.
function sliderOnSlide(event, ui) {
state.setParameterValue(paramName, ui.value, sliderDiv);
// Last parameter passed to setParameterValue() will be 'true'
// so that the function knows we are a slider, and it can
// change the our value back in the case when the new value is
// invalid for some reason.
if (state.setParameterValue(paramName, ui.value, sliderDiv, true) === undefined) {
logme('ERROR: Could not update the parameter named "' + paramName + '" with the value "' + ui.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