corn_js 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,628 +0,0 @@
1
- var Popcorn = function ($element, defaults) {
2
- this.$element = $element;
3
- this.$anchor = this.$element;
4
- this.defaults = defaults;
5
- };
6
-
7
- Popcorn.prototype.decorateContainerWithHtml = function () {
8
- if (this.positionType) {
9
- throw "inferPositionType must be called after decorateContainerWithHtml";
10
- }
11
- this.containerOf().hide().addClass('popcorn');
12
- if (!this.containerOf().find('.popcorn-body').length) {
13
- this.containerOf().contents().wrap("<div class='popcorn-body'/>");
14
- }
15
- this.containerOf().append("<div class='popcorn-tail'></div>");
16
- };
17
-
18
- Popcorn.prototype.inferPositionType = function () {
19
- var self = this;
20
- this.$anchor = this.$element;
21
-
22
- function _createPositionType(defaults) {
23
-
24
- if (self.$element.offset().left < 1) {
25
- self.$anchor = self.$element.parent();
26
- }
27
-
28
- if (self.collideLeft()) {
29
- return new LeftPosition(self);
30
- }
31
- else if (self.collideRight()) {
32
- return new RightPosition(self);
33
- }
34
- return new CenterPosition(self);
35
- }
36
-
37
- self.positionType = _createPositionType(self.defaults);
38
- };
39
-
40
- Popcorn.prototype.decorateContainerWithArrow = function () {
41
- if (this.positionType == undefined) {
42
- throw "inferPositionType must be called in order to set up the arrows";
43
- }
44
-
45
- this.containerOf().find('.popcorn-tail').css('left', this.positionType.leftOffset());
46
- };
47
-
48
- var LeftPosition = function (popcorn) {
49
- // TODO centrare la freccia sull'elemento puntato da fatpopcorn
50
- // this.leftOffset = function() {return popcorn.$element.offset().left + (popcorn.$element.width() - popcorn.defaults.arrowWidth) / 2; }
51
- this.leftOffset = function () {
52
- return popcorn.defaults.marginArrow;
53
- }
54
- this.left = function () {
55
- return popcorn.defaults.marginBorder;
56
- }
57
- this.top = function () {
58
- return popcorn.$anchor.offset().top + popcorn.defaults.verticalOffsetFromElement
59
- };
60
- };
61
-
62
- var RightPosition = function (popcorn) {
63
- this.leftOffset = function () {
64
- return popcorn.containerOf().width() - (popcorn.defaults.arrowWidth + popcorn.defaults.marginArrow);
65
- }
66
- this.left = function () {
67
- return $('html').width() - popcorn.defaults.marginBorder - popcorn.containerOf().width();
68
- }
69
- this.top = function () {
70
- return popcorn.$anchor.offset().top + popcorn.defaults.verticalOffsetFromElement
71
- };
72
- };
73
-
74
- var CenterPosition = function (popcorn) {
75
- this.leftOffset = function () {
76
- return popcorn.containerOf().width() / 2 - Math.floor(popcorn.defaults.arrowWidth / 2);
77
- }
78
- this.left = function () {
79
- var middleOfElement = Popcorn.calculateMiddle(popcorn.$anchor.offset().left, popcorn.$anchor.width());
80
- return Popcorn.calculateLeftOffset(middleOfElement, popcorn.containerOf().width());
81
- }
82
- this.top = function () {
83
- return popcorn.$anchor.offset().top + popcorn.defaults.verticalOffsetFromElement
84
- };
85
- };
86
-
87
- Popcorn.containerOf = function ($element) {
88
- return $element.next();
89
- }
90
-
91
- Popcorn.prototype.containerOf = function () {
92
- return Popcorn.containerOf(this.$element);
93
- }
94
-
95
- Popcorn.prototype.setContainerPosition = function () {
96
- this.containerOf().css('top', this.positionType.top());
97
- this.containerOf().css('left', this.positionType.left());
98
- }
99
-
100
- Popcorn.prototype.collideRight = function () {
101
- var middleOfElement = Popcorn.calculateMiddle(this.$anchor.offset().left, this.$anchor.width());
102
- var rightOffset = middleOfElement + this.containerOf().width() / 2;
103
- return ($('html').width() - (rightOffset + this.defaults.marginBorder)) < 0;
104
- }
105
-
106
- Popcorn.prototype.collideLeft = function () {
107
- return (Popcorn.calculateLeftOffset(this.middleOf(), this.containerOf().width()) - this.defaults.marginBorder) < 0;
108
- }
109
-
110
- Popcorn.prototype.middleOf = function () {
111
- return Popcorn.calculateMiddle(this.$anchor.offset().left, this.$anchor.width());
112
- }
113
-
114
- Popcorn.calculateMiddle = function (left, width) {
115
- return left + width / 2;
116
- }
117
-
118
- Popcorn.calculateRightOffset = function (middlePoint, width) {
119
- return middlePoint + width / 2;
120
- }
121
- Popcorn.calculateLeftOffset = function (middlePoint, width) {
122
- return middlePoint - width / 2;
123
- }
124
- Popcorn.containerOf = function (element) {
125
- return $(element).next();
126
- }
127
- Popcorn.hideAllContainers = function ($elements) {
128
- $elements.each(function () {
129
- Popcorn.containerOf(this).hide();
130
- });
131
- }
132
- Popcorn.hideAllContainersExcept = function ($elements, element) {
133
- $elements.not(element).each(function () {
134
- Popcorn.containerOf(this).hide()
135
- });
136
- };
137
-
138
- var FatPopcorn = function ($element, defaults) {
139
- function _checkOptions(options) {
140
- if (!options.autoWrap) options.autoWrap = false;
141
-
142
- return typeof options.modelId === undefined ||
143
- options.token === undefined ||
144
- options.current_user === undefined;
145
- }
146
-
147
- ;
148
- var self = this;
149
-
150
- if (_checkOptions(defaults)) throw("parameters [token], [current_user] are required");
151
-
152
- self.$element = $element;
153
- self.defaults = defaults;
154
-
155
-
156
- self.defaults.modelName = "#"
157
- };
158
- FatPopcorn.prototype = new Popcorn();
159
-
160
- FatPopcorn.prototype.init = function () {
161
- this.setupFormAction();
162
- this.setupFormToken();
163
- this.setupStreamUrl();
164
- this.setupHistoryUrl();
165
- this.setupEditForm();
166
- this.setupWatchlistUrl();
167
-
168
- if (this.hasStream()) {
169
- $('.fatpopcorn .stream-tab').click();
170
- }
171
- else {
172
- $('.fatpopcorn .edit-tab').click();
173
- }
174
- };
175
-
176
- FatPopcorn.prototype.setupStreamUrl = function () {
177
- $('.fatpopcorn .stream').attr('data-url', this.streamUrl());
178
- };
179
- FatPopcorn.prototype.setupEditForm = function () {
180
- FatPopcorn.createAttachmentButton(this.attachmentsUrl());
181
- $('.fatpopcorn .edit').attr('data-attach-url', this.attachmentsUrl());
182
- $('.on-off label.' + this.$element.attr('data-watching')).click();
183
- };
184
- FatPopcorn.prototype.setupWatchlistUrl = function () {
185
- $('.fatpopcorn .edit').attr('data-url', this.watchlistUrl());
186
- };
187
- FatPopcorn.prototype.setupHistoryUrl = function () {
188
- $('.fatpopcorn .history').attr('data-url', this.historyUrl());
189
- };
190
- FatPopcorn.prototype.setupFormAction = function () {
191
- $('.fatpopcorn #notes_form').attr('action', this.actionUrl());
192
- $('.fatpopcorn .edit').attr('data-note-url', this.actionUrl());
193
- };
194
- FatPopcorn.prototype.setupFormToken = function () {
195
- $('.fatpopcorn #notes_form input[name="authenticity_token"]').val(FatPopcorn.formToken());
196
- };
197
- FatPopcorn.prototype.actionUrl = function () {
198
- return this.urlPrefix() + '/notes';
199
- };
200
- FatPopcorn.prototype.streamUrl = function () {
201
- return this.urlPrefix() + '/stream';
202
- };
203
- FatPopcorn.prototype.attachmentsUrl = function () {
204
- return this.urlPrefix() + '/attachments';
205
- };
206
- FatPopcorn.prototype.watchlistUrl = function () {
207
- return this.urlPrefix() + '/watchers/' + this.defaults.current_user;
208
- };
209
- FatPopcorn.prototype.historyUrl = function () {
210
- return this.urlPrefix() + '/histories';
211
- };
212
- FatPopcorn.prototype.urlPrefix = function () {
213
- return '/active_metadata/' + this.$element.attr('data-model') + '/' + this.$element.attr('data-model-id') + '/' + this.$element.attr('data-label');
214
- };
215
- FatPopcorn.prototype.currentLabel = function () {
216
- return this.$element.attr('data-label');
217
- };
218
- FatPopcorn.prototype.hasStream = function () {
219
- return parseInt(this.$element.attr('data-stream')) > 0;
220
- };
221
- FatPopcorn.hideContainer = function () {
222
- $(window).off('resize');
223
- return FatPopcorn.container().hide();
224
- }
225
- FatPopcorn.container = function () {
226
- return $('.fatpopcorn').first();
227
- }
228
- FatPopcorn.prototype.containerOf = function () {
229
- return $('.fatpopcorn').first();
230
- }
231
- FatPopcorn.onCompleteUpload = function (id, fileName, response, qq) {
232
- console.log('FatPopcorn.onCompleteUpload');
233
- if (qq.getQueue().length == 1) {
234
- $('.qq-upload-list').empty();
235
- }
236
- if (!response.success) {
237
- console.log(response);
238
- FatPopcorn.displayFailure("Si è verificato un errore.");
239
- return;
240
- }
241
- FatPopcorn.newNoteOrAttachmentSuccess(response);
242
- }
243
- FatPopcorn.decorateContainerWithHtml = function () {
244
- var self = this;
245
-
246
- function _html() {
247
- return '<div class="fatpopcorn"><div class="popcorn-body"><div class="header"><ul><li class="stream-tab"><div>stream</div></li>' +
248
- '<li class="edit-tab"><div>edit</div></li><li class="history-tab"><div>history</div></li></ul></div>' +
249
- '<div class="stream"><div class="content"></div></div><div class="history"><div class="content"></div></div>' +
250
- '<div class="edit"><div class="watchlist"><h1>Watchlist</h1><div class="on-off _23states"><input name="watchlist" id="watchlist_true" value="true" type="radio"><label class="true" for="watchlist_true"><span>On</span></label><input checked="checked" name="watchlist" id="watchlist_false" value="false" type="radio"><label class="false" for="watchlist_false"><span>Off</span></label></div></div><hr/>' +
251
- '<div class="note"><h1>Nota</h1><form form action="" method="post" id="notes_form"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"><input type="hidden" value="' + self['token'] + '" name="authenticity_token"></div>' +
252
- '<textarea id="note_text" name="note" rows="4"></textarea><a id="send_note">Inserisci</a></form></div><hr/>' +
253
- '<div class="attachment"><h1>Allegati</h1><div id="fatpopcorn_attach"></div><div id="attach_output"></div></div>' +
254
- '<div class="info"><h1>Info</h1><p>Lorem ipsum...</p></div></div></div><div class="popcorn-tail"></div><span class="loader"></span></div>';
255
- }
256
-
257
- ;
258
-
259
- if (FatPopcorn.container().size() == 0) {
260
- $('body').append(_html());
261
- }
262
- FatPopcorn.container().hide();
263
- }
264
-
265
- FatPopcorn.prototype.addGripToElement = function ($element) {
266
- if (!$element.parent().is('span.fatpopcorn_grip') && this.defaults.autoWrap) {
267
- $element.wrap('<span class="fatpopcorn_grip"/>')
268
- }
269
- return $element.parent();
270
- };
271
- FatPopcorn.prototype.gripOf = function ($element) {
272
- return $element.parent();
273
- };
274
-
275
- FatPopcorn.activateTheClickedTab = function () {
276
- $('.fatpopcorn .header > ul > li').unbind('click').click(function (e) {
277
- var self = this;
278
-
279
- function _tabBodyName(tabName) {
280
- return tabName.split('-')[0].trim();
281
- }
282
-
283
- ;
284
- function _currentTabName() {
285
- return _tabBodyName($(self).attr('class'));
286
- }
287
-
288
- ;
289
- function _currentTab() {
290
- return $('.' + _currentTabName());
291
- }
292
-
293
- ;
294
- function _currentTabMethod() {
295
- return _currentTabName() + "Event";
296
- }
297
-
298
- e.stopPropagation();
299
- e.preventDefault();
300
-
301
- $('.fatpopcorn .active').removeClass('active');
302
- $('.fatpopcorn .popcorn-body > div:not(.header)').hide();
303
-
304
- _currentTab().show();
305
-
306
- $(this).addClass('active');
307
-
308
- FatPopcorn[_currentTabMethod()].call();
309
- });
310
- };
311
- FatPopcorn.streamEvent = function () {
312
- $.ajax($('.fatpopcorn .stream').attr('data-url'))
313
- .success(FatPopcorn.getStreamSuccess);
314
- };
315
- FatPopcorn.editEvent = function () {
316
- // should do something?
317
- };
318
- FatPopcorn.historyEvent = function () {
319
- $.ajax($('.fatpopcorn .history').attr('data-url'))
320
- .success(FatPopcorn.getHistorySuccess);
321
- };
322
- FatPopcorn.containerVisible = function () {
323
- return FatPopcorn.container().is(':visible');
324
- };
325
- FatPopcorn.formToken = function () {
326
- return $('meta[name="csrf-token"]').attr('content');
327
- };
328
- FatPopcorn.createAttachmentButton = function (actionUrl) {
329
- delete FatPopcorn.uploader;
330
- FatPopcorn.uploader = new qq.FileUploader({
331
- element:document.getElementById('fatpopcorn_attach'),
332
- allowedExtensions:[],
333
- params:{ authenticity_token:FatPopcorn.formToken(), target:"attach_output"},
334
- uploadButtonText:'Inserisci',
335
- action:actionUrl,
336
- multiple:true,
337
- onComplete:FatPopcorn.onCompleteUpload
338
- });
339
- };
340
-
341
- FatPopcorn.bindRemoteEvents = function () {
342
-
343
- function _startWatching() {
344
- _callWatchlistService({authenticity_token:FatPopcorn.formToken() });
345
- }
346
-
347
- ;
348
- function _stopWatching() {
349
- _callWatchlistService({_method:'delete', authenticity_token:FatPopcorn.formToken() });
350
- }
351
-
352
- ;
353
- function _callWatchlistService(data) {
354
- var url = $('.fatpopcorn .edit').attr('data-url');
355
- $.post(url, data, {dataType:'script'}).done(FatPopcorn.watchingServiceSuccess).error(FatPopcorn.watchingServiceFail);
356
- }
357
-
358
- ;
359
- $('.fatpopcorn').unbind('click').click(function (e) {
360
- e.stopPropagation();
361
- });
362
- $('.fatpopcorn #watchlist_true').unbind('click').click(function () {
363
- _startWatching()
364
- });
365
- $('.fatpopcorn #watchlist_false').unbind('click').click(function () {
366
- _stopWatching();
367
- });
368
-
369
- $('#send_note').unbind('click').click(function () {
370
- if ($('#note_text').val() == '') return false;
371
-
372
- $.post($('form#notes_form').attr('action'), $('form#notes_form').serialize())
373
- .success('success.rails', FatPopcorn.newNoteOrAttachmentSuccess)
374
- .fail(function () {
375
- FatPopcorn.displayFailure("Si è verificato un errore.")
376
- });
377
- });
378
- $('.loader').ajaxSend(function (e) {
379
- $(this).show();
380
- });
381
- $('.loader').ajaxComplete(function () {
382
- $(this).hide();
383
- });
384
- };
385
-
386
- /******
387
- Notifier
388
- *******/
389
- FatPopcorn.notifier = {
390
-
391
- notify: function (type, message) {
392
- type = type || "notice";
393
- this.removeBox();
394
- var $messageBox = $('.fatpopcorn .info h1').after('<p class="' + type + ' messageBox">' + message + '</p>').next();
395
- var self = this;
396
- $messageBox.bind('click', function () {
397
- self.removeBox();
398
- });
399
- },
400
-
401
- notice : function(message){
402
- this.notify("notice",message);
403
- },
404
-
405
- error : function(message){
406
- this.notify("error",message);
407
- },
408
-
409
- removeBox : function(){
410
- $('.fatpopcorn .info p.messageBox').remove();
411
- }
412
-
413
- };
414
-
415
- /*****
416
- * Error Handling
417
- *****/
418
- FatPopcorn.displayFailure = function (message) {
419
- FatPopcorn.notifier.error(message);
420
- };
421
-
422
- /* ajax callbacks */
423
- FatPopcorn.watchingServiceSuccess = function (jqxhr) {
424
- var data = eval(jqxhr);
425
- //remove the error/notice messageBox
426
- FatPopcorn.notifier.removeBox();
427
- FatPopcorn.item(data).attr('data-watching', data.watching);
428
- }
429
- FatPopcorn.watchingServiceFail = function (data) {
430
- FatPopcorn.displayFailure("Si è verificato un errore.")
431
- console.log('Watchlist service request failed');
432
- console.log(data);
433
- console.log(data.state());
434
- console.log(data.statusCode());
435
- console.log(data.getAllResponseHeaders());
436
-
437
- }
438
-
439
- FatPopcorn.item = function (data) {
440
- return $('[data-model="' + data.modelName + '"][data-label="' + data.fieldName + '"]');
441
- }
442
-
443
- FatPopcorn.newNoteOrAttachmentSuccess = function (dataString) {
444
- var data = eval(dataString)
445
- //remove the error/notice messageBox
446
- FatPopcorn.notifier.removeBox();
447
- FatPopcorn.item(data).attr('data-stream', data.streamItemsCount);
448
-
449
- if (data.streamItemsCount > 0) {
450
- FatPopcorn.item(data).parents('.fatpopcorn_grip').addClass('has-stream');
451
- FatPopcorn.item(data).siblings('.stream-items-count').text(data.streamItemsCount);
452
- if (data.streamItemsCount > 9)
453
- FatPopcorn.item(data).siblings('.stream-items-count').addClass('two-digits')
454
- else
455
- FatPopcorn.item(data).siblings('.stream-items-count').removeClass('two-digits')
456
- } else {
457
- FatPopcorn.item(data).parents('.fatpopcorn_grip').removeClass('has-stream');
458
- FatPopcorn.item(data).siblings('.stream-items-count').empty();
459
- }
460
- $('.fatpopcorn textarea#note_text').val('');
461
- $('.fatpopcorn .active').removeClass('active');
462
- $('.fatpopcorn .popcorn-body > div:not(.header)').hide();
463
- $(".fatpopcorn .stream").show();
464
- $(".fatpopcorn .stream-tab").addClass('active');
465
- FatPopcorn.getStreamSuccess(data.streamBody);
466
-
467
- };
468
-
469
- FatPopcorn.getStreamSuccess = function (data) {
470
- $('.fatpopcorn .stream .content').html(data);
471
- $('.fatpopcorn .stream .attachment span.delete').click(FatPopcorn.deleteAttachment);
472
- $('.fatpopcorn .stream .note span.delete').click(FatPopcorn.deleteNote);
473
- $('.fatpopcorn .stream span.star').click(FatPopcorn.starUnstar);
474
- };
475
-
476
- FatPopcorn.deleteAttachment = function (e) {
477
- if(confirm("Sei sicuro?")){
478
- FatPopcorn.deleteStream(e, $('.fatpopcorn .edit').attr('data-attach-url'));
479
- }
480
- };
481
-
482
- FatPopcorn.deleteNote = function (e) {
483
- if(confirm("Sei sicuro?")){
484
- FatPopcorn.deleteStream(e, $('.fatpopcorn .edit').attr('data-note-url'));
485
- }
486
- };
487
-
488
- FatPopcorn.deleteStream = function (e, urlPrefix) {
489
- function _url() {
490
- return urlPrefix + '/' + $(e.target).parent().attr('data-id');
491
- }
492
-
493
- $.post(_url(), {_method:'delete'}).
494
- success('success.rails', FatPopcorn.newNoteOrAttachmentSuccess).
495
- fail(FatPopcorn.deleteFailure);
496
- };
497
-
498
- FatPopcorn.starUnstar = function (e, urlPrefix) {
499
- var url = $(e.target).attr('data-url');
500
- $.post(url, {_method:'put'}).
501
- success('success.rails',
502
- function (data) {
503
- FatPopcorn.getStreamSuccess(data.streamBody)
504
- }).
505
- fail(FatPopcorn.deleteFailure);
506
- };
507
-
508
- FatPopcorn.deleteSuccess = function () {
509
- $('.fatpopcorn .stream-tab').click();
510
- };
511
-
512
- FatPopcorn.deleteFailure = function () {
513
- };
514
-
515
- FatPopcorn.getHistorySuccess = function (data) {
516
- $('.fatpopcorn .history .content').empty();
517
- $('.fatpopcorn .history .content').append(data);
518
- };
519
-
520
- (function ($) {
521
- $.fn.fatpopcorn = function (options) {
522
-
523
- // plugin default options
524
- var self = this, defaults = {
525
- marginBorder:4,
526
- arrowWidth:24,
527
- marginArrow:11,
528
- verticalOffsetFromElement:26
529
- };
530
-
531
- // extends defaults with options provided
532
- if (options) {
533
- $.extend(defaults, options);
534
- }
535
-
536
- $(window).click(function () {
537
- FatPopcorn.hideContainer();
538
- FatPopcorn.notifier.removeBox();
539
- });
540
-
541
- FatPopcorn.decorateContainerWithHtml();
542
- FatPopcorn.activateTheClickedTab();
543
- FatPopcorn.bindRemoteEvents();
544
- FatPopcorn.createAttachmentButton();
545
-
546
- function _setUpElement() {
547
- var $element = $(this), fatpopcorn = new FatPopcorn($element, defaults);
548
- fatpopcorn.addGripToElement($element);
549
-
550
- fatpopcorn.gripOf($element).children().click(function (e) {
551
- $(e.target).data('elementMatched', true);
552
- });
553
-
554
- $('.fatpopcorn_grip .stream-items-count').click(function (e) {
555
- $(e.target).data('elementMatched', false);
556
- });
557
-
558
- fatpopcorn.gripOf($element).click(function (e) {
559
- if ($(e.target).data('elementMatched')) return;
560
-
561
- e.stopPropagation();
562
- e.preventDefault();
563
- fatpopcorn.init();
564
-
565
- if (FatPopcorn.containerVisible()) {
566
- FatPopcorn.hideContainer();
567
- return;
568
- }
569
-
570
- fatpopcorn.inferPositionType();
571
- fatpopcorn.setContainerPosition();
572
- fatpopcorn.decorateContainerWithArrow();
573
- fatpopcorn.containerOf().show();
574
-
575
- $(window).on('resize', function () {
576
- if (FatPopcorn.containerVisible()) fatpopcorn.setContainerPosition();
577
- });
578
- });
579
-
580
- return this;
581
- }
582
-
583
- return self.each(_setUpElement);
584
- }
585
-
586
- $.fn.popcorn = function (options) {
587
-
588
- // plugin default options
589
- var elements = this, defaults = {
590
- marginBorder:4,
591
- arrowWidth:19,
592
- marginArrow:10,
593
- verticalOffsetFromElement:20
594
- };
595
-
596
- // extends defaults with options provided
597
- if (options) {
598
- $.extend(delfaults, options);
599
- }
600
-
601
- $(window).unbind('click').click(function () {
602
- Popcorn.hideAllContainers(elements);
603
- });
604
-
605
- function _setUpElement() {
606
- var $element = $(this), popcorn = new Popcorn($element, defaults);
607
-
608
- popcorn.decorateContainerWithHtml();
609
- popcorn.inferPositionType();
610
- popcorn.decorateContainerWithArrow();
611
- popcorn.setContainerPosition();
612
-
613
- $element.click(function (e) {
614
- e.stopPropagation();
615
- e.preventDefault();
616
- Popcorn.hideAllContainersExcept(elements, this);
617
- Popcorn.containerOf($element).show();
618
- });
619
-
620
- $(window).resize(function () {
621
- popcorn.setContainerPosition();
622
- });
623
- }
624
-
625
- return this.each(_setUpElement);
626
-
627
- };
628
- })(jQuery);