-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.js
More file actions
164 lines (137 loc) · 5.5 KB
/
main.js
File metadata and controls
164 lines (137 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* ==========================================================
WakaTime
Description: Analytics for programmers.
Maintainer: WakaTime <support@wakatime.com>
License: BSD, see LICENSE for more details.
Website: https://wakatime.com/
===========================================================*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */
/** Simple extension that adds a "File > Hello World" menu item */
define(function (require, exports, module) {
"use strict";
var VERSION = '1.0.8';
var MainViewManager = brackets.getModule("view/MainViewManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
Dialogs = brackets.getModule("widgets/Dialogs"),
prefs = PreferencesManager.getExtensionPrefs("WakaTime");
var lastAction = 0,
lastFile = undefined;
function init() {
setupPreferences();
addMenuItem();
// prompt for api key if not already set
if (!prefs.get("apikey")) {
promptForApiKey();
}
setupEventListeners();
}
function addMenuItem() {
// register menu command
var COMMAND_ID = "wakatime.apikey";
CommandManager.register("WakaTime API Key", COMMAND_ID, promptForApiKey);
// add menu item
var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
menu.addMenuDivider();
menu.addMenuItem(COMMAND_ID);
}
function setupEventListeners() {
MainViewManager.on('currentFileChange', function () {
handleAction();
});
DocumentManager.on('documentSaved', function () {
handleAction(true);
});
$(window).on('keypress', function () {
handleAction();
});
}
function setupPreferences() {
prefs.definePreference("apikey", "string", "");
prefs.definePreference("ignore", "array", ["^/tmp/", "^/private/"]);
if (prefs.getPreferenceLocation('ignore').scope == 'default') {
prefs.set('ignore', prefs.get('ignore'));
prefs.save();
}
}
function sendHeartbeat(file, time, project, language, isWrite, lines) {
$.ajax({
type: 'POST',
url: 'https://wakatime.com/api/v1/heartbeats',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
time: time/1000,
entity: file,
type: 'file',
project: project,
language: language,
is_write: isWrite ? true : false,
lines: lines,
plugin: 'brackets-wakatime/'+VERSION,
}),
headers: {
'Authorization': 'Basic '+btoa(prefs.get("apikey")),
},
});
lastAction = time;
lastFile = file;
}
function enoughTimePassed() {
return lastAction + 120000 < Date.now();
}
function fileIsIgnored(file) {
var patterns = prefs.get("ignore");
var ignore = false;
for (var i=0; i<patterns.length; i++) {
var re = new RegExp(patterns[i], "gi");
if (re.test(file)) {
ignore = true;
break;
}
}
return ignore;
}
function handleAction(isWrite) {
var currentDocument = DocumentManager.getCurrentDocument();
if (currentDocument) {
var file = currentDocument.file;
if (file && file.isFile) {
var time = Date.now();
if (isWrite || enoughTimePassed() || lastFile !== file.fullPath) {
if (!fileIsIgnored(file.fullPath)) {
var language = currentDocument.language ? currentDocument.language.getName() : undefined;
var project = ProjectManager.getProjectRoot() ? ProjectManager.getProjectRoot().name : undefined;
var editor = currentDocument._masterEditor;
var lines = editor ? editor.lineCount() : undefined;
sendHeartbeat(file.fullPath, time, project, language, isWrite, lines);
}
}
}
}
}
// Function to run when the menu item is clicked
function promptForApiKey() {
var dialog, wakaKey, $input, $okButton;
dialog = Dialogs.showModalDialog('DefaultDialogs', '[WakaTime] Enter your wakatime.com api key:',
'<input type="text" id="wakatime-key" name="wakatime" value="' + prefs.get("apikey") + '" style="width: 97.5%;"/>',
[{ className: Dialogs.DIALOG_BTN_CLASS_PRIMARY + ' wakatime-ok', id: Dialogs.DIALOG_BTN_OK, text: 'Ok' },
{ id: Dialogs.DIALOG_BTN_CANCEL, text: 'Cancel'}]
);
$input = $('#wakatime-key');
//get OK button on actual modal window
$okButton = $('.wakatime-ok');
//save key when OK pressed
$okButton.on('click', function(event) {
if ($input.val()) {
prefs.set("apikey", $input.val());
prefs.save();
}
});
}
init();
});