Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function GraphWithToolbar(id, widthInUnits, absWidth, absHeight, correctAnswer) {
- this.innerStructure = new Object();
- this.containerId = id;
- this.boardWidthInUnits=widthInUnits;
- this.boardHeightInUnits=this.boardWidthInUnits;
- this.boardAbsWidth=absWidth;
- this.boardAbsHeight=absHeight;
- this.pixelsPerUnit = this.boardAbsWidth / this.boardWidthInUnits;
- this.selected_tool = "";
- this.selected_element;
- this.element_on_the_board = new Array();
- this.messages = {pointer : "Message not specified", point : "Message not specified", empty_point : "Message not specified", open_segment : "Infinite range: click on number line to insert closed point and infinite range to the left.", segment : "Message not specified", ray : "Message not specified", connected_points : "Create connected points", arrows : "Select all axe", nothing : "Not supported in prototype"};
- this.toolbarElements = ["pointer", "point", "empty_point", "open_segment", "segment", "ray", "connected_points", "arrows", "nothing", "minus", "messageBox", "redo", "undo", "remove"];
- this.tools = {pointer : function(new_element, x) {
- }, point : function(new_element, x) {
- new_element.mainPoints[0] = this.boardCreate(new_element, 'glider',[x,0,this.b1axisx],{style:5});
- }, empty_point : function(new_element, x) {
- new_element.mainPoints[0] = g = this.boardCreate(new_element, 'glider',[x,0,this.b1axisx],{style: 0, visible: true, strokeOpacity:0, highlightStrokeOpacity: 0 }, 'glider_opacity');
- this.boardCreateElement(new_element, 'circle',[g,1/this.pixelsPerUnit()*4], {style: 0});
- }, open_segment : function(new_element, x) {
- xr = this.pixelsPerUnit();
- end = this.boardCreate(new_element, 'point', [xr,0], {style:7, visible: false});
- var sq = [];
- new_element.mainPoints[0] = sq[0] = this.boardCreate(new_element, 'glider', [x,0,this.b1axisx], {style:4});
- var right = this.boardCreate(new_element, 'transform', [0.2,0], {type:'translate'});
- var up = this.boardCreate(new_element, 'transform', [0,0.3], {type:'translate'});
- var down = this.boardCreate(new_element, 'transform', [0,-0.3], {type:'translate'});
- sq[1] = this.boardCreate(new_element, 'point', [sq[0],[up,right]], {style:7, visible: false});
- sq[2] = this.boardCreate(new_element, 'point', [sq[0],up], {style:7, visible: false});
- sq[3] = this.boardCreate(new_element, 'point', [sq[0],[down]], {style:7, visible: false});
- sq[4] = this.boardCreate(new_element, 'point', [sq[0],[down,right]], {style:7, visible: false});
- this.boardCreateElement(new_element, 'line',[sq[1],sq[2]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[sq[2],sq[3]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[sq[3],sq[4]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[sq[0],end],{straightFirst: false, straightLast: true});
- }, segment : function(new_element, x) {
- var sq = [];
- new_element.mainPoints[0] = sq[0] = this.boardCreate(new_element, 'glider', [x - 1,0,this.b1axisx], {style:4});
- var right = this.boardCreate(new_element, 'transform', [0.2,0], {type:'translate'});
- var up = this.boardCreate(new_element, 'transform', [0,0.3], {type:'translate'});
- var down = this.boardCreate(new_element, 'transform', [0,-0.3], {type:'translate'});
- sq[1] = this.boardCreate(new_element, 'point', [sq[0],[up,right]], {style:7, visible: false});
- sq[2] = this.boardCreate(new_element, 'point', [sq[0],up], {style:7, visible: false});
- sq[3] = this.boardCreate(new_element, 'point', [sq[0],[down]], {style:7, visible: false});
- sq[4] = this.boardCreate(new_element, 'point', [sq[0],[down,right]], {style:7, visible: false});
- this.boardCreateElement(new_element, 'line',[sq[1],sq[2]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[sq[2],sq[3]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[sq[3],sq[4]],{straightFirst: false, straightLast: false});
- var tq = [];
- new_element.mainPoints[1] = tq[0] = this.boardCreate(new_element, 'glider', [x + 1,0,this.b1axisx], {style:4});
- var left = this.boardCreate(new_element, 'transform', [-0.2,0], {type:'translate'});
- var up = this.boardCreate(new_element, 'transform', [0,0.3], {type:'translate'});
- var down = this.boardCreate(new_element, 'transform', [0,-0.3], {type:'translate'});
- tq[1] = this.boardCreate(new_element, 'point', [tq[0],[up,left]], {style:7, visible: false});
- tq[2] = this.boardCreate(new_element, 'point', [tq[0],up], {style:7, visible: false});
- tq[3] = this.boardCreate(new_element, 'point', [tq[0],[down]], {style:7, visible: false});
- tq[4] = this.boardCreate(new_element, 'point', [tq[0],[down,left]], {style:7, visible: false});
- this.boardCreateElement(new_element, 'line',[tq[1],tq[2]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[tq[2],tq[3]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[tq[3],tq[4]],{straightFirst: false, straightLast: false});
- this.boardCreateElement(new_element, 'line',[tq[0],sq[0]],{straightFirst: false, straightLast: false});
- }, ray : function(new_element, x) {
- xr = this.pixelsPerUnit();
- end = this.boardCreate(new_element, 'point', [xr,0], {style:7, visible: false});
- new_element.mainPoints[0] = sq = this.boardCreate(new_element, 'glider', [x,0,this.b1axisx], {style:4});
- this.boardCreateElement(new_element, 'line',[sq,end],{straightFirst: false, straightLast: true});
- }, connected_points : function(new_element, x) {
- new_element.mainPoints[0] = gl1 = this.boardCreate(new_element, 'glider', [x + 1,0,this.b1axisx], {style:4});
- new_element.mainPoints[1] = gl2 = this.boardCreate(new_element, 'glider', [x - 1,0,this.b1axisx], {style:4});
- this.boardCreateElement(new_element, 'line',[gl1,gl2],{straightFirst: false, straightLast: false});
- }, arrows : function(new_element, x) {
- xr = this.pixelsPerUnit();
- begin = this.boardCreate(new_element, 'point', [-xr,0], {style:7, visible: false});
- end = this.boardCreate(new_element, 'point', [xr,0], {style:7, visible: false});
- this.boardCreateElement(new_element, 'line',[begin,end],{straightFirst: false, straightLast: true});
- }, nothing : function(new_element, x) {
- }
- };
- this.additionalElements = {minus : function () {
- var messageBox = this.innerStructure.messageBox;
- if (this.innerStructure.messageBox.style.display != 'none') {
- this.innerStructure.minus.src = "img/plus.png"
- this.innerStructure.messageBox.style.display = 'none';
- }
- else {
- this.innerStructure.minus.src = "img/minus.png"
- this.innerStructuremessageBox.style.display = '';
- }
- }, redo : function () {
- }, undo : function () {
- }
- };
- this.selectingByElementType = {glider : {selected:{strokeColor: '#00ff00', fillColor: '#00ff00'}, unselected: {strokeColor: '#ff0000', fillColor: '#ff0000'}}, circle : {selected:{strokeColor: '#00ff00'}, unselected: {strokeColor: '#0000ff'}}, line : {selected:{strokeColor: '#00ff00', fillColor: '#00ff00'}, unselected: {strokeColor: '#0000ff', fillColor: '#0000ff'}}};
- // not need when grading on server
- this.correctAnswer = correctAnswer;
- this.changeSelectedTool = function (id) {
- if (this.selected_tool != id ) {
- this.unselectElement();
- if (this.selected_tool != "") {
- this.UnselectTool(this.selected_tool);
- }
- this.selectTool(id);
- this.selected_tool = id;
- }
- }
- this.selectTool = function (id) {
- var element = this.innerStructure[id];
- element.src = "img/selected_toolbar_" + id + ".png";
- this.innerStructure.messageBox.innerHTML = this.messages[id];
- }
- this.UnselectTool = function(id) {
- var element = this.innerStructure[id];
- element.src = "img/toolbar_" + id + ".png";
- }
- this.selectElement = function(element) {
- for (var part=0; part< element.length; part++) {
- if (this.selectingByElementType[element[part].elementType]) {
- element[part].setProperty(this.selectingByElementType[element[part].elementType].selected);
- }
- }
- }
- this.unselectElement = function() {
- if (this.selected_element) {
- for (var part=0; part< this.selected_element.length; part++) {
- if (this.selectingByElementType[this.selected_element[part].elementType]) {
- this.selected_element[part].setProperty(this.selectingByElementType[this.selected_element[part].elementType].unselected);
- }
- }
- }
- }
- this.actionsStack = [];
- this.stackIndex = -1;
- this.addAction = function(action) {
- if (this.actionsStack.length > 0 && this.stackIndex == this.actionsStack.length - 1 && !this.isIncompleteDragAction(this.actionsStack[this.stackIndex])) {
- this.actionsStack.splice(this.stackIndex, 1);
- this.stackIndex--;
- }
- else if (this.stackIndex != this.actionsStack.length - 1) {
- this.actionsStack.splice(this.stackIndex, this.actionsStack.length - this.stackIndex);
- }
- this.actionsStack[this.actionsStack.length] = action;
- this.stackIndex++;
- }
- this.isIncompleteDragAction = function(action) {
- if (action.type='drag' && !action.upCoord) {
- return false;
- }
- return true;
- }
- this.mouseDownOnTheElement = function(event) {
- if (this.selected_tool != "pointer") {
- var element = this.findboardElement(event, true);
- if (element) {
- var x = this.getXCoord(event);
- this.addAction({type: "drag", downCoord: x, element : element});
- }
- }
- }
- this.mouseUpOnTheElement = function(event) {
- var x = getXCoord(event);
- if (this.isIncompleteDragAction(this.actionsStack[this.stackIndex]))) {
- if (this.actionsStack[this.stackIndex].downCoord != x) {
- this.actionsStack[this.stackIndex].upCoord = x;
- }
- if (this.selected_tool == "pointer") {
- var element = this.findboardElement(event, false);
- if (this.selected_element) {
- unselectElement();
- if (element && this.selected_element != element) {
- this.selected_element = element;
- this.selectElement(element);
- } else {
- this.selected_element = null;
- }
- }
- else if (element) {
- this.selected_element = element;
- this.selectElement(element);
- }
- }
- else if(this.selected_tool == "remove"){
- var element = findboardElement(event, false)
- var coords = [];
- this.addAction({type: "remove", element : element});
- }
- }
- else {
- this.addAction({type: "create", downCoord: x});
- var new_element = new Array();
- new_element.type = this.selected_tool;
- new_element.mainPoints = new Array();
- this.tools[this.selected_tool](new_element, x);
- this.element_on_the_board[this.element_on_the_board.length] = new_element;
- }
- }
- this.findboardElement = function(event, onlyGlider) {
- var cPos = this.board.getRelativeMouseCoordinates(event);
- var absPos = JXG.getPosition(event);
- var dx = absPos[0]-cPos[0];
- var dy = absPos[1]-cPos[1];
- for ( var boardElement in this.element_on_the_board) {
- for (var i = 0; i < this.element_on_the_board[this.boardElement].length; i++) {
- part = this.element_on_the_board[this.boardElement][i];
- if(JXG.exists(part.hasPoint) && part.visProp['visible'] && part.hasPoint(dx, dy)) {
- if (onlyGlider) {
- if (part.elementType == 'glider') {
- part;
- }
- else {
- continue;
- }
- }
- return this.element_on_the_board[this.boardElement];
- }
- }
- }
- }
- this.boardCreateElement = function(arrayForStore, type, elements, properties, selectingType) {
- if (!selectingType) {
- selectingType = type;
- }
- var currentElement = this.board.createElement(type, elements, properties);
- currentElement.elementType = selectingType;
- return arrayForStore[arrayForStore.length] = currentElement;
- }
- this.boardCreate = function(arrayForStore, type, elements, properties, selectingType) {
- if (!selectingType) {
- selectingType = type;
- }
- var currentElement = this.board.create(type, elements, properties);
- currentElement.elementType = selectingType;
- return arrayForStore[arrayForStore.length] = currentElement;
- }
- this.deleteSelected = function() {
- if (this.selected_element) {
- for (var i=0; i < this.selected_element.length; i++) {
- this.board.removeObject(this.selected_element[i]);
- delete(this.selected_element[i]);
- }
- for (var i=0; i < this.element_on_the_board.length; i++) {
- if (this.selected_element == this.element_on_the_board[i]) {
- this.element_on_the_board.splice(i, 1);
- delete(this.selected_element);
- break;
- }
- }
- this.selected_element = null;
- }
- }
- this.getXCoord = function(event) {
- var cPos = this.board.getRelativeMouseCoordinates(event);
- var absPos = JXG.getPosition(event);
- var dx = absPos[0]-cPos[0];
- var dy = absPos[1]-cPos[1];
- return new JXG.Coords(JXG.COORDS_BY_SCREEN, this.board.getScrCoordsOfMouse(dx,dy), this.board).usrCoords[1];
- }
- var rootElement = document.getElementById(this.containerId);
- var table = document.createElement('table');
- table.setAttribute('style', 'border: 1px solid gray;width:' + this.boardAbsWidth + 'px');
- rootElement.appendChild(table);
- var tr = document.createElement('tr');
- table.appendChild(tr);
- var self = this;
- for (var id in this.toolbarElements) {
- id = this.toolbarElements[id];
- var td = document.createElement('td');
- tr.appendChild(td);
- if (id == "messageBox") {
- this.innerStructure[id] = document.createElement('div');
- td.setAttribute('style', 'width:100%');
- }
- else {
- this.innerStructure[id] = document.createElement('img');
- if (this.tools[id]) {
- this.innerStructure[id].setAttribute('src', 'img/toolbar_' + id + '.png');
- var f = function() {var a = id; return function() {self.changeSelectedTool(a);};};
- this.innerStructure[id].onclick = f();
- }
- else {
- this.innerStructure[id].setAttribute('src', 'img/' + id + '.png');
- var f = function() {var a = id; return function() {self.changeSelectedTool(a);};};
- this.innerStructure[id].onclick = f();
- }
- }
- td.appendChild(this.innerStructure[id]);
- }
- tr = document.createElement('tr');
- table.appendChild(tr);
- var td = document.createElement('td');
- td.setAttribute("colspan", this.toolbarElements.length);
- tr.appendChild(td);
- var div = document.createElement('div');
- div.class = 'jxgbox';
- div.id = this.containerId + '_jxgbox';
- div.onclick = this.clickEvent;
- td.appendChild(div);
- this.check = function() {
- var match = new Object();
- if (this.element_on_the_board.length == this.correctAnswer.length) {
- for (var i = 0; i < this.element_on_the_board.length; i++) {
- var find = false;
- for (var j = 0; j < this.correctAnswer.length; j++) {
- if (!match[j] && this.correctAnswer[j].type == this.element_on_the_board[i].type && this.correctAnswer[j].coords.length == this.element_on_the_board[i].mainPoints.length) {
- find = true;
- for (var k = 0; k < this.correctAnswer[j].coords.length; k++) {
- if (Math.abs(this.correctAnswer[j].coords[k] - this.element_on_the_board[i].mainPoints[k].coords.usrCoords[1]) >= 0.2) {
- find = false;
- break;
- }
- }
- if (find) {
- match[j] = true;
- break;
- }
- }
- }
- if (!find) {
- return false;
- }
- }
- return true;
- }
- return false
- }
- this.board = JXG.JSXGraph.initBoard(this.containerId + '_jxgbox', {originX: this.boardAbsWidth/2, originY: this.boardAbsHeight/2, unitX: this.pixelsPerUnit, unitY: this.pixelsPerUnit, showCopyright: false, showNavigation : false});
- this.b1axisx = this.board.createElement('axis', [[0,0], [this.pixelsPerUnit,0]], {firstArrow: true, lastArrow: true });
- this.board.resizeContainer(this.boardAbsWidth, this.boardAbsHeight);
- this.board.fullUpdate();
- this.changeSelectedTool("pointer");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement