Skip to content

Commit cf5db6d

Browse files
author
Olaf Kwant
committed
Es6
1 parent 26b54d5 commit cf5db6d

File tree

9 files changed

+509
-569
lines changed

9 files changed

+509
-569
lines changed

lib/client.js

Lines changed: 143 additions & 147 deletions
Large diffs are not rendered by default.

lib/index.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
var Client = require('./client')
2-
var Utils = require('./utils')
3-
var ZAFClient = {}
1+
import Client from './client'
2+
import { queryParameters } from './utils'
43

54
/**
65
* ### ZAFClient API
@@ -20,29 +19,30 @@ var ZAFClient = {}
2019
* Example:
2120
*
2221
* ```javascript
23-
* var client = ZAFClient.init(function(context) {
24-
* var currentUser = context.currentUser;
22+
* const client = ZAFClient.init((context) => {
23+
* const currentUser = context.currentUser;
2524
* console.log('Hi ' + currentUser.name);
2625
* });
2726
* ```
2827
*/
29-
ZAFClient.init = function (callback, loc) {
30-
loc = loc || window.location
31-
var queryParams = Utils.queryParameters(loc.search)
32-
var hashParams = Utils.queryParameters(loc.hash)
33-
var origin = queryParams.origin || hashParams.origin
34-
var appGuid = queryParams.app_guid || hashParams.app_guid
35-
var client
3628

37-
if (!origin || !appGuid) { return false }
29+
const ZAFClient = {
30+
init: function (callback, loc) {
31+
loc = loc || window.location
32+
const queryParams = queryParameters(loc.search)
33+
const hashParams = queryParameters(loc.hash)
34+
const origin = queryParams.origin || hashParams.origin
35+
const appGuid = queryParams.app_guid || hashParams.app_guid
36+
if (!origin || !appGuid) { return false }
3837

39-
client = new Client({ origin: origin, appGuid: appGuid })
38+
const client = new Client({ origin, appGuid })
4039

41-
if (typeof callback === 'function') {
42-
client.on('app.registered', callback.bind(client))
43-
}
40+
if (typeof callback === 'function') {
41+
client.on('app.registered', callback.bind(client))
42+
}
4443

45-
return client
44+
return client
45+
}
4646
}
4747

48-
module.exports = ZAFClient
48+
export default ZAFClient

lib/tracker.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
1-
function Tracker (client) {
2-
this.startTime = Date.now()
3-
this.client = client
4-
this.MIN_HOVER_TIME = 200 // milliseconds
5-
6-
return this
7-
}
1+
export default class Tracker {
2+
constructor (client) {
3+
this.startTime = Date.now()
4+
this.client = client
5+
this.MIN_HOVER_TIME = 200 // milliseconds
6+
}
87

9-
Tracker.prototype = {
10-
handleMouseEnter: function () {
8+
handleMouseEnter () {
119
this.startTime = Date.now()
12-
},
10+
}
1311

14-
handleMouseLeave: function () {
15-
var overFor = Date.now() - this.startTime
12+
handleMouseLeave () {
13+
const overFor = Date.now() - this.startTime
1614
if (overFor >= this.MIN_HOVER_TIME) {
1715
this.client.invoke('track', { type: 'hover', value: overFor })
1816
}
19-
},
17+
}
2018

21-
handleClick: function () {
19+
handleClick () {
2220
this.client.invoke('track', { type: 'click' })
23-
},
21+
}
2422

25-
setup: function () {
26-
var $html = document.querySelector('html')
23+
setup () {
24+
const $html = document.querySelector('html')
2725
$html.addEventListener('click', this.handleClick.bind(this))
2826
$html.addEventListener('mouseleave', this.handleMouseLeave.bind(this))
2927
$html.addEventListener('mouseenter', this.handleMouseEnter.bind(this))
3028
}
3129
}
32-
33-
module.exports = { Tracker: Tracker }

lib/utils.js

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
var Promise = window.Promise || require('native-promise-only')
2-
3-
function isObject (obj) {
1+
export function isObject (obj) {
42
return (obj !== null && typeof obj === 'object')
53
}
64

75
function decode (s) {
86
return decodeURIComponent((s || '').replace(/\+/g, ' '))
97
}
108

11-
function queryParameters (queryString) {
9+
export function queryParameters (queryString) {
1210
queryString = queryString || ''
1311

14-
var result = {}
15-
var keyValuePairs
16-
var keyAndValue
17-
var key
18-
var value
12+
const result = {}
13+
let keyValuePairs, keyAndValue, key, value
1914

2015
if (queryString.search(/\?|#/) === 0) {
2116
queryString = queryString.slice(1)
@@ -25,7 +20,7 @@ function queryParameters (queryString) {
2520

2621
keyValuePairs = queryString.split('&')
2722

28-
for (var i = 0; i < keyValuePairs.length; i++) {
23+
for (let i = 0; i < keyValuePairs.length; i++) {
2924
keyAndValue = keyValuePairs[i].split('=')
3025
key = decode(keyAndValue[0])
3126
value = decode(keyAndValue[1]) || ''
@@ -36,8 +31,8 @@ function queryParameters (queryString) {
3631
}
3732

3833
function isPromise (obj) {
39-
return obj instanceof Promise ||
40-
(!!obj && obj.then && typeof obj.then === 'function')
34+
if (obj instanceof Promise) return true
35+
return !!obj && obj.then && typeof obj.then === 'function'
4136
}
4237

4338
function shouldReject (val) {
@@ -46,7 +41,7 @@ function shouldReject (val) {
4641
typeof val === 'string'
4742
}
4843

49-
function isString (key) {
44+
export function isString (key) {
5045
return typeof key === 'string'
5146
}
5247

@@ -57,20 +52,19 @@ function isString (key) {
5752
// c) first to settle with a falsy value
5853
// c) is sub-optimal, since it has a specific narrow use-case
5954
// for save hooks
60-
function when (items) {
55+
export function when (items) {
6156
items = items || []
62-
var resolveAll
63-
var rejectAll
64-
var allResolvedPromise = new Promise(function (resolve, reject) {
57+
let resolveAll, rejectAll
58+
const allResolvedPromise = new Promise((resolve, reject) => {
6559
resolveAll = resolve
6660
rejectAll = reject
6761
})
68-
var remaining = 0
69-
var settledWith = []
70-
var itemsCopy = Array.isArray(items)
71-
? items.slice()
72-
: [items]
73-
var resolveWith = function (data, index) {
62+
63+
let remaining = 0
64+
const settledWith = []
65+
const itemsCopy = Array.isArray(items) ? items.slice() : [items]
66+
67+
function resolveWith (data, index) {
7468
settledWith[index] = data
7569

7670
if (--remaining <= 0) {
@@ -85,13 +79,12 @@ function when (items) {
8579
return allResolvedPromise
8680
}
8781

88-
itemsCopy.forEach(function (item, index) {
89-
var promise
90-
82+
itemsCopy.forEach((item, index) => {
83+
let promise
9184
if (isPromise(item)) {
9285
promise = item
9386
} else if (typeof item === 'function') {
94-
var res
87+
let res
9588
try {
9689
res = item()
9790
if (isPromise(res)) {
@@ -105,25 +98,16 @@ function when (items) {
10598
promise = Promise.reject(err)
10699
}
107100
} else {
108-
promise = new Promise(function (resolve, reject) {
101+
promise = new Promise((resolve, reject) => {
109102
shouldReject(item)
110103
? reject(item)
111104
: resolve(item)
112105
})
113106
}
114-
promise.then(
115-
function (data) {
116-
resolveWith(data, index)
117-
}
118-
).catch(rejectAll.bind(rejectAll))
107+
promise.then((data) => {
108+
resolveWith(data, index)
109+
}).catch(rejectAll.bind(rejectAll))
119110
})
120111

121112
return allResolvedPromise
122113
}
123-
124-
module.exports = {
125-
queryParameters: queryParameters,
126-
when: when,
127-
isString: isString,
128-
isObject: isObject
129-
}

0 commit comments

Comments
 (0)