-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareTagJournal.krl
314 lines (273 loc) · 8.99 KB
/
SquareTagJournal.krl
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
ruleset a41x193 {
meta {
name "Journal"
description <<
Journal
Copyright 2012 Kynetx, All Rights Reserved
>>
author "Jessie A. Morris"
// Uncomment this line to require Marketplace purchase to use this app.
// authz require user
logging off
use module a169x701 alias CloudRain
use module a169x676 alias pds
use module a41x196 alias SquareTag
key aws {
"AWSAccessKey": "0GEYA8DTVCB3XHM819R2",
"AWSSecretKey": "I4TrjKcflLnchhsEzjlNju/s9EHiqdOScbyqGgn+"
}
use module a41x174 alias AWSS3
with AWSKeys = keys:aws()
}
dispatch {
// Some example dispatch domains
// domain "example.com"
// domain "other.example.com"
}
global {
thisRID = meta:rid();
thisECI = meta:eci();
SquareTagRID = "a41x178";
S3Bucket = "k-mycloud";
get_journal_entries = function(){
myEntries = ent:entries.reverse();
entriesListMade = myEntries.map(
function(entry) {
timeISO = entry{"time"};
time = time:strftime(timeISO, "%c");
entryText = entry{"entry"};
thumbnail = entry{"thumbnail"};
image = entry{"image"};
imageHTML = <<
<a href="#{image}" class="fancybox" rel="JournalGallery" data-fancybox-type="image">
<img src="#{thumbnail}" />
</a>
>>;
imageLink = (image && image neq "none") => imageHTML | "None";
thisEntry = <<
<tr>
<td>#{time}</td>
<td>#{entryText}</td>
<td>#{imageLink}</td>
</tr>
>>;
thisEntry
}
).join(" ");
entriesListEmpty = <<
<tr>
<td>You have no journal entries</td>
<td></td>
<td></td>
</tr>
>>;
entriesList = (myEntries.length() > 0) =>
entriesListMade |
entriesListEmpty;
entriesGallery = <<
<table class="table table-striped">
<thead>
<tr>
<th>Time</th>
<th>Entry</th>
<th>Image</th>
</tr>
</thead>
<tbody>
#{entriesList}
</tbody>
</table>
>>;
// return the gallery
entriesGallery
};
}
rule ifOwner {
select when explicit isOwner
or web cloudAppSelected
pre {
defaultAppHtml = SquareTag:get_default_app_html(thisRID);
profile = pds:get_all_me();
myProfileName = profile{"myProfileName"};
myProfilePhoto = profile{"myProfilePhoto"};
journalEntries = get_journal_entries();
formHTML = <<
#{defaultAppHtml}
<form id="formAddJournalEntry" class="form-horizontal form-mycloud">
<fieldset>
<div class="control-group">
<div class="controls">
<div class="thumbnail-wrapper" style="width: 100px;">
<div class="thumbnail mycloud-thumbnail">
<img src="#{myProfilePhoto}" alt="#{myProfileName}">
<h5 class="cloudUI-center">#{myProfileName}</h5>
</div> <!-- .thumbnail -->
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="entryText">Entry</label>
<div class="controls">
<textarea class="input-xlarge" name="entryText" title="Your journal entry" placeholder="Your journal entry"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="imageFileInput">Image (Optional)</label>
<div class="controls">
<input type="file" class="input-xlarge" id="imageFileInput" /><br />
<img id="thumbnailPreview" />
<input name="thumbnailSource" id="thumbnailSource" type="hidden"/>
<input name="imageSource" id="imageSource" type="hidden"/>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save Entry</button>
</div>
</fieldset>
</form>
>>;
entries = ent:entries;
html = <<
<div class="squareTag wrapper">
#{defaultAppHtml}
<ul id="myTab" class="nav nav-tabs">
<li class="active"><a href="#entryNewTab" data-toggle="tab">New Entry</a></li>
<li class="">
<a href="#entryTableTab" data-toggle="tab">
Journal Entries
<span class="badge">#{entries.length()}</span>
</a>
</li>
</ul>
<div class="tab-content" id="entryTabContent">
<div class="tab-pane fade active in" id="entryNewTab">
#{formHTML}
</div>
<div class="tab-pane fade" id="entryTableTab">
<div id="journalEntries" class="wrapper squareTag">
#{journalEntries}
</div>
</div>
</div>
</div>
>>;
entries = ent:entries;
}
{
SquareTag:inject_styling();
CloudRain:createAppPanel(thisRID, "Journal", {});
CloudRain:loadAppPanel(thisRID, html);
CloudRain:skyWatchPost("formAddJournalEntry", meta:eci());
emit <<
$K("#imageFileInput").change(function(e) {
var file = e.target.files[0];
KOBJ.canvasResize(file, {
width: 120,
height: 0,
crop: false,
quality: 80,
callback: function(data, width, height) {
$K("#thumbnailPreview").attr("src", data);
$K("#thumbnailSource").val(data);
}
});
KOBJ.canvasResize(file, {
width: 1200,
height: 1200,
crop: false,
quality: 80,
callback: function(data, width, height) {
$K("#imageSource").val(data);
}
});
});
$K('.fancybox').fancybox();
>>;
}
}
// ------------------------------------------------------------------------
rule saveBoard {
select when web submit "#formAddJournalEntry.post"
pre {
time = time:now({tz:'America/Denver'});
entryText = event:attr("entryText");
thumbnailSource = event:attr("thumbnailSource");
imageSource = event:attr("imageSource");
imageType = (thumbnailSource) => AWSS3:getType(thumbnailSource) | "none";
guid = random:uuid();
thumbName = "#{thisRID}/#{thisECI}-#{guid}-thumbnail.img";
thumbURL = (thumbnailSource) => "https://s3.amazonaws.com/#{S3Bucket}/#{thumbName}" | "none";
thumbValue = (thumbnailSource) => this2that:base642string(AWSS3:getValue(thumbnailSource)) | "none";
imageName = "#{thisRID}/#{thisECI}-#{guid}.img";
imageURL = (imageSource) => "https://s3.amazonaws.com/#{S3Bucket}/#{imageName}" | "none";
imageValue = (imageSource) => this2that:base642string(AWSS3:getValue(imageSource)) | "none";
entry = {
"thumbnail": thumbURL,
"image": imageURL,
"entry": entryText,
"time": time
};
entries = (ent:entries || []).append(entry);
}
if(thumbnailSource && imageSource) then {
AWSS3:upload(S3Bucket, thumbName, thumbValue)
with object_type = imageType;
AWSS3:upload(S3Bucket, imageName, imageValue)
with object_type = imageType;
}
always {
set ent:entries entries if
((imageSource && thumbnailSource) || entryText);
}
}
rule showEntries {
select when web submit "#formAddJournalEntry$"
pre {
journalEntries = get_journal_entries();
}
{
emit <<
$K("#journalEntries").html(journalEntries);
$K("#formAddJournalEntry")[0].reset();
$K('a[href="#entryTableTab"]').tab('show');
$K('.fancybox').fancybox();
>>;
CloudRain:hideSpinner();
}
}
rule resetEntries {
select when web submit "#formReset"
{
noop();
}
fired {
clear ent:entries;
}
}
rule makeDefault {
select when web cloudAppAction action re/makeDefault/
pre {
backChannel = event:attr("backChannel");
relationship = SquareTag:getRelationship(backChannel);
defaultOtherApps = (pds:get_item("SquareTag", "defaultApps") || {});
newDefaultOtherApps = (backChannel) =>
defaultOtherApps.put([backChannel], thisRID) | defaultOtherApps;
}
{
replace_html("#makeDefaultSquareTagApp", "");
CloudRain:hideSpinner();
}
fired {
raise pds event new_data_available
with _api = "sky"
and namespace = "SquareTag"
and keyvalue = "defaultApps"
and value = newDefaultOtherApps if (relationship && relationship{"relationship"} neq "slave");
raise pds event new_settings_attribute
with _api = "sky"
and setRID = SquareTagRID
and setAttr = "defaultOwnerApp"
and setValue = thisRID if (relationship{"relationship"} eq "slave");
}
}
}