Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hgoebl committed Sep 11, 2013
0 parents commit aeea68e
Show file tree
Hide file tree
Showing 31 changed files with 10,233 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
node_modules
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
node_modules
105 changes: 105 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*global module:false*/
module.exports = function (grunt) {

// Project configuration.
grunt.initConfig({
// Metadata.
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.repository.url%> */',

// Task configuration.
exec: {
generate: {
cmd: 'node generate/generate.js'
}
},
jasmine_node: {
specNameMatcher: "spec", // load only specs containing specNameMatcher
projectRoot: ".",
requirejs: false,
forceExit: true,
jUnit: {
report: false,
savePath: "./build/reports/jasmine/",
useDotNotation: true,
consolidate: true
}
},
uglify: {
options: {
banner: '<%= banner %>'
},
dist: {
src: 'mobile-detect.js',
dest: 'mobile-detect.min.js'
}
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
unused: true,
boss: true,
eqnull: true,
browser: true,
globals: {
jQuery: true,
Backbone: true,
Modernizr: true,
Mustache: true,
App: true,
console: true,
_: true
}
},
gruntfile: {
src: 'Gruntfile.js'
},
lib_test: {
src: ['generate/mobile-detect.template.js', 'tests/spec/*.js']
}
},
jsdoc: {
dist: {
src: ['<%= uglify.dist.src %>', 'README.md'],
options: {
destination: 'doc',
//template: "default",
encoding: "utf8",
"private": false,
lenient: true
}
}
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
lib_test: {
files: '<%= jshint.lib_test.src %>',
tasks: ['jshint:lib_test', 'jasmine_node']
}
}
});

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-jsdoc');
grunt.loadNpmTasks('grunt-jasmine-node');

// Default task.
grunt.registerTask('default', ['jshint', 'exec:generate', 'jasmine_node', 'uglify', 'jsdoc']);
grunt.registerTask('dev', ['jshint']);
};
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Heinrich Goebl, Agenda Software GmbH & Co. KG

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.
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# mobile-detect.js

A loose port of [Mobile-Detect](https://github.com/serbanghita/Mobile-Detect) to JavaScript.

This script will detect the device by comparing patterns against a given User-Agent string.
You can find out information about the device rendering your web page:

* mobile or not
* if mobile, whether phone or tablet
* operating system
* Mobile Grade (A, B, C)
* specific versions (e.g. WebKit)


# Usage

## Browser

<script src="mobile-detect.js"></script>
<script>
var md = new MobileDetect(window.navigator.userAgent);
// ... see below
</script>

## Node.js / Express

var MobileDetect = require('mobile-detect'),
md = new MobileDetect(req.headers['user-agent']);
// ... see below

## General

```js
// more typically we would instantiate with `window.navigator.userAgent` as user-agent
var md = new MobileDetect('Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30');
console.log(md.mobile()); // 'Sony'
console.log(md.phone()); // 'Sony'
console.log(md.tablet()); // null
console.log(md.userAgent()); // 'Safari'
console.log(md.os()); // 'AndroidOS'
console.log(md.is('iPhone')); // false
console.log(md.is('bot')); // false
console.log(md.version('Webkit')); // 534.3
```

## More Info ...

Open generated JSDoc in `doc/index.html`

## Side Effects

Script creates the global property `MobileDetect`.

## Modernizr Extension

When using [Modernizr](http://modernizr.com/), you can include `mobile-detect-modernizr.js`.
It will add the CSS classes `mobile`, `phone`, `tablet` and `mobilegradea` if applicable.

You can easily extend it, e.g. `android`, `iphone`, etc.

## Size (bytes)

* development: 30025
* minified: 18045
* minified + gzipped: 7149


# Installation

## Bower

**TODO** is not working yet, since project is not tagged w/ a version!

$ bower install hgoebl/mobile-detect.js --save

## Node.js / npm

**TODO** is not working yet, since project is not tagged w/ a version and not published to npmjs.org!

$ npm install mobile-detect --save

# Alternatives

Often device detection is the first solution in your mind. Please consider looking for other solutions
like media queries and feature detection (e.g. w/ Modernizr). Maybe there are better (simpler, smaller,
faster) device detection libraries, so here you have a list:

* [Modernizr](http://modernizr.com/)
In most cases the better solution: don't use knowledge about device and version, but detect features
(touch, canvas, ...)
* [Mobile-Detect](https://github.com/serbanghita/Mobile-Detect)
A lightweight PHP class for detecting mobile devices (including tablets).
This is the "source" of this JavaScript project and if you use PHP on your server you should use it!
* [dmolsen/Detector](https://github.com/dmolsen/Detector)
Detector is a simple, PHP- and JavaScript-based browser- and
feature-detection library that can adapt to new devices & browsers
on its own without the need to pull from a central database of browser information.
* [matthewhudson/device.js](https://github.com/matthewhudson/device.js)
Conditional CSS and/or JavaScript based on device operating system, orientation and type
* [brendanlim/mobile-fu](https://github.com/brendanlim/mobile-fu)
Automatically detect mobile requests from mobile devices in your Rails application.
* [FormFactor](https://github.com/PaulKinlan/formfactor)
FormFactor helps you customize your web app for different form factors, e.g. when you make
"the mobile version", "the TV version", etc.
* [UAParser.js](http://faisalman.github.com/ua-parser-js/)
Lightweight JavaScript-based User-Agent String Parser


# License

MIT-License (see LICENSE file).


# Contributing

Your contribution is welcome.
If you want new devices to be supported, please contribute to [Mobile-Detect] instead.

To run generate-script it is necessary to have [Mobile-Detect] as a sibling directory to mobile-detect.js/.
(I tried to use `git subtree` but had some problems on Mac OS X - probably my fault...)

* fork or clone serbanghita/Mobile-Detect
* fork hgoebl/mobile-detect.js
* run `npm install`
* create branch
* make changes and run `grunt` (needs PHP >= 5.4 in your path)
* run browser test (tests/SpecRunner.html)
* commit, push to your branch
* create pull request

## Testing

### Browser

Open `tests/SpecRunner.html` in different browsers.

### Node.js

$ npm test
$ # or
$ grunt jasmine_node


# Donations

If you want, you can donate to [Mobile-Detect].


# TODO

* Extend RegEx patterns so that test passes
* update mobilegrade() function to PHP-version
* Provide gh_pages w/ JSDoc and a live example
11 changes: 11 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "mobile-detect.js",
"version": "0.0.1",
"main": "mobile-detect.js",
"ignore": [
"**/.*",
"node_modules",
"tests",
"generate"
]
}
Loading

0 comments on commit aeea68e

Please sign in to comment.