Skip to content

Commit

Permalink
GPII-1584: in midst of refactoring for svgDrawingArea grade, d3ViewCo…
Browse files Browse the repository at this point in the history
…mponent + svgDrawingArea unit tests passing
  • Loading branch information
waharnum committed Mar 15, 2016
1 parent 92f5e93 commit d8704ef
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 111 deletions.
80 changes: 2 additions & 78 deletions src/js/d3ViewComponent.js
@@ -1,5 +1,5 @@
/*
Copyright 2015 OCAD University
Copyright 2015-2016 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Expand Down Expand Up @@ -32,19 +32,7 @@ https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.tx
},
model: {
// Keeps track of D3 keys and their associated DOM elements
dataKeys: {},
svgTitle: "An SVG",
svgDescription: "An SVG image"
},
// Options controlling the behaviour of the base SVG drawing area
svgOptions: {
width: 500,
height: 500
},
selectors: {
title: ".floec-ca-d3ViewComponent-title",
description: ".floec-ca-d3ViewComponent-description",
svg: ".floec-ca-d3ViewComponent-svg"
dataKeys: {}
},
invokers: {
jQueryToD3: {
Expand Down Expand Up @@ -74,12 +62,7 @@ https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.tx
exitD3Elements: {
funcName: "floe.d3ViewComponent.exitD3Elements",
args: ["{arguments}.0", "{that}"]
},
createBaseSVGDrawingArea: {
funcName: "floe.d3ViewComponent.createBaseSVGDrawingArea",
args: ["{that}"]
}

}
});

Expand Down Expand Up @@ -233,63 +216,4 @@ https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.tx
d3ExitSelection.remove();
};

// Returns a properly formatted viewBox attribute that helps in making
// SVG elements scalable
// https://sarasoueidan.com/blog/svg-coordinate-systems/ has a lengthy
// explanation

floe.d3ViewComponent.getViewBoxConfiguration = function (x, y, width, height) {
return x + "," + y + "," + width + "," + height;
};

// Given width, height and class, creates an initial SVG to draw in,
// and appends tags and attributes for alternative representation
floe.d3ViewComponent.createBaseSVGDrawingArea = function (that) {
var container = that.container,
width = that.options.svgOptions.width,
height = that.options.svgOptions.height,
titleClass = that.classes.title,
descriptionClass = that.classes.description,
svgClass = that.classes.svg;

that.svg = that.jQueryToD3(container)
.append("svg")
.attr({
"width": width,
"height": height,
"class": svgClass,
"viewBox": floe.d3ViewComponent.getViewBoxConfiguration(0, 0, width, height),
// Set aria role to image - this causes the chart to appear as a
// static image to AT rather than as a number of separate
// images
"role": "img"
});

that.svg
.append("title")
.attr({
"class": titleClass
})
.text(that.model.svgTitle);

// Allocate ID for the title element
var svgTitleId = fluid.allocateSimpleId(that.locate("title"));

that.svg
.append("desc")
.attr({
"class": descriptionClass
})
.text(that.model.svgDescription);

// Allocate ID for the desc element
var svgDescId = fluid.allocateSimpleId(that.locate("description"));

// Now that they've been created and have IDs, explicitly associate SVG
// title & desc via aria-labelledby
that.svg.attr({
"aria-labelledby": svgTitleId + " " + svgDescId
});
};

})(jQuery, fluid);
115 changes: 115 additions & 0 deletions src/js/svgDrawingArea.js
@@ -0,0 +1,115 @@
/*
Copyright 2016 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.txt
*/

