This repository was archived by the owner on Nov 25, 2024. It is now read-only.
forked from ryanramage/garden-app-support
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgarden-app-support.js
More file actions
140 lines (110 loc) · 4.14 KB
/
Copy pathgarden-app-support.js
File metadata and controls
140 lines (110 loc) · 4.14 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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
} else {
// Browser globals
root.returnExports = factory(root.$);
}
}(this, function ($) {
var exports = {}
/**
* Callback when the topbar has loaded.
*/
exports.on_topbar = function( timeout, callback) {
// retrieve arguments as array
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
callback = args.pop();
timeout = 5000;
if (args.length > 0) timeout = args.shift();
if ($('#dashboard-topbar').data('ready')) {
return callback(null);
}
var has_returned = false;
var on_complete = function(err) {
if (!has_returned) {
has_returned = true;
callback(err);
}
}
var iv = setInterval(function (){
if ($('#dashboard-topbar').data('ready')) {
return on_complete(null);
}
})
setTimeout(function() {
clearInterval(iv);
on_complete(new Error('Timeout waiting for the topbar'));
}, timeout);
$('#dashboard-topbar').on('ready', function(jquery_event){
on_complete(null);
});
}
/**
*
*
* @param after_login_url and optional url you want the user to be redirected to after they login. If omitted
* the user will be returned to the current url
*/
exports.get_login_url = function( after_login_url, callback) {
if (isFunction(after_login_url)) {
callback = after_login_url;
after_login_url = window.location;
}
exports.on_topbar(function(err){
if (err) return callback(err);
callback($('#dashboard-topbar-session').data('login') + '?redirect=' + encodeURIComponent(after_login_url));
})
}
exports.get_user_ctx = function(callback) {
exports.on_topbar(function(err){
if (err) return callback(err);
var userctx = JSON.parse(decodeURI($('#dashboard-topbar-session').data('userctx')));
return callback(null, userctx);
})
}
/**
* Get the all the garden details, as one easy to use call
* @param callback
*/
exports.get_garden_ctx = function(callback) {
exports.on_topbar(function(err){
if (err) return callback(err);
var garden_ctx = {
userCtx : JSON.parse(decodeURI($('#dashboard-topbar-session').data('userctx'))),
login_url : $('#dashboard-topbar-session').data('login') + '?redirect=' + encodeURIComponent(window.location)
};
return callback(null, garden_ctx);
})
}
/**
* There are times you want to be able to link to something, but you dont know where it will be.
* For example on a graph you may have a legend with things like 'zeptotrophic'. Lets say you want to be able to
* link to a definition of that word. So use this method like so
*
* garden.create_redirect_url('about', 'zeptotrophic');
*
* and use the result url as a link. The admin of the garden then can decide where the link should go. Maybe to something like
* /wiki/zeptotrophic
*
* Create a redirect url that can be mapped by a dashboard admin to the proper place.
*
* @name createRedirectUrl
* @param {String} category - A category like, wiki used to define the general place you want the url to go
* @param {String} id - An identifier for the resource in the category, like 'London_Bridge'
* @returns {String} - A url.
* @api public
*/
exports.create_redirect_url = function(category, id){
return '/dashboard/_design/dashboard/_rewrite/redirect/' + category + '/' + id;
}
return exports;
function isFunction(obj) {
return toString.call(obj) == '[object Function]';
};
}));