Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 1ca6415

Browse files
committed
mq support in ish controls
media query hunter support reads style.css for the unique max|min widths unit tests to support this
1 parent b80e45d commit 1ca6415

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

builder/media_hunter.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
(function () {
22
"use strict";
33

4+
var diveSync = require('diveSync'),
5+
path = require('path'),
6+
fs = require('fs-extra');
7+
48
var media_hunter = function(){
59

610
function findMediaQueries(patternlab){
711
patternlab.mediaQueries = [];
812

9-
10-
11-
13+
diveSync('./source/css', function(err, file){
14+
if(path.extname(file) === '.css'){
15+
var contents = fs.readFileSync(file, 'utf8');
16+
var safeContents = contents.replace("\r", " ").replace("\n", " ");
17+
var matches = safeContents.match(/\((min|max)-width:([ ]+)?(([0-9]{1,5})(\.[0-9]{1,20}|)(px|em))/g);
18+
for(var i = 0; i < matches.length; i++){
19+
var breakpoint = matches[i].substring(matches[i].indexOf(' ') + 1);
20+
if(patternlab.mediaQueries.indexOf(breakpoint) === -1){
21+
patternlab.mediaQueries.push(breakpoint);
22+
}
23+
}
24+
}
25+
});
26+
//alpha sort for now, but should meet most use-cases except greater than 100ems. you are using ems right?
27+
patternlab.mediaQueries.sort();
1228
}
1329

1430
return {
@@ -21,4 +37,23 @@
2137

2238
module.exports = media_hunter;
2339

24-
}());
40+
}());
41+
42+
// protected function gatherMQs() {
43+
44+
// $mqs = array();
45+
46+
// foreach(glob($this->sd."/css/*.css") as $filename) {
47+
// $data = file_get_contents($filename);
48+
// preg_match_all("/(min|max)-width:([ ]+)?(([0-9]{1,5})(\.[0-9]{1,20}|)(px|em))/",$data,$matches);
49+
// foreach ($matches[3] as $match) {
50+
// if (!in_array($match,$mqs)) {
51+
// $mqs[] = $match;
52+
// }
53+
// }
54+
// }
55+
56+
// sort($mqs);
57+
// return $mqs;
58+
59+
// }

builder/patternlab.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ var patternlab_engine = function(){
150150
patternlab.viewAllPaths = {};
151151

152152
//find mediaQueries
153-
// var media_hunter = new mh();
154-
// media_hunter.find_media_queries(patternlab);
155-
// console.log(patternlab.mediaQueries);
153+
var media_hunter = new mh();
154+
media_hunter.find_media_queries(patternlab);
156155

157156
//build the styleguide
158157
var styleguideTemplate = fs.readFileSync('./source/_patternlab-files/styleguide.mustache', 'utf8');
@@ -297,6 +296,9 @@ var patternlab_engine = function(){
297296

298297
//ishControls
299298
var ishControlsTemplate = fs.readFileSync('./source/_patternlab-files/partials/ishControls.mustache', 'utf8');
299+
console.log(patternlab.mediaQueries);
300+
patternlab.config.mqs = patternlab.mediaQueries;
301+
console.log(patternlab.config);
300302
var ishControlsPartialHtml = renderPattern(ishControlsTemplate, patternlab.config);
301303

302304
//patternPaths

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"ranndom": true,
1717
"disco": true,
1818
"hay": true,
19-
"mqs": false,
19+
"mqs": true,
2020
"find": false,
2121
"views-all": true,
2222
"views-annotations": true,

test/media_hunter_tests.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(function () {
2+
"use strict";
3+
4+
var MediaHunter = require('../builder/media_hunter');
5+
6+
exports['media_query hunter '] = {
7+
'test media hunter finds query' : function(test){
8+
9+
//setup pl object
10+
var pl = {};
11+
var mh = new MediaHunter();
12+
13+
mh.find_media_queries(pl);
14+
15+
test.equals(pl.mediaQueries.length, 6);
16+
17+
test.done();
18+
}
19+
};
20+
21+
}());

0 commit comments

Comments
 (0)