(function ($, fluid) {

"use strict";

// The D3 view component that is able to:
// 1. Convert jQuery DOM elements to D3 elements;
// 2. Attach D3 DOM event listeners;
// 3. Synthesize that.options.styles and that.options.selectors to combine elements with the same key into that.classes
// 4. Create a basic SVG drawing area for use by implementing grades that actually
// draw charts

fluid.defaults("floe.svgDrawingArea", {
gradeNames: ["floe.d3ViewComponent", "autoInit"],
members: {
classes: {
expander: {
funcName: "floe.d3ViewComponent.synthesizeClasses",
args: ["{that}.options.styles", "{that}.options.selectors"]
}
}
},
model: {
svgTitle: "An SVG",
svgDescription: "An SVG image"
},
// Options controlling the behaviour of the base SVG drawing area
svgOptions: {
width: 500,
height: 500
},
selectors: {
title: ".floec-ca-svgDrawingArea-title",
description: ".floec-ca-svgDrawingArea-description",
svg: ".floec-ca-svgDrawingArea-svg"
},
invokers: {
createBaseSVGDrawingArea: {
funcName: "floe.svgDrawingArea.createBaseSVGDrawingArea",
args: ["{that}"]
}

}
});

// Returns a properly formatted viewBox attribute that helps in making
// SVG elements scalable
// https://sarasoueidan.com/blog/svg-coordinate-systems/ has a lengthy
// explanation

floe.svgDrawingArea.getViewBoxConfiguration = function (x, y, width, height) {
return x + "," + y + "," + width + "," + height;
};

// Given width, height and class, creates an initial SVG to draw in,
// and appends tags and attributes for alternative representation
floe.svgDrawingArea.createBaseSVGDrawingArea = function (that) {
var container = that.container,
width = that.options.svgOptions.width,
height = that.options.svgOptions.height,
titleClass = that.classes.title,
descriptionClass = that.classes.description,
svgClass = that.classes.svg;

that.svg = that.jQueryToD3(container)
.append("svg")
.attr({
"width": width,
"height": height,
"class": svgClass,
"viewBox": floe.svgDrawingArea.getViewBoxConfiguration(0, 0, width, height),
// Set aria role to image - this causes the chart to appear as a
// static image to AT rather than as a number of separate
// images
"role": "img"
});

that.svg
.append("title")
.attr({
"class": titleClass
})
.text(that.model.svgTitle);

// Allocate ID for the title element
var svgTitleId = fluid.allocateSimpleId(that.locate("title"));

that.svg
.append("desc")
.attr({
"class": descriptionClass
})
.text(that.model.svgDescription);

// Allocate ID for the desc element
var svgDescId = fluid.allocateSimpleId(that.locate("description"));

// Now that they've been created and have IDs, explicitly associate SVG
// title & desc via aria-labelledby
that.svg.attr({
"aria-labelledby": svgTitleId + " " + svgDescId
});
};

})(jQuery, fluid);
3 changes: 1 addition & 2 deletions tests/html/d3ViewComponent-Tests.html
Expand Up @@ -15,6 +15,7 @@

<script src="../../src/js/d3Utils.js"></script>
<script src="../../src/js/d3ViewComponent.js"></script>
<script src="../../src/js/svgDrawingArea.js"></script>
<script src="../js/d3ViewComponentTests.js"></script>
</head>

Expand All @@ -27,8 +28,6 @@ <h2 id="qunit-userAgent"></h2>

<!-- Test HTML -->
<div class="floec-d3-api"></div>
<h2>floec-d3-baseSVG</h2>
<div class="floec-d3-baseSVG"></div>
<h2>floec-toggleCSSClassByIdTable</h2>
<div class="floec-toggleCSSClassById">
<table class="floec-toggleCSSClassByIdTable">
Expand Down
33 changes: 33 additions & 0 deletions tests/html/svgDrawingArea-Tests.html
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SVG Drawing Area Component Tests</title>

<link rel="stylesheet" media="screen" href="../lib/infusion/lib/qunit/css/qunit.css" />

<script src="../lib/infusion/lib/qunit/js/qunit.js"></script>
<script src="../../src/lib/infusion/infusion-custom.js"></script>
<script src="../../src/lib/ext/d3/d3.js"></script>
<script src="../lib/infusion/test-core/jqUnit/js/jqUnit.js"></script>
<script src="../lib/infusion/test-core/jqUnit/js/jqUnit-browser.js"></script>
<script src="../lib/infusion/test-core/utils/js/IoCTestUtils.js"></script>

<script src="../../src/js/d3Utils.js"></script>
<script src="../../src/js/d3ViewComponent.js"></script>
<script src="../../src/js/svgDrawingArea.js"></script>
<script src="../js/svgDrawingAreaTests.js"></script>
</head>

<body id="body">
<h1 id="qunit-header">SVG Drawing Area Component Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>

