This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
relay-for-toggl.user.js
146 lines (116 loc) · 3.87 KB
/
relay-for-toggl.user.js
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
// ==UserScript==
// @name Relay Toggl for Tribe
// @namespace https://central.tri.be/
// @version 0.1
// @description Adds a button to record task/description and start time to toggl.
// @author Gary Kovar
// @match *://central.tri.be/issues/*
// ==/UserScript==
// Your Toggl API key
var apiKeyToggl = 'toggleAPIkey';
// Or use your own
var proxyURL = 'https://relay-for-toggl.herokuapp.com/';
// Default activity text (ex. Back End Development)
var defaultActivity = '';
// This may differ from project to project, use defaultActivity for this to auto-detect
var defaultActivityID = '';
// You can customize the default timer text to make it easy for certain workflows
var defaultDescription = '';
window.tribeToggl = {};
(function( window, $, app, apiKeyToggl ) {
'use strict';
var cache = {};
app.init = function() {
$('.title-bar-actions div').append('<span id="toggl_container"></span>');
app.setupCache();
app.setupProjects();
app.getButton();
};
app.setupCache = function() {
cache.id = window.location.pathname.match(/\d+/)[0];
cache.project = $('title').text().trim().split('-')[0];
cache.taskName = $('#issue_subject').val();
cache.projects = $('ul #projects-menu li');
cache.project_names = [];
cache.projects.each(function(proj) {
cache.project_names.push($(this).text().replace('»', '').trim());
} );
cache.projectID = '';
cache.activityNumber = '';
cache.activityText = '';
cache.activityDescription = '';
};
// This makes sure each project is already set up.
app.setupProjects = function() {
$.post( proxyURL,
{ request: 'project',
action: 'setup',
api_key: apiKeyToggl,
projects : cache.project_names,
project_name : cache.project
}, 'json' )
.done(function( data ) {
cache.projectId = data;
});
};
app.getButton = function() {
$.post( proxyURL,
{ request: 'time',
action: 'running',
api_key: apiKeyToggl
}, 'json' )
.done(function( data ) {
data = $.parseJSON( data );
$('#toggl_container').html(app.startTime( data.running ) );
var $toggl_relay = $('#toggl-relay');
$toggl_relay.on('change', function (e) {
cache.activityNumber = $(e.target).val();
cache.activityText = $(e.target).find("option:selected").text();
$( 'button.time' ).show();
$( 'button.time-new' ).on( 'click', function() {
cache.activityDescription = $('#toggl_timerdescription').val();
$('.time').remove();
app.startToggl();
});
});
if ( '' !== defaultActivity ) {
var defaultActivityID = '';
$('option', $toggl_relay).each( function() {
var $this = $(this);
if ( $this.text() === defaultActivity ) {
defaultActivityID = $this.val();
}
} );
$toggl_relay.val( defaultActivityID );
$toggl_relay.trigger('change');
}
if ( '' !== defaultDescription ) {
$('#toggl_timerdescription').val( defaultDescription );
}
});
};
app.startTime = function( running ) {
var input = '<input type="text" class="time" id="toggl_timerdescription">';
//if ( running === 0 ) {
return app.activityType() + input + '<button class="time time-new" style="display: none">Start Timer</button>';
//} else {
// return app.activityType() + input + '<button class="time time-stop-new" style="display: none">Stop Timer / Start on This Task</button>' ;
//}
};
app.activityType = function() {
return '<select id="toggl-relay" class="time">' + $('#time_entry_activity_id').html() + '</select>';
};
app.startToggl = function() {
$.post( proxyURL,
{ request: 'time',
action: 'start',
api_key: apiKeyToggl,
description: '[' + cache.id + '][' + cache.activityText + '][' + cache.activityDescription + ']',
pid : cache.projectId
}, 'json' )
.done( function () {
app.getButton();
} );
};
app.init();
})( window, jQuery, window.tribeToggl, apiKeyToggl );