Commented stuff.

This commit is contained in:
Mario Basic
2015-06-04 00:48:31 +02:00
parent af4c748763
commit 6ae7bec081
7 changed files with 167 additions and 56 deletions

View File

@@ -15,6 +15,11 @@ class WakaTime {
currentUserApiUrl = 'https://wakatime.com/api/v1/users/current'; currentUserApiUrl = 'https://wakatime.com/api/v1/users/current';
/**
* Checks if the user is logged in.
*
* @return $.promise()
*/
checkAuth() checkAuth()
{ {
var deferredObject = $.Deferred(); var deferredObject = $.Deferred();
@@ -38,29 +43,28 @@ class WakaTime {
return deferredObject.promise(); return deferredObject.promise();
} }
/**
* Depending on various factors detects the current active tab URL or domain,
* and sends it to WakaTime for logging.
*
* @return null
*/
recordHeartbeat() recordHeartbeat()
{ {
this.checkAuth().done(data => { this.checkAuth().done(data => {
if(data !== false){ if(data !== false){
console.log('user is logged id.');
// User is logged in. // User is logged in.
// Change extension icon to default color.
changeExtensionIcon(); changeExtensionIcon();
console.log('recording heartbeat.');
chrome.idle.queryState(this.detectionIntervalInSeconds, (newState) => { chrome.idle.queryState(this.detectionIntervalInSeconds, (newState) => {
console.log(newState);
if(newState === 'active') if(newState === 'active')
{ {
// Get current tab URL. // Get current tab URL.
chrome.tabs.query({active: true}, (tabs) => { chrome.tabs.query({active: true}, (tabs) => {
console.log(tabs[0].url);
this.sendHeartbeat(tabs[0].url); this.sendHeartbeat(tabs[0].url);
}); });
} }
@@ -70,16 +74,20 @@ class WakaTime {
else { else {
// User is not logged in. // User is not logged in.
// Change extension icon to red color.
changeExtensionIcon('red'); changeExtensionIcon('red');
console.log('user is not logged id.');
//TODO: Redirect user to wakatime login page.
//
} }
}); });
} }
/**
* Creates payload for the heartbeat and returns it as JSON.
*
* @param string entity
* @param string type 'domain' or 'url'
* @param boolean debug = false
* @return JSON
*/
_preparePayload(entity, type, debug = false) _preparePayload(entity, type, debug = false)
{ {
return JSON.stringify({ return JSON.stringify({
@@ -90,6 +98,11 @@ class WakaTime {
}); });
} }
/**
* Returns a promise with logging type variable.
*
* @return $.promise
*/
_getLoggingType() _getLoggingType()
{ {
var deferredObject = $.Deferred(); var deferredObject = $.Deferred();
@@ -103,16 +116,20 @@ class WakaTime {
return deferredObject.promise(); return deferredObject.promise();
} }
/**
* Given the entity and logging type it creates a payload and
* sends an ajax post request to the API.
*
* @param string entity
* @return null
*/
sendHeartbeat(entity) sendHeartbeat(entity)
{ {
this._getLoggingType().done((loggingType) => { this._getLoggingType().done((loggingType) => {
// Get only the domain from the entity.
// And send that in heartbeat
if(loggingType == 'domain') { if(loggingType == 'domain') {
console.log('sending entity with type domain');
// Get only the domain from the entity.
// And send that in heartbeat
console.log(UrlHelper.getDomainFromUrl(entity));
var domain = UrlHelper.getDomainFromUrl(entity); var domain = UrlHelper.getDomainFromUrl(entity);
@@ -120,24 +137,28 @@ class WakaTime {
console.log(payload); console.log(payload);
this.sendAjaxRequestToApi(payload); //this.sendAjaxRequestToApi(payload);
} }
// Send entity in heartbeat
else if (loggingType == 'url') { else if (loggingType == 'url') {
console.log('sending entity with type url');
// Send entity in heartbeat
var payload = this._preparePayload(entity, 'url'); var payload = this._preparePayload(entity, 'url');
console.log(payload); console.log(payload);
this.sendAjaxRequestToApi(payload); //this.sendAjaxRequestToApi(payload);
} }
}); });
} }
/**
* Sends AJAX request with payload to the heartbeat API as JSON.
*
* @param JSON payload
* @param string method = 'POST'
* @return $.promise
*/
sendAjaxRequestToApi(payload, method = 'POST') { sendAjaxRequestToApi(payload, method = 'POST') {
var deferredObject = $.Deferred(); var deferredObject = $.Deferred();

View File

@@ -1,3 +1,8 @@
/**
* Returns UNIX timestamp
*
* @return integer
*/
export default function(){ export default function(){
return Math.round((new Date()).getTime() / 1000); return Math.round((new Date()).getTime() / 1000);
} }

View File

@@ -19,7 +19,7 @@ elixir(function (mix) {
mix.copy('vendor/bower_components/font-awesome/less', 'assets/less/font-awesome'); mix.copy('vendor/bower_components/font-awesome/less', 'assets/less/font-awesome');
mix.copy('vendor/bower_components/font-awesome/fonts', 'public/fonts'); mix.copy('vendor/bower_components/font-awesome/fonts', 'public/fonts');
mix.less('app.less'); mix.less('app.less');
mix.browserify('app.js', null, 'assets/js'); //mix.browserify('app.js', null, 'assets/js');
//mix.browserify('events.js', 'public/js/events.js', 'assets/js'); mix.browserify('events.js', 'public/js/events.js', 'assets/js');
//mix.browserify('options.js', 'public/js/options.js', 'assets/js'); //mix.browserify('options.js', 'public/js/options.js', 'assets/js');
}); });

View File

@@ -1500,6 +1500,58 @@ address {
font-style: normal; font-style: normal;
line-height: 1.846; line-height: 1.846;
} }
code,
kbd,
pre,
samp {
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
}
code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
background-color: #f9f2f4;
border-radius: 3px;
}
kbd {
padding: 2px 4px;
font-size: 90%;
color: #ffffff;
background-color: #333333;
border-radius: 3px;
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.25);
}
kbd kbd {
padding: 0;
font-size: 100%;
font-weight: bold;
box-shadow: none;
}
pre {
display: block;
padding: 11px;
margin: 0 0 11.5px;
font-size: 12px;
line-height: 1.846;
word-break: break-all;
word-wrap: break-word;
color: #212121;
background-color: #f5f5f5;
border: 1px solid #cccccc;
border-radius: 3px;
}
pre code {
padding: 0;
font-size: inherit;
color: inherit;
white-space: pre-wrap;
background-color: transparent;
border-radius: 0;
}
.pre-scrollable {
max-height: 340px;
overflow-y: scroll;
}
.container { .container {
margin-right: auto; margin-right: auto;
margin-left: auto; margin-left: auto;

File diff suppressed because one or more lines are too long

View File

@@ -181,7 +181,7 @@ var WakaTime = (function () {
console.log(payload); console.log(payload);
_this3.sendAjaxRequestToApi(payload); //this.sendAjaxRequestToApi(payload);
} else if (loggingType == 'url') { } else if (loggingType == 'url') {
console.log('sending entity with type url'); console.log('sending entity with type url');
@@ -191,7 +191,7 @@ var WakaTime = (function () {
console.log(payload); console.log(payload);
_this3.sendAjaxRequestToApi(payload); //this.sendAjaxRequestToApi(payload);
} }
}); });
} }

View File

@@ -100,6 +100,12 @@ var WakaTime = (function () {
_createClass(WakaTime, [{ _createClass(WakaTime, [{
key: 'checkAuth', key: 'checkAuth',
/**
* Checks if the user is logged in.
*
* @return $.promise()
*/
value: function checkAuth() { value: function checkAuth() {
var _this = this; var _this = this;
@@ -124,6 +130,13 @@ var WakaTime = (function () {
} }
}, { }, {
key: 'recordHeartbeat', key: 'recordHeartbeat',
/**
* Depending on various factors detects the current active tab URL or domain,
* and sends it to WakaTime for logging.
*
* @return null
*/
value: function recordHeartbeat() { value: function recordHeartbeat() {
var _this2 = this; var _this2 = this;
@@ -131,22 +144,15 @@ var WakaTime = (function () {
if (data !== false) { if (data !== false) {
console.log('user is logged id.');
// User is logged in. // User is logged in.
// Change extension icon to default color.
changeExtensionIcon(); changeExtensionIcon();
console.log('recording heartbeat.');
chrome.idle.queryState(_this2.detectionIntervalInSeconds, function (newState) { chrome.idle.queryState(_this2.detectionIntervalInSeconds, function (newState) {
console.log(newState);
if (newState === 'active') { if (newState === 'active') {
// Get current tab URL. // Get current tab URL.
chrome.tabs.query({ active: true }, function (tabs) { chrome.tabs.query({ active: true }, function (tabs) {
console.log(tabs[0].url);
_this2.sendHeartbeat(tabs[0].url); _this2.sendHeartbeat(tabs[0].url);
}); });
} }
@@ -154,17 +160,22 @@ var WakaTime = (function () {
} else { } else {
// User is not logged in. // User is not logged in.
// Change extension icon to red color.
changeExtensionIcon('red'); changeExtensionIcon('red');
console.log('user is not logged id.');
//TODO: Redirect user to wakatime login page.
//
} }
}); });
} }
}, { }, {
key: '_preparePayload', key: '_preparePayload',
/**
* Creates payload for the heartbeat and returns it as JSON.
*
* @param string entity
* @param string type 'domain' or 'url'
* @param boolean debug = false
* @return JSON
*/
value: function _preparePayload(entity, type) { value: function _preparePayload(entity, type) {
var debug = arguments[2] === undefined ? false : arguments[2]; var debug = arguments[2] === undefined ? false : arguments[2];
@@ -177,6 +188,12 @@ var WakaTime = (function () {
} }
}, { }, {
key: '_getLoggingType', key: '_getLoggingType',
/**
* Returns a promise with logging type variable.
*
* @return $.promise
*/
value: function _getLoggingType() { value: function _getLoggingType() {
var deferredObject = $.Deferred(); var deferredObject = $.Deferred();
@@ -190,17 +207,22 @@ var WakaTime = (function () {
} }
}, { }, {
key: 'sendHeartbeat', key: 'sendHeartbeat',
/**
* Given the entity and logging type it creates a payload and
* sends an ajax post request to the API.
*
* @param string entity
* @return null
*/
value: function sendHeartbeat(entity) { value: function sendHeartbeat(entity) {
var _this3 = this; var _this3 = this;
this._getLoggingType().done(function (loggingType) { this._getLoggingType().done(function (loggingType) {
// Get only the domain from the entity.
// And send that in heartbeat
if (loggingType == 'domain') { if (loggingType == 'domain') {
console.log('sending entity with type domain');
// Get only the domain from the entity.
// And send that in heartbeat
console.log(UrlHelper.getDomainFromUrl(entity));
var domain = UrlHelper.getDomainFromUrl(entity); var domain = UrlHelper.getDomainFromUrl(entity);
@@ -208,22 +230,28 @@ var WakaTime = (function () {
console.log(payload); console.log(payload);
_this3.sendAjaxRequestToApi(payload); //this.sendAjaxRequestToApi(payload);
} else if (loggingType == 'url') { }
console.log('sending entity with type url'); // Send entity in heartbeat
else if (loggingType == 'url') {
// Send entity in heartbeat
var payload = _this3._preparePayload(entity, 'url'); var payload = _this3._preparePayload(entity, 'url');
console.log(payload); console.log(payload);
_this3.sendAjaxRequestToApi(payload); //this.sendAjaxRequestToApi(payload);
} }
}); });
} }
}, { }, {
key: 'sendAjaxRequestToApi', key: 'sendAjaxRequestToApi',
/**
* Sends AJAX request with payload to the heartbeat API as JSON.
*
* @param JSON payload
* @param string method = 'POST'
* @return $.promise
*/
value: function sendAjaxRequestToApi(payload) { value: function sendAjaxRequestToApi(payload) {
var _this4 = this; var _this4 = this;
@@ -294,14 +322,19 @@ function changeExtensionIcon() {
module.exports = exports['default']; module.exports = exports['default'];
},{}],5:[function(require,module,exports){ },{}],5:[function(require,module,exports){
/**
* Returns UNIX timestamp
*
* @return integer
*/
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
exports["default"] = function () { exports["default"] = function () {
return Math.round(new Date().getTime() / 1000); return Math.round(new Date().getTime() / 1000);
}; };
module.exports = exports["default"]; module.exports = exports["default"];