<!-- Test HTML -->
<h2>floec-svgDrawingArea-baseSVG</h2>
<div class="floec-svgDrawingArea-baseSVG"></div>
</body>
</html>
32 changes: 1 addition & 31 deletions tests/js/d3ViewComponentTests.js
@@ -1,5 +1,5 @@
/*
Copyright 2015 OCAD University
Copyright 2015-2016 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Expand Down Expand Up @@ -47,26 +47,6 @@ https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.tx
jqUnit.assertTrue("The mouseover listener for pie slices have been registered", that.mouseOverListenerCalled);
});

jqUnit.test("Test createBaseSVGDrawingArea function", function () {
jqUnit.expect(5);

var that = floe.tests.d3ViewComponent(".floec-d3-baseSVG");

that.createBaseSVGDrawingArea();

var svg = that.locate("svg"),
svgTitleId = that.locate("title").attr("id"),
svgDescId = that.locate("description").attr("id"),
svgAriaLabelledByAttr = svg.attr("aria-labelledby");

jqUnit.assertEquals("The width is set correctly on the SVG", that.options.svgOptions.width, Number(svg.attr("width")));
jqUnit.assertEquals("The height is set correctly on the SVG", that.options.svgOptions.height, Number(svg.attr("height")));

jqUnit.assertEquals("The SVG's title has been created", that.model.svgTitle, that.locate("title").text());
jqUnit.assertEquals("The SVG's description has been created", that.model.svgDescription, that.locate("description").text());
jqUnit.assertDeepEq("The SVG's title and description are connected through the aria-labelledby attribute of the SVG", svgAriaLabelledByAttr, svgTitleId + " " + svgDescId);
});

jqUnit.test("Test floe.d3ViewComponent.extractSelectorName()", function () {
jqUnit.expect(2);

Expand Down Expand Up @@ -295,14 +275,4 @@ https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.tx
});
});

jqUnit.test("Test getViewBoxConfiguration function", function () {
jqUnit.expect(1);

var expectedViewBoxConfig = "0,0,300,500";

var getViewBoxConfigurationOutput = floe.d3ViewComponent.getViewBoxConfiguration(0, 0, 300, 500);

jqUnit.assertEquals("function output matches expected format", expectedViewBoxConfig, getViewBoxConfigurationOutput);
});

})(jQuery, fluid);
48 changes: 48 additions & 0 deletions tests/js/svgDrawingAreaTests.js
@@ -0,0 +1,48 @@
/*
Copyright 2016 OCAD University
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://raw.githubusercontent.com/fluid-project/chartAuthoring/master/LICENSE.txt
*/

(function ($, fluid) {

"use strict";

fluid.registerNamespace("floe.tests");

jqUnit.test("Test createBaseSVGDrawingArea function", function () {
jqUnit.expect(5);

var that = floe.svgDrawingArea(".floec-svgDrawingArea-baseSVG");

that.createBaseSVGDrawingArea();

var svg = that.locate("svg"),
svgTitleId = that.locate("title").attr("id"),
svgDescId = that.locate("description").attr("id"),
svgAriaLabelledByAttr = svg.attr("aria-labelledby");

jqUnit.assertEquals("The width is set correctly on the SVG", that.options.svgOptions.width, Number(svg.attr("width")));
jqUnit.assertEquals("The height is set correctly on the SVG", that.options.svgOptions.height, Number(svg.attr("height")));

jqUnit.assertEquals("The SVG's title has been created", that.model.svgTitle, that.locate("title").text());
jqUnit.assertEquals("The SVG's description has been created", that.model.svgDescription, that.locate("description").text());
jqUnit.assertDeepEq("The SVG's title and description are connected through the aria-labelledby attribute of the SVG", svgAriaLabelledByAttr, svgTitleId + " " + svgDescId);
});

jqUnit.test("Test getViewBoxConfiguration function", function () {
jqUnit.expect(1);

var expectedViewBoxConfig = "0,0,300,500";

var getViewBoxConfigurationOutput = floe.svgDrawingArea.getViewBoxConfiguration(0, 0, 300, 500);

jqUnit.assertEquals("function output matches expected format", expectedViewBoxConfig, getViewBoxConfigurationOutput);
});

})(jQuery, fluid);

0 comments on commit d8704ef

Please sign in to comment.