Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Add old IE polyfills and add build tasks #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
/dist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No leading slash.

54 changes: 53 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,61 @@ module.exports = function( grunt ) {

"use strict";

var banner = "/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - " +
"<%= grunt.template.today('isoDate') %>\n" +
"<%= pkg.homepage ? '* ' + pkg.homepage + '\\n' : '' %>" +
"* Copyright <%= grunt.template.today('yyyy') %> <%= pkg.author.name %>;" +
" Licensed <%= _.pluck(pkg.licenses, 'type').join(', ') %> */\n";

grunt.loadNpmTasks( "grunt-contrib-jshint" );
grunt.loadNpmTasks( "grunt-git-authors" );
grunt.loadNpmTasks( "grunt-contrib-uglify" );
grunt.loadNpmTasks( "grunt-contrib-concat" );
grunt.loadNpmTasks( "grunt-contrib-copy" );

grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing

concat: {
oldIE: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't call this oldIE. There are likely other browsers that we support which need at least one polyfill.

options: {
banner: banner,
stripBanners: {
block: true
}
},
src: [ "src/old-ie.js", "src/pointer.js" ],
dest: "dist/jquery.pointer.js"
}
},
copy: {
modern: {
files: [{
src: "src/pointer.js",
dest: "dist/jquery.pointer.modern.js"
}]
}
},
uglify: {
options: {
preserveComments: false
},
oldIE: {
options: {
banner: banner
},
files: {
"dist/jquery.pointer.min.js": "dist/jquery.pointer.js"
}
},
modern: {
options: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the options for both targets are the same, why not just merge them?

banner: banner
},
files: {
"dist/jquery.pointer.modern.min.js": "dist/jquery.pointer.modern.js"
}
}
},
jshint: {
src: {
options: {
Expand All @@ -26,7 +77,8 @@ grunt.initConfig({
}
});

grunt.registerTask( "default", [ "lint" ] );
grunt.registerTask( "default", [ "lint", "concat:oldIE", "uglify:oldIE" ] );
grunt.registerTask( "modern", [ "lint", "copy:modern", "uglify:modern" ] );
grunt.registerTask( "lint", [ "jshint" ] );

};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"devDependencies": {
"grunt": "0.4.1",
"grunt-git-authors": "1.2.0",
"grunt-contrib-jshint": "0.4.3"
"grunt-contrib-jshint": "0.4.3",
"grunt-contrib-concat": "0.3.0",
"grunt-contrib-uglify": "0.2.7",
"grunt-contrib-copy": "0.4.1"
}
}
132 changes: 132 additions & 0 deletions src/old-ie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
if (!Array.prototype.filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, let's rename this file.

Array.prototype.filter = function filter(callback, scope) {
for (var array = this, arrayB = [], index = 0, length = array.length, element; index < length; ++index) {
element = array[index];

if (callback.call(scope || window, element, index, array)) {
arrayB.push(element);
}
}

return arrayB;
};
}

if (!Array.prototype.forEach) {
Array.prototype.forEach = function forEach(callback, scope) {
for (var array = this, index = 0, length = array.length; index < length; ++index) {
callback.call(scope || window, array[index], index, array);
}
};
}

if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function indexOf(searchElement) {
for (var array = this, index = 0, length = array.length; index < length; ++index) {
if (array[index] === searchElement) {
return index;
}
}

return -1;
};
}

if (!Array.prototype.map) {
Array.prototype.map = function map(callback, scope) {
for (var array = this, arrayB = [], index = 0, length = array.length, element; index < length; ++index) {
element = array[index];

arrayB.push(callback.call(scope || window, array[index], index, array));
}

return arrayB;
};
}

if (!Function.prototype.bind) {
Function.prototype.bind = function bind(scope) {
var callback = this, prepend = Array.prototype.slice.call(arguments, 1), Constructor = function () {}, bound = function () {
return callback.apply(this instanceof Constructor && scope ? this : scope, Array.prototype.concat.apply(prepend, arguments));
};

Constructor.prototype = bound.prototype = callback.prototype;

return bound;
};
}

if (!Object.keys) {
Object.keys = function keys(object) {
var buffer = [], key;

for (key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
buffer.push(key);
}
}

return buffer;
};
}

if (!Object.defineProperty) {
Object.defineProperty = function (object, property, descriptor) {
var propertyValue = object[property];

function onPropertyChange(event) {
if (event.propertyName === property) {
// temporarily remove the event so it doesn't fire again and create a loop
object.detachEvent("onpropertychange", onPropertyChange);

// set the value using the setter
if (descriptor.set) {
propertyValue = descriptor.set.call(object, object[property]);
}

// restore the getter
object[property] = String(propertyValue);

object[property].toString = function () {
return descriptor.get.call(object);
};

// restore the event
object.attachEvent("onpropertychange", onPropertyChange);
}
}

// assign the getter
object[property] = String(propertyValue);

object[property].toString = function () {
return descriptor.get.call(object);
};

// assign the event
object.attachEvent("onpropertychange", onPropertyChange);

// return the object
return object;
};
}

if (!Object.defineProperties) {
Object.defineProperties = function defineProperties(object, descriptors) {
for (var property in descriptors) {
Object.defineProperty(object, property, descriptors[property]);
}

return object;
};
}

if (!Date.now) {
Date.now = function() {
return new Date().getTime();
};
}

if ( !document.head ) {
document.head = document.getElementsByTagName("head")[0];
}
4 changes: 2 additions & 2 deletions src/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
(function( $ ) {

if ( !Object.keys ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a legitimate test.

function PointerEvent( type, event ) {
window.PointerEvent = function( type, event ) {
event.type = type;
var nevent = $.Event( event );
$.extend( nevent, event );
return nevent;
}
};
}

window.PointerEventsPolyfill = window.PointerEventsPolyfill || {};
Expand Down