Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 46b5500

Browse files
fix(layout): corrections to material.core.layout module and tests
fixes to unintended, premature commit of `material.core.layout` code and tests.
1 parent 2fe726c commit 46b5500

File tree

4 files changed

+159
-88
lines changed

4 files changed

+159
-88
lines changed

src/core/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ angular
77
'ngAnimate',
88
'material.animate',
99
'material.core.gestures',
10+
'material.core.layout',
1011
'material.core.theming'
1112
])
1213
.directive('mdTemplate', MdTemplateDirective)

src/core/services/layout/layouts.js renamed to src/core/services/layout/layout.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,46 @@
22

33
'use strict';
44

5-
angular.module('material.layouts', ['material.core'])
5+
/**
6+
*
7+
* The original ngMaterial Layout solution used attribute selectors and CSS.
8+
*
9+
* ```html
10+
* <div layout="column"> My Content </div>
11+
* ```
12+
*
13+
* ```css
14+
* [layout] {
15+
* box-sizing: border-box;
16+
* display:flex;
17+
* }
18+
* [layout=column] {
19+
* flex-direction : column
20+
* }
21+
* ```
22+
*
23+
* Use of attribute selectors creates significant performance impacts in some
24+
* browsers... mainly IE.
25+
*
26+
* This module registers directives that allow the same layout attributes to be
27+
* interpreted and converted to class selectors. The directive will add equivalent classes to each element that
28+
* contains a Layout directive.
29+
*
30+
* ```html
31+
* <div layout="column" class="layout layout-column"> My Content </div>
32+
*```
33+
*
34+
* ```css
35+
* .layout {
36+
* box-sizing: border-box;
37+
* display:flex;
38+
* }
39+
* .layout-column {
40+
* flex-direction : column
41+
* }
42+
* ```
43+
*/
44+
angular.module('material.core.layout', [ ])
645

746
// Attribute directives with optional value(s)
847

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
describe('layout directives', function() {
2+
3+
beforeEach(module('material.core'));
4+
5+
describe('expecting layout classes', function() {
6+
7+
var suffixes = ['sm', 'gt-sm', 'md', 'gt-md', 'lg', 'gt-lg'];
8+
9+
var directionValues = ['row', 'column'];
10+
var flexOrderValues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
11+
var flexValues = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 33, 34, 66, 67];
12+
var offsetValues = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 33, 34, 66, 67];
13+
var alignmentValues = [
14+
'center', 'center center', 'center start', 'center end',
15+
'end', 'end-center', 'end start', 'end end',
16+
'space-around', 'space-around center', 'space-around start', 'space-around end',
17+
'space-between', 'space-between center', 'space-between start', 'space-between end',
18+
'center center', 'start center', 'end center', 'space-between center', 'space-around center',
19+
'center start', 'start start', 'end start', 'space-between start', 'space-around start',
20+
'center end', 'start end', 'end end', 'space-between end', 'space-around end'
21+
];
22+
23+
var mappings = [
24+
{ attribute: 'layout', suffixes: suffixes, values: directionValues, addDirectiveAsClass: true, testStandAlone: true },
25+
{ attribute: 'layout-align', suffixes: suffixes, values: alignmentValues },
26+
{ attribute: 'layout-padding', testStandAlone: true },
27+
{ attribute: 'layout-margin', testStandAlone: true },
28+
{ attribute: 'layout-wrap', testStandAlone: true },
29+
{ attribute: 'layout-fill', testStandAlone: true },
30+
{ attribute: 'flex', suffixes: suffixes, values: flexValues, addDirectiveAsClass: true, testStandAlone: true},
31+
{ attribute: 'flex-order', suffixes: suffixes, values: flexOrderValues },
32+
{ attribute: 'offset', suffixes: suffixes, values: offsetValues },
33+
{ attribute: 'hide', suffixes: suffixes, testStandAlone: true },
34+
{ attribute: 'show', suffixes: suffixes, testStandAlone: true }
35+
];
36+
37+
// Run all the tests; iterating the mappings...
38+
39+
for (var i = 0; i < mappings.length; i++) {
40+
var map = mappings[i];
41+
42+
if (map.testStandAlone) testMapping(map.attribute, map.attribute);
43+
if (map.suffixes) testWithSuffix(map.attribute, map.suffixes, map.values, map.testStandAlone, map.addDirectiveAsClass);
44+
if (map.values) testWithSuffixAndValue(map.attribute, map.values, undefined, map.addDirectiveAsClass);
45+
}
46+
47+
/**
48+
*
49+
*/
50+
function testMapping(attribute, expectedClass) {
51+
it('should fail if the class ' + expectedClass + ' was not added for attribute ' + attribute, inject(function($compile, $rootScope) {
52+
var element = $compile('<div ' + attribute + '>Layout</div>')($rootScope);
53+
expect(element.hasClass(expectedClass)).toBe(true);
54+
}));
55+
}
56+
57+
/**
58+
*
59+
*/
60+
function testWithSuffixAndValue(attribute, values, suffix, addDirectiveAsClass) {
61+
62+
for (var j = 0; j < values.length; j++) {
63+
var value = values[j].toString();
64+
var attr = suffix ? attribute + '-' + suffix : attribute;
65+
66+
var attrWithValue = buildAttributeWithValue(attr, value);
67+
var expectedClass = buildExpectedClass(attr, value);
68+
69+
// Run each test.
70+
testMapping(attrWithValue, expectedClass);
71+
}
72+
73+
/**
74+
* Build string of expected classes that should be added to the
75+
* DOM element.
76+
*
77+
* Convert directive with value to classes
78+
*
79+
* @param attrClass String full attribute name; eg 'layout-gt-lg'
80+
* @param attrValue String HTML directive; eg "column"
81+
*
82+
* @returns String to be used with element.addClass(...); eg `layout-gt-lg layout-gt-lg-column`
83+
*/
84+
function buildExpectedClass(attrClass, attrValue) {
85+
if (addDirectiveAsClass) attrClass += ' ' + attrClass;
86+
return attrClass + "-" + attrValue.replace(/\s+/g, "-");
87+
}
88+
89+
/**
90+
* Build full string of expected directive with its value
91+
* Note: The expected class always starts with the
92+
* attribute name, add the suffix if any.
93+
*
94+
* @param attrClass String full attribute name; eg 'layout-gt-lg'
95+
* @param attrValue String HTML directive; eg "column"
96+
*
97+
* @returns String like `layout-gt-lg="column"`
98+
*/
99+
function buildAttributeWithValue(attr, value) {
100+
return attr + '="' + value + '"';
101+
}
102+
}
103+
104+
/**
105+
*
106+
*/
107+
function testWithSuffix(attribute, suffixes, values, testStandAlone, addDirectiveAsClass) {
108+
for (var j = 0; j < suffixes.length; j++) {
109+
var suffix = suffixes[j];
110+
var attr = attribute + '-' + suffix;
111+
112+
if (testStandAlone) testMapping(attr, attr);
113+
if (values) testWithSuffixAndValue(attribute, values, suffix, addDirectiveAsClass);
114+
}
115+
}
116+
});
117+
118+
});

src/core/services/layout/layouts.spec.js

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)