Skip to content
Snippets Groups Projects
Commit 1ec72927 authored by Jacob Poul Richardt's avatar Jacob Poul Richardt
Browse files

Fixed TwoDScaleK with no answer bug.

Added Scroll to question with invalid answer functionality.
parent 7bfca78c
Branches
No related tags found
No related merge requests found
Showing
with 112 additions and 22 deletions
......@@ -21,10 +21,13 @@ define(["require", "exports", "knockout", "jquery", "Highcharts", "HighchartsMor
TwoDScaleK.prototype.InitializeItems = function () {
var _this = this;
var answers = {};
var answer = this.GetAnswer();
if (answer.Scalings) {
this.GetAnswer().Scalings.forEach(function (scaling) {
var coordinates = scaling.Position.split(" ");
answers[scaling.Id] = { x: parseFloat(coordinates[0]), y: parseFloat(coordinates[1]) };
});
}
this.Items = this.GetInstrument("Items").Item.map(function (i) { return _this.CreateItem(i, answers[i.Id]); });
};
TwoDScaleK.prototype.InitializeChart = function () {
......@@ -88,7 +91,7 @@ define(["require", "exports", "knockout", "jquery", "Highcharts", "HighchartsMor
labels: { enabled: false }
},
tooltip: false,
series: this.Items.map(function (item) { return item.GraphData; })
series: this.Items.map(function (item) { return item.GraphData; }).filter(function (data) { return data != null; })
});
this._chart = jquery(this.ChartElement()).highcharts();
};
......
......@@ -35,11 +35,16 @@ class TwoDScaleK extends QuestionBase<{ Scalings: AnswerItem[]}>
{
var answers: {[key:string]:{x:number; y:number}} = {};
var answer = this.GetAnswer();
if (answer.Scalings)
{
this.GetAnswer().Scalings.forEach((scaling:AnswerItem) =>
{
var coordinates = scaling.Position.split(" ");
answers[scaling.Id] = { x: parseFloat(coordinates[0]), y: parseFloat(coordinates[1]) }
});
}
this.Items = (<any[]>this.GetInstrument("Items").Item).map(i => this.CreateItem(i, answers[i.Id]));
}
......@@ -106,7 +111,7 @@ class TwoDScaleK extends QuestionBase<{ Scalings: AnswerItem[]}>
labels: { enabled: false }
},
tooltip: false,
series: this.Items.map(item => item.GraphData)
series: this.Items.map(item => item.GraphData).filter(data => data != null)
});
this._chart = jquery(this.ChartElement()).highcharts();
}
......
......@@ -31,6 +31,7 @@ define(["require", "exports", "knockout", "Managers/Experiment", "Models/Slide"]
this.LoadNextSlide();
}
else {
this.SlideData().ScrollToFirstInvalidAnswer();
this.IsHighlighted(false);
setTimeout(function () { return _this.IsHighlighted(true); }, 50);
}
......
......@@ -60,6 +60,7 @@ class SlideShell
this.LoadNextSlide();
} else
{
this.SlideData().ScrollToFirstInvalidAnswer();
this.IsHighlighted(false);
setTimeout(() => this.IsHighlighted(true), 50);
}
......
<div data-bind="foreach: Questions" class="SlideContainer">
<!-- ko if: HasUIElement -->
<div data-bind="component: {name: Type, params: $data}, css: {DoesNotHaveValidAnswer: !HasValidAnswer()}"></div>
<div data-bind="component: {name: Type, params: $data}, css: {DoesNotHaveValidAnswer: !HasValidAnswer()}, ScrollTo: ScrollToCallback"></div>
<hr />
<!-- /ko -->
</div>
\ No newline at end of file
......@@ -7,6 +7,7 @@ define(["require", "exports", "knockout", "Models/Question", "Managers/Experimen
this.Questions = [];
this._slide = slide;
slide.SlideCompleted = function (callback) { return _this.SlideCompleted(callback); };
slide.ScrollToFirstInvalidAnswerCallback = function () { return _this.ScrollToFirstInvalidAnswer(); };
this.HaveActiveAnswersSet = knockout.computed(function () { return _this._activeAnsweSets() === 0; });
this.InitializeQuestions(slide.Questions);
}
......@@ -48,6 +49,11 @@ define(["require", "exports", "knockout", "Models/Question", "Managers/Experimen
else
completed();
};
Default.prototype.ScrollToFirstInvalidAnswer = function () {
var question = this.GetFirstQuestionWithoutValidAnswer();
if (question != null)
question.ScrollTo(ExperimentManager.ScrollToInvalidAnswerDuration);
};
Default.prototype.AnswerChanged = function (question) {
var _this = this;
if (question.HasValidAnswer()) {
......@@ -56,13 +62,15 @@ define(["require", "exports", "knockout", "Models/Question", "Managers/Experimen
}
this.CheckIfAllQuestionsAreAnswered();
};
Default.prototype.CheckIfAllQuestionsAreAnswered = function () {
var allQuestionsAnswered = true;
Default.prototype.GetFirstQuestionWithoutValidAnswer = function () {
for (var i = 0; i < this.Questions.length; i++) {
if (this.Questions[i].RequiresInput && !this.Questions[i].HasValidAnswer())
allQuestionsAnswered = false;
return this.Questions[i];
}
this._slide.CanGoToNextSlide(allQuestionsAnswered);
return null;
};
Default.prototype.CheckIfAllQuestionsAreAnswered = function () {
this._slide.CanGoToNextSlide(this.GetFirstQuestionWithoutValidAnswer() == null);
};
return Default;
})();
......
......@@ -18,6 +18,7 @@ class Default
{
this._slide = slide;
slide.SlideCompleted = callback => this.SlideCompleted(callback);
slide.ScrollToFirstInvalidAnswerCallback = () => this.ScrollToFirstInvalidAnswer();
this.HaveActiveAnswersSet = knockout.computed(() => this._activeAnsweSets() === 0);
......@@ -74,6 +75,13 @@ class Default
completed();
}
private ScrollToFirstInvalidAnswer():void
{
var question = this.GetFirstQuestionWithoutValidAnswer();
if(question != null) question.ScrollTo(ExperimentManager.ScrollToInvalidAnswerDuration);
}
private AnswerChanged(question: QuestionModel):void
{
if (question.HasValidAnswer())
......@@ -85,17 +93,19 @@ class Default
this.CheckIfAllQuestionsAreAnswered();
}
private CheckIfAllQuestionsAreAnswered():void
private GetFirstQuestionWithoutValidAnswer(): QuestionModel
{
var allQuestionsAnswered = true;
for (var i = 0; i < this.Questions.length; i++)
{
if (this.Questions[i].RequiresInput && !this.Questions[i].HasValidAnswer())
allQuestionsAnswered = false;
if (this.Questions[i].RequiresInput && !this.Questions[i].HasValidAnswer()) return this.Questions[i];
}
return null;
}
this._slide.CanGoToNextSlide(allQuestionsAnswered);
private CheckIfAllQuestionsAreAnswered():void
{
this._slide.CanGoToNextSlide(this.GetFirstQuestionWithoutValidAnswer() == null);
}
}
......
define(["require", "exports", "KnockoutBindings/CanvasContext", "KnockoutBindings/Element", "KnockoutBindings/ClickPosition", "KnockoutBindings/Height", "KnockoutBindings/Width"], function (require, exports, CanvasContext, Element, ClickPosition, Height, Width) {
define(["require", "exports", "KnockoutBindings/CanvasContext", "KnockoutBindings/Element", "KnockoutBindings/ClickPosition", "KnockoutBindings/ScrollTo", "KnockoutBindings/Height", "KnockoutBindings/Width"], function (require, exports, CanvasContext, Element, ClickPosition, ScrollTo, Height, Width) {
CanvasContext;
Element;
ClickPosition;
ScrollTo;
Height;
Width;
});
import CanvasContext = require("KnockoutBindings/CanvasContext"); CanvasContext;
import Element = require("KnockoutBindings/Element"); Element;
import ClickPosition = require("KnockoutBindings/ClickPosition"); ClickPosition;
import ScrollTo = require("KnockoutBindings/ScrollTo"); ScrollTo;
import Height = require("KnockoutBindings/Height"); Height;
import Width = require("KnockoutBindings/Width"); Width;
\ No newline at end of file
define(["require", "exports", "knockout"], function (require, exports, knockout) {
knockout.bindingHandlers["ScrollTo"] = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = valueAccessor();
var $element = jQuery(element);
var $document = jQuery("html, body");
value(function (duration) {
$document.animate({ scrollTop: $element.offset().top }, duration);
});
}
};
});
import jquery = require("jquery");
import knockout = require("knockout");
knockout.bindingHandlers["ScrollTo"] = {
init: (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) =>
{
},
update: (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) =>
{
var value: KnockoutObservable<(duration:number)=>void> = valueAccessor();
var $element = jQuery(element);
var $document = jQuery("html, body");
value(duration =>
{
$document.animate({scrollTop: $element.offset().top}, duration);
});
}
};
\ No newline at end of file
......@@ -10,6 +10,7 @@ define(["require", "exports", "knockout", "CockpitPortal", "Managers/Navigation"
this.FooterLabel = knockout.observable(null);
this.SlideName = knockout.observable("slide");
this.StyleSheet = knockout.observable(null);
this.ScrollToInvalidAnswerDuration = 2000;
this.CloseExperimentEnabled = knockout.observable(false);
this.CloseSlidesEnabled = knockout.observable(false);
this.GoToPreviousSlideEnabled = knockout.observable(true);
......
......@@ -15,6 +15,7 @@ class Experiment
public FooterLabel: KnockoutObservable<string> = knockout.observable(null);
public SlideName: KnockoutObservable<string> = knockout.observable("slide");
public StyleSheet: KnockoutObservable<string> = knockout.observable(null);
public ScrollToInvalidAnswerDuration:number = 2000;
public CloseExperimentEnabled: KnockoutObservable<boolean> = knockout.observable(false);
public CloseSlidesEnabled: KnockoutObservable<boolean> = knockout.observable(false);
......
......@@ -4,6 +4,7 @@ define(["require", "exports", "knockout", "Components/Questions/QuestionMap"], f
var _this = this;
this.Answer = knockout.observable();
this.HasValidAnswer = knockout.observable(false);
this.ScrollToCallback = knockout.observable(null);
var questionMap = QuestionMap.Get(question.Type);
this.Id = question.Id;
this.Type = questionMap.Type;
......@@ -22,6 +23,11 @@ define(["require", "exports", "knockout", "Components/Questions/QuestionMap"], f
this._loadedCallback();
this._loadedCallback = null;
};
Question.prototype.ScrollTo = function (duration) {
if (this.ScrollToCallback() == null)
throw new Error("SrollTo not ready");
this.ScrollToCallback()(duration);
};
return Question;
})();
return Question;
......
......@@ -12,6 +12,7 @@ class Question
public Answer: KnockoutObservable<CockpitPortal.IOutput> = knockout.observable<CockpitPortal.IOutput>();
public HasValidAnswer: KnockoutObservable<boolean> = knockout.observable(false);
public RequiresInput: boolean;
public ScrollToCallback: KnockoutObservable<(duration: number) => void> = knockout.observable(null);
private _loadedCallback:()=>void;
......@@ -39,6 +40,12 @@ class Question
this._loadedCallback();
this._loadedCallback = null;
}
public ScrollTo(duration:number):void
{
if (this.ScrollToCallback() == null) throw new Error("SrollTo not ready");
this.ScrollToCallback()(duration);
}
}
export = Question;
\ No newline at end of file
......@@ -13,6 +13,10 @@ define(["require", "exports"], function (require, exports) {
if (this.SlideCompleted != null)
this.SlideCompleted(callback);
};
Slide.prototype.ScrollToFirstInvalidAnswer = function () {
if (this.ScrollToFirstInvalidAnswerCallback != null)
this.ScrollToFirstInvalidAnswerCallback();
};
return Slide;
})();
return Slide;
......
......@@ -8,6 +8,7 @@ class Slide
public CanGoToNextSlide:KnockoutObservable<boolean>;
public Questions:CockpitPortal.IQuestion[];
public SlideCompleted: (completed: () => void) => void;
public ScrollToFirstInvalidAnswerCallback: () => void;
constructor(name: string, index: number = null, canGoToNextSlide: KnockoutObservable<boolean> = null, questions:CockpitPortal.IQuestion[] = null)
{
......@@ -21,6 +22,11 @@ class Slide
{
if (this.SlideCompleted != null) this.SlideCompleted(callback);
}
public ScrollToFirstInvalidAnswer():void
{
if (this.ScrollToFirstInvalidAnswerCallback != null) this.ScrollToFirstInvalidAnswerCallback();
}
}
export = Slide;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment