Skip to content

Commit

Permalink
Added polyfill for CustomEvent, res.js should support IE9+ now
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Belovarich committed Oct 3, 2014
1 parent 30cc3c6 commit 959db8f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 79 deletions.
30 changes: 15 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE>
<!DOCTYPE>
<html>
<body>
<style>
Expand Down Expand Up @@ -55,34 +55,34 @@
$('.box:eq(2)').text('os: '+r.os);
$('.box:eq(3)').text('columns: '+r.grid.cols);
switch(r.state) {

case 'mobile':
$('.box').each(function(){
$(this).css('width',r.grid.colSpan[4]);
$(this).css('top',80*$(this).index()+'px');
$(this).css('left',r.grid.col[0]);
$(this).css('left',r.grid.col[0]);
});
break;
case 'small':
$('.box').css('top','0px');
$('.box').css('top','0px');
$('.box').css('width',r.grid.colSpan[6]);
$('.box:eq(0)').css('left',r.grid.col[0]);
$('.box:eq(1)').css('left',r.grid.col[6]);
$('.box:eq(2)').css('top',$('.box:eq(0)').height()+20);
$('.box:eq(2)').css('left',r.grid.col[0]);
$('.box:eq(3)').css('top',$('.box:eq(0)').height()+20);
$('.box:eq(3)').css('left',r.grid.col[6]);
$('.box:eq(2)').css('left',r.grid.col[0]);
$('.box:eq(3)').css('top',$('.box:eq(0)').height()+20);
$('.box:eq(3)').css('left',r.grid.col[6]);
break;
case 'desktop':
case 'desktop':
$('.box').css('width',r.grid.colSpan[4]);
$('.box').css('top','0px');
$('.box').css('top','0px');
$('.box:eq(0)').css('left',r.grid.col[0]);
$('.box:eq(1)').css('left',r.grid.col[4]);
$('.box:eq(2)').css('left',r.grid.col[8]);
$('.box:eq(3)').css('left',r.grid.col[12]);
break;
}
});
$('.box:eq(1)').css('left',r.grid.col[4]);
$('.box:eq(2)').css('left',r.grid.col[8]);
$('.box:eq(3)').css('left',r.grid.col[12]);
break;
}
});
</script>

<ul>
Expand Down
138 changes: 75 additions & 63 deletions res.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*
/*
res.js v1.25.1
Author: Steve Belovarich
The MIT License (MIT)
Copyright (c) 2014 Steve Belovarich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Usage: var r = new res([{
"state": "portrait",
"breakpoint": 420,
Expand All @@ -45,16 +45,28 @@
"margin": 40,
"gutter": 10
}]);
window.addEventListener('stateChange',function(ev,i){
console.log(r.state); // get the state from the object you created
});
*/
});
*/

//Polyfill for CustomEvent in IE9+
(function () {
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
};
CustomEvent.prototype = window.Event.prototype;
})();

var res = function(json){

var that = this;
this.uagent = navigator.userAgent.toLowerCase();
this.state = undefined;
Expand All @@ -66,8 +78,8 @@ var res = function(json){
this.version = undefined;
this.width = 0;
this.grid = {};
this.viewports = {};
this.gridsettings = {};
this.viewports = {};
this.gridsettings = {};

var lastBreakpoint = 0;

Expand All @@ -78,14 +90,14 @@ var res = function(json){
}
lastBreakpoint = json[i].breakpoint;
};

this.init();
};
res.prototype = {

setState: function() {
var that = this;

if(that.device === 'desktop'){
that.width = window.innerWidth;
}
Expand All @@ -94,18 +106,18 @@ res.prototype = {
that.width = screen.width;
}
else if(that.orient === 'landscape'){
that.width = screen.height;
that.width = screen.height;
}
}

for (var key in that.viewports) {
if (that.viewports.hasOwnProperty(key)) {
if (that.width >= that.viewports[key][0] && that.width <= that.viewports[key][1]) {
if (that.state != key) {
that.state = key;
return that.state;
}
}
}
}
}
},
Expand All @@ -116,35 +128,35 @@ res.prototype = {
that.input = 'touch';
}
else{
that.input = 'mouse';
that.input = 'mouse';
}
},

browserCheck: function(){
var that = this;
var tem,
var tem,
M = that.uagent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem = /\brv[ :]+(\d+)/g.exec(that.uagent) || [];
return 'IE '+(tem[1] || '');
}
if(M[1] === 'Chrome'){
tem = that.uagent.match(/\bOPR\/(\d+)/);
if(tem != null) {
return 'Opera '+tem[1];
if(tem != null) {
return 'Opera '+tem[1];
}
}

M = M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];

if((tem = that.uagent.match(/version\/(\d+)/i)) != null) {
M.splice(1, 1, tem[1]);
}
}
that.browser = M[0];
that.version = M[1];
return M.join(' ');
},

osCheck: function() {
var that = this;
if (navigator.appVersion.indexOf("Win")!=-1) {
Expand All @@ -161,24 +173,24 @@ res.prototype = {
that.device = 'mobile';
}
else{
that.device = 'tablet';
that.device = 'tablet';
}

}
else if (navigator.userAgent.indexOf("windows phone") > 0) {
that.os = 'windows';
that.device = 'mobile';
}
}
else if (navigator.appVersion.indexOf("X11")!=-1) {
that.os = 'unix';
that.device = 'desktop';
}
}
else if (navigator.appVersion.indexOf("Linux")!=-1) {
that.os = 'linux';
that.device = 'desktop';
}
}
else if (navigator.userAgent.match(/(iPhone|iPod|iPad)/).length > 0) {
that.os = 'ios';
that.os = 'ios';
if( that.uagent.indexOf("iphone") > 0 ){
that.device = "iphone";
}
Expand All @@ -187,15 +199,15 @@ res.prototype = {
}
if( that.uagent.indexOf("ipad") > 0 ){
that.device = "ipad";
}
}
}
}
},

gridHelper: function(key){

var that = this;
var col,

var col,
colArr = [],
colSpan,
colSpanArr = [],
Expand All @@ -205,27 +217,27 @@ res.prototype = {

cols = that.gridsettings[key][0];
margin = that.gridsettings[key][1];
gutter = that.gridsettings[key][2];
gutter = that.gridsettings[key][2];

col = [];
colSpan = [];
width = window.innerWidth - (margin*2) + gutter;
columnWidth = (width/cols)-gutter;
columnWidth = (width/cols)-gutter;

for(var i = 0; i<cols; i++){
if(i===0){
colSpan = 0;
colSpan = 0;
}
else{
colSpan = (columnWidth*i)+(gutter*(i-1));
}
col = ((width/cols)*i)+margin;
colArr.push(col);
colSpanArr.push(colSpan);

if(i===cols-1){
colSpan = (columnWidth*(i+1))+(gutter*(i))
colSpanArr.push(colSpan);
colSpanArr.push(colSpan);
}
}
return {
Expand All @@ -238,49 +250,49 @@ res.prototype = {
};

},

resize: function() {
var that = this;

if (window.innerHeight > window.innerWidth) {
that.orient = 'portrait';
}
}
else {
that.orient = 'landscape';
}

that.setState();

if (that.gridsettings.hasOwnProperty(that.state)) {
that.grid = that.gridHelper(that.state);
}

that.stateChange = new CustomEvent("stateChange",{
bubbles: false,
cancelable: true
});

window.dispatchEvent(that.stateChange);

return that;
},

init: function() {
var that = this;

that.osCheck();
that.inputCheck();
that.browserCheck();

window.onorientationchange = function() {
that.resize();
};

window.onresize = function() {
that.resize();
};

that.resize();
}
};

};
2 changes: 1 addition & 1 deletion res.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 959db8f

Please sign in to comment.