Skip to content

Commit

Permalink
refactor(eg): separate eg.Agent from eg (naver#327)
Browse files Browse the repository at this point in the history
Close naver#323
  • Loading branch information
sculove authored Jul 29, 2016
1 parent 2c4891f commit 1fa3ce5
Show file tree
Hide file tree
Showing 22 changed files with 1,534 additions and 1,511 deletions.
8 changes: 4 additions & 4 deletions config/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ module.exports = {
banner: "<%=banner.common %>\"use strict\";\n",
process: changeSrc
},
src: dependency.egCore.concat(dependency.egExtend, ["src/visible.js", "src/movableCoord.js", "src/flicking.js", "src/infiniteGrid.js"]),
src: dependency.eg.concat(["src/agent.js"], dependency.egCore, dependency.egExtend, ["src/visible.js", "src/movableCoord.js", "src/flicking.js", "src/infiniteGrid.js"]),
dest: "dist/<%=pkg.outputname%>.js"
},
pkgd_eg: {
options : {
banner : "<%=banner.common %><%=banner.pkgd('egjs') %><%=concat.pkgd_eg.src.join('\\n* ')%>\n*/\n",
process: changeSrc
},
src : dependency.hammer.concat(dependency.egCore, dependency.egExtend, ["src/visible.js", "src/movableCoord.js", "src/flicking.js", "src/infiniteGrid.js"]),
src : dependency.hammer.concat(dependency.eg, ["src/agent.js"], dependency.egCore, dependency.egExtend, ["src/visible.js", "src/movableCoord.js", "src/flicking.js", "src/infiniteGrid.js"]),
dest : "dist/pkgd/<%=pkg.outputname%>.pkgd.js"
},
pkgd_flicking: {
options : {
banner : "<%=banner.common %><%=banner.pkgd('eg.flicking') %><%=concat.pkgd_flicking.src.join('\\n* ')%>\n*/\n",
process: changeSrc
},
src : dependency.hammer.concat(dependency.egCore, ["src/hook/cssPrefix.js", "src/movableCoord.js", "src/flicking.js"]),
src : dependency.hammer.concat(dependency.eg, ["src/agent.js"], dependency.egCore, ["src/hook/cssPrefix.js", "src/movableCoord.js", "src/flicking.js"]),
dest : "dist/pkgd/flicking.pkgd.js"
},
pkgd_infiniteGrid: {
options : {
banner : "<%=banner.common %><%=banner.pkgd('eg.infiniteGrid') %><%=concat.pkgd_infiniteGrid.src.join('\\n* ')%>\n*/",
process: changeSrc
},
src : dependency.egCore.concat(["src/plugin/persist.js", "src/infiniteGrid.js"]),
src : dependency.eg.concat(dependency.egCore, ["src/plugin/persist.js", "src/infiniteGrid.js"]),
dest : "dist/pkgd/infiniteGrid.pkgd.js"
}
};
5 changes: 3 additions & 2 deletions config/dependency.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
eg: [
"src/module.js"
],
egCore : [
"src/module.js",
"src/eg.js",
"src/class.js",
"src/component.js"
Expand All @@ -11,7 +13,6 @@ module.exports = {
"src/hook/transform.js",
"src/hook/cssPrefix.js",
"src/hook/pauseResume.js",
"src/plugin/apply.js",
"src/plugin/persist.js"
],
hammer : [
Expand Down
255 changes: 255 additions & 0 deletions src/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/**
* Copyright (c) 2015 NAVER Corp.
* egjs projects are licensed under the MIT license
*/

eg.module("agent", [eg], function(ns) {
"use strict";

/*
* {String|RegEx} criteria
* {String|RegEx} identity
* {String} versionSearch
* {String} versionAlias
* {String|RegEx} webviewBrowserVersion
* {String|RegEx} webviewToken
*/
var userAgentRules = {
browser: [{
criteria: "PhantomJS",
identity: "PhantomJS"
}, {
criteria: /Edge/,
identity: "Edge",
versionSearch: "Edge"
}, {
criteria: /MSIE|Trident|Windows Phone/,
identity: "IE",
versionSearch: "IEMobile|MSIE|rv"
}, {
criteria: /SAMSUNG|SamsungBrowser/,
identity: "SBrowser",
versionSearch: "Chrome"
}, {
criteria: /Chrome|CriOS/,
identity: "Chrome"
}, {
criteria: /Android/,
identity: "default"
}, {
criteria: /iPhone|iPad/,
identity: "Safari",
versionSearch: "Version"
}, {
criteria: "Apple",
identity: "Safari",
versionSearch: "Version"
}, {
criteria: "Firefox",
identity: "Firefox"
}],
os: [{
criteria: /Windows Phone|Windows NT/,
identity: "Window",
versionSearch: "Windows Phone|Windows NT"
}, {
criteria: "Windows 2000",
identity: "Window",
versionAlias: "5.0"
}, {
criteria: /iPhone|iPad/,
identity: "iOS",
versionSearch: "iPhone OS|CPU OS"
}, {
criteria: "Mac",
versionSearch: "OS X",
identity: "MAC"
}, {
criteria: /Android/,
identity: "Android"
}],

// Webview check condition
// ios: If has no version information
// Android 5.0 && chrome 40+: Presence of "; wv" in userAgent
// Under android 5.0: Presence of "NAVER" or "Daum" in userAgent
webview: [{
criteria: /iPhone|iPad/,
browserVersionSearch: "Version",
webviewBrowserVersion: /-1/
}, {
criteria: /iPhone|iPad|Android/,
webviewToken: /NAVER|DAUM|; wv/

}],
defaultString: {
browser: {
version: "-1",
name: "default"
},
os: {
version: "-1",
name: "unknown"
}
}
};

var UA;

function getBrowserName(browserRules) {
return getIdentityStringFromArray(
browserRules,
userAgentRules.defaultString.browser
);
}

function getBrowserVersion(browserName) {
var browserVersion;
var versionToken;

if (!browserName) {
return;
}
versionToken = getBrowserRule(browserName).versionSearch || browserName;
browserVersion = extractBrowserVersion(versionToken, UA);
return browserVersion;
}

function extractBrowserVersion(versionToken, ua) {
var browserVersion = userAgentRules.defaultString.browser.version;
var versionIndex;
var versionTokenIndex;
var versionRegexResult =
(new RegExp("(" + versionToken + ")", "i")).exec(ua);

if (!versionRegexResult) {
return browserVersion;
}

versionTokenIndex = versionRegexResult.index;
versionToken = versionRegexResult[0];
if (versionTokenIndex > -1) {
versionIndex = versionTokenIndex + versionToken.length + 1;
browserVersion = ua.substring(versionIndex)
.split(" ")[0]
.replace(/_/g, ".")
.replace(/\;|\)/g, "");
}
return browserVersion;
}

function getOSName(osRules) {
return getIdentityStringFromArray(
osRules,
userAgentRules.defaultString.os
);
}

function getOSVersion(osName) {
var ua = UA;
var osRule = getOSRule(osName) || {};
var defaultOSVersion = userAgentRules.defaultString.os.version;
var osVersion;
var osVersionToken;
var osVersionRegex;
var osVersionRegexResult;
if (!osName) {
return;
}
if (osRule.versionAlias) {
return osRule.versionAlias;
}
osVersionToken = osRule.versionSearch || osName;
osVersionRegex =
new RegExp(
"(" + osVersionToken + ")\\s([\\d_\\.]+|\\d_0)",
"i"
);

osVersionRegexResult = osVersionRegex.exec(ua);
if (osVersionRegexResult) {
osVersion = osVersionRegex.exec(ua)[2].replace(/_/g, ".")
.replace(/\;|\)/g, "");
}
return osVersion || defaultOSVersion;
}

function getOSRule(osName) {
return getRule(userAgentRules.os, osName);
}

function getBrowserRule(browserName) {
return getRule(userAgentRules.browser, browserName);
}

function getRule(rules, targetIdentity) {
var criteria;
var identityMatched;

for (var i = 0, rule; rule = rules[i]; i++) {
criteria = rule.criteria;
identityMatched =
new RegExp(rule.identity, "i").test(targetIdentity);
if (criteria ?
identityMatched && isMatched(UA, criteria) :
identityMatched) {
return rule;
}
}
}
function getIdentityStringFromArray(rules, defaultStrings) {
for (var i = 0, rule; rule = rules[i]; i++) {
if (isMatched(UA, rule.criteria)) {
return rule.identity || defaultStrings.name;
}
}
return defaultStrings.name;
}
function isMatched(base, target) {
return target &&
target.test ? !!target.test(base) : base.indexOf(target) > -1;
}
function isWebview() {
var ua = UA;
var webviewRules = userAgentRules.webview;
var ret = false;
var browserVersion;

for (var i = 0, rule; rule = webviewRules[i]; i++) {
if (!isMatched(ua, rule.criteria)) {
continue;
}

browserVersion =
extractBrowserVersion(rule.browserVersionSearch, ua);

if (isMatched(ua, rule.webviewToken) ||
isMatched(browserVersion, rule.webviewBrowserVersion)) {
ret = true;
break;
}
}

return ret;
}

ns.Agent = {
"create": function(useragent) {
this.ua = UA = useragent;
var agent = {
os: {},
browser: {}
};
agent.browser.name = getBrowserName(userAgentRules.browser);
agent.browser.version = getBrowserVersion(agent.browser.name);
agent.os.name = getOSName(userAgentRules.os);
agent.os.version = getOSVersion(agent.os.name);
agent.browser.webview = isWebview();

agent.browser.name = agent.browser.name.toLowerCase();
agent.os.name = agent.os.name.toLowerCase();

return agent;
}
};
});
Loading

0 comments on commit 1fa3ce5

Please sign in to comment.