-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdeckster.coffee
1502 lines (1240 loc) · 51.7 KB
/
deckster.coffee
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#THESE NEED TO MATCH THE CSS
_css_variables =
selectors:
deck: '.deckster-deck'
card: '.deckster-card'
card_title: '.deckster-card-title'
controls: '.deckster-controls'
deck_controls: '.deck-controls'
drag_handle: '.deckster-drag-handle'
expand_handle: '.deckster-expand-handle'
collapse_handle: '.deckster-collapse-handle'
card_jump_scroll: '.deckster-card-jump-scroll'
deck_jump_scroll: '.deckster-deck-jump-scroll'
remove_handle: '.deckster-remove-handle'
removed_dropdown: '.deckster-removed-dropdown'
removed_card_li: '.deckster-removed-card-li'
removed_card_button: '.deckster-removed-card-button'
add_card_to_bottom_button: '.deckster-add-card-to-bottom-button'
card_content:'.content'
placeholders: '.placeholders'
droppable:'.droppable'
deck_title:'.deckster-title'
deck_container:'.deckster-deck-container'
selector_functions:
card_expanded: (option)->'[data-expanded='+option+']'
deck_expanded: (option) -> '[data-cards-expanded='+option+']'
classes: {}
dimensions: {}
styleSheet: "deckster.css"
# if no title available, display this many chars from the content section
chars_to_display: 20
buffer: "b"
###
Default Ajax options, some options are typically overwritten.
###
_ajax_default =
success: (data,status, response) ->
console.log("Success: "+status)
error: (response,status,exception) ->
console.log("Status: "+status+" Error: "+exception)
timeout: 3000
async: true
###
Used to keep track of ajax requests. Typically stored as _ajax_requests[deckId][cardId] = $.ajax(...)
###
_ajax_requests = {}
_css_variables.classes[sym] = selector[1..] for sym, selector of _css_variables.selectors
# Jump scroll area
_scrollToView = ($el) ->
offset = $el.offset()
offset.top -= 20
offset.left -= 20
$('html, body').animate {
scrollTop: offset.top
scrollLeft: offset.left
}
_nav_menu = null # Feel free to rename this if something else fits better
_nav_menu_options = {}
###
# Creates the Bootstrap-based Navigation menu/Jump Scroll bar/Scroll helper from HTML,
# applies config options, places it in the DOM tree and returns the new element
###
_create_nav_menu = () ->
markup = """<div class="btn-group #{_css_variables.classes.deck_jump_scroll}">
<span class="dropdown-toggle control jump-deck" data-toggle="dropdown"></span>
<ul class="dropdown-menu pull-right"></ul>
</div>
</div>
""" # "stupid emacs
button_dom = $ markup
### Let the Design/Developer place this in CSS
stay_in_view = _nav_menu_options["stay-in-view"]
if stay_in_view? and not stay_in_view
outer_el = document
button_dom.css 'position', 'absolute'
else
outer_el = window # outer_el is what we're going to measure to place the button bar
left = false
x_pos =_nav_menu_options["x-position"]
calculate_x = () ->
if x_pos is "left"
left = "5px"
else if x_pos is "right"
button_dom.css "right", "5px"
button_dom.find("ul.dropdown-menu")
.removeClass("pull-left")
.addClass("pull-right")
else if x_pos is "middle"
bw = button_dom.find(_css_variables.selectors.deck_jump_scroll)
.width()
left = ($(outer_el).width() - bw) / 2
else
if left
button_dom.css "left", left
y_pos = _nav_menu_options["y-position"]
top = "5px"
calculate_top = () ->
if y_pos is "bottom"
top = ($(outer_el).height() - button_dom.height()) - 5
button_dom.addClass("dropup")
else if y_pos is "middle"
top = ($(outer_el).height() - button_dom.height()) / 2
button_dom.css "top", top
# Apply calculate functions once to get approximate positioning
calculate_x()
calculate_top()
###
$("body").append button_dom
###
# Re-calculate with button size known
calculate_top()
calculate_x()
###
# This makes sure something relevant is returned
button_dom
# Designed both for scrolling to a deck and scrolling to a card in any deck.
# Builds the list based on all elements present in the DOM that match
# the title-selector (e.g., '.deckster-deck [data-title]' for a card
# with a title
_create_jump_scroll = (target_ul_selector, title_selector,classId) ->
_nav_menu ?= _create_nav_menu()
$item_title_ddl = $ target_ul_selector
# Start fresh
$item_title_ddl.children().remove()
$title_items = $ title_selector
if $title_items.length is 0
return
$title_items.each (index, item) ->
title = $(item).data 'title'
elementId = $(item).attr("data-card-id") ? $(item).attr("id")
$nav_item = $ "<li id='#{classId+"-"+elementId}'><a href='#'>#{title}</a></li>"
# Set up the click callback for the menu item
$nav_item.on 'click', () ->
_scrollToView $ item
$item_title_ddl.append $nav_item
_create_jump_scroll_card = ($deck) ->
# Collect all data-title cards from given deck
_create_jump_scroll("#"+$deck.attr("id")+"-nav"+" ul",
"#"+$deck.attr("id")+'.deckster-deck [data-title]',
_css_variables.classes.card_jump_scroll)
_create_jump_scroll_deck = () ->
_create_jump_scroll "#{_css_variables.selectors.deck_jump_scroll} ul",
'.deckster-deck[data-title]',
_css_variables.classes.deck_jump_scroll
jQuery.deckster = (options)->
console.log("Registering global callbacks")
_document.__deck_mgr = options
window.Deckster = (options) ->
$deck = $(this)
original_content = $deck[0].outerHTML
unless $deck.hasClass(_css_variables.classes.deck)
return console.log 'Not a valid deck'
# Options
__default_options =
draggable: true
expandable: true
url_enabled:true
removable: true
droppable: true
persist: true
options = $.extend {}, __default_options, options
###
# Modify an option setting (with the config_option key) based on the
# presence and value of a corresponding data- attribute (data_attr)
# on the Deck DOM element
###
__set_option = (data_attr, config_option) ->
option = $deck.data data_attr
if option?
options[config_option or data_attr] = option in [true, 'true']
# if the data- attribute is not found, don't change the value
__set_option 'draggable'
__set_option 'expandable'
__set_option 'removable'
__set_option 'url-enabled', 'url_enabled'
__set_option 'droppable'
__set_option 'persist'
###
Init Dragging options
###
options.animate = options.animate ? {}
options.animate.properties = options.animate.properties ? {}
options.animate.options = options.animate.options ? {}
###
# Nav menu options (global)
###
$.extend(_nav_menu_options, options["scroll-helper"])
###
Deckster Base
--- Deckster Base Variables
###
__next_id = 1
__deck = {}
__cards_by_id = {}
__card_data_by_id = {}
__col_max = 0
__row_max = 0
__cards_needing_resolved_in_order = []
__cards_needing_resolved_by_id = {}
__dominate_card_data = undefined
__is_saved = false
###
Registered callbacks events.
###
__events =
card_added: 'card_added'
inited: 'inited'
card_expanded: 'card_expanded'
card_collapsed: 'card_collapsed'
card_moved:'card_moved'
__event_callbacks = {}
# --- Deckster Base Functions
_on = (event, callback) ->
__event_callbacks[event] = [] unless __event_callbacks[event]?
__event_callbacks[event].push callback
_ajax = (options) ->
options = $.extend(true,{},_ajax_default,options)
$.ajax(options)
_add_card = ($card, d) ->
throw 'Card is too wide' if d.col_span > __col_max
_force_card_to_position $card, d, {row: d.row, col: d.col}
retain_callbacks = []
for callback in __event_callbacks[__events.card_added] || []
unless callback($card, d) == false
retain_callbacks.push(callback)
__event_callbacks[__events.card_added] = retain_callbacks
_force_card_to_position = ($card, d, p) ->
console.log("__col_max",__col_max)
throw 'Card expands out of bounds' if p.col + (d.col_span - 1) > __col_max
_mark_card_as_resolved d
__dominate_card_data = d
_identify_problem_cards()
__deck = {}
_document.__deck_mgr = _document.__deck_mgr || {}
_loop_through_spaces p.row, p.col, (p.row + (d.row_span - 1)), (p.col + (d.col_span - 1)), (p2) ->
__deck[p2.row] = {} unless __deck[p2.row]?
__deck[p2.row][p2.col] = d.id
_resolve_cards()
_mark_card_as_resolved = (d) ->
if __cards_needing_resolved_in_order.length > 0
i = $.inArray(d.id, __cards_needing_resolved_in_order)
if i > -1
__cards_needing_resolved_in_order.splice i, 1
delete __cards_needing_resolved_by_id[d.id]
_identify_problem_cards = () ->
for row, cols of __deck
for col, id of cols
unless id == undefined || id == __dominate_card_data.id || __cards_needing_resolved_by_id[id]?
__cards_needing_resolved_by_id[id] = true
__cards_needing_resolved_in_order.push id
_loop_through_spaces = (row_start, col_start, row_end, col_end, callback) ->
row_i = row_start
while row_i <= row_end
col_i = col_start
while col_i <= col_end
p =
row: row_i
col: col_i
r_value = callback p
return if r_value == false # gives the option to break the loop
col_i++
row_i++
_resolve_cards = () ->
while __cards_needing_resolved_in_order.length > 0
id = __cards_needing_resolved_in_order[0]
$card = __cards_by_id[id]
d = __card_data_by_id[id]
_resolve_card_position $card, d
_mark_card_as_resolved d
_resolve_card_position = ($card, d) ->
row_i = 1
while true # WARNING --- MUST BREAK LOOP
__deck[row_i] = {} unless __deck[row_i]?
col_i = 1
while col_i <= (__col_max - d.col_span) + 1
can_go_here = true
# can the card start here
_loop_through_spaces row_i, col_i, (row_i + (d.row_span - 1)), (col_i + (d.col_span - 1)), (p2) ->
__deck[p2.row] = {} unless __deck[p2.row]?
if __deck[p2.row][p2.col]
can_go_here = false
return false
# if so, then put it here
if can_go_here == true
_loop_through_spaces row_i, col_i, (row_i + (d.row_span - 1)), (col_i + (d.col_span - 1)), (p2) ->
__deck[p2.row] = {} unless __deck[p2.row]?
__deck[p2.row][p2.col] = d.id
return
col_i++
row_i++
###
Used to transition cards to new positions on the deck. Typical scenario arises when a card is being dragged to a new position and adjacent cards need to be repositioned.
Transition positions are looked up and cached locally.
###
_apply_transition = ($card,d) ->
rowStr = _css_variables.selectors.card+"[data-row=\""+d.row+"\"]"
colStr = _css_variables.selectors.card+"[data-col=\""+d.col+"\"]"
_css_variables.dimensions = _css_variables.dimensions || {}
leftAnimate = _css_variables.dimensions[colStr]
topAnimate = _css_variables.dimensions[rowStr]
#Did we have this value saved?
unless leftAnimate? and topAnimate?
mysheet = null
for sheet, index in document.styleSheets
if _css_variables.styleSheet == sheet.href.split("/").pop()
mysheet = sheet
break
if mysheet == null
$card.attr 'data-row', d.row
$card.attr 'data-col', d.col
$card.css 'opacity','1'
return
myrules = mysheet.cssRules ? mysheet.rules
for rule,index in myrules
if rule.selectorText == rowStr
topAnimate = rule.style.top
_css_variables.dimensions[rowStr] = topAnimate
else if rule.selectorText == colStr
leftAnimate = rule.style.left
_css_variables.dimensions[colStr] = leftAnimate
options.animate.properties.top = topAnimate
options.animate.properties.left = leftAnimate
options.animate.options.duration?= "slow"
options.animate.options.easing?= "swing"
options.animate.options.always = () ->
$card.attr 'data-row', d.row
$card.attr 'data-col', d.col
$card.css 'opacity','1'
###
The animation becomes confusing and inaccurate when to many animations are attempted on the same card;Solution: Stop current and pending animations and start just this one.
###
$card.stop(true,false).animate(options.animate.properties, options.animate.options)
_apply_deck = () ->
row_max = 0
applied_card_ids = {}
isDragging = true
for row, cols of __deck
for col, id of cols
unless applied_card_ids[id]?
applied_card_ids[id] = true
$card = __cards_by_id[id]
__card_data_by_id[id].row = parseInt row
__card_data_by_id[id].col = parseInt col
d = __card_data_by_id[id]
$card.attr 'data-card-id', id
if isDragging and not $card.hasClass "draggable"
_apply_transition($card,d)
else
$card.attr 'data-row', d.row
$card.attr 'data-col', d.col
$card.attr 'data-row-span', d.row_span
$card.attr 'data-col-span', d.col_span
row_max_value = d.row + d.row_span - 1
__row_max = row_max_value if row_max_value > __row_max
$deck.attr 'data-row-max', __row_max
###
# Initially, cards will be hidden if the 'data-hidden' attribute is true, or
# if the deck's 'remove-empty' attribute is true, and
# there is no card content, and
# there is no 'data-url' attribute
###
_should_remove_card_in_init = ($card, $deck) ->
($card.data('hidden') == true or
($deck.data('remove-empty') == true and
!$card.find(_css_variables.selectors.card_content).text().trim() and
!$card.data('url')))
_init_deck_header = ($deck) ->
# Add title to deck if present
title = $deck.data("title")
unless title
$deck.attr "data-title",$deck.attr("id")
title = $deck.attr("id")
$deck_wrapper = $(_init_deck_wrapper($deck))
$deck.replaceWith($deck_wrapper)
$deck_wrapper.append $deck
# Hide the "Removed Cards" dropdown if it doesn't have any cards
$dropdown = $deck_wrapper.find(_css_variables.selectors.removed_dropdown)
$dropdown.hide() if $dropdown.find('ul').children().size() == 0
return true
_init_deck_wrapper = ($deck) ->
return """
<div class="#{_css_variables.classes.deck_container}">
<div class="deck-header">
<div class="wrapper">
<div class="#{_css_variables.classes.deck_title}">#{$deck.data("title") or ""}</div>
<div class="deck-controls">
#{_init_card_add_remove()}
#{_init_card_scroll($deck)}
</div>
</div>
</div>
"""
_init_card_add_remove = ()->
return """
<div class="btn-group #{_css_variables.classes.removed_dropdown}">
<span class="dropdown-toggle control add" data-toggle="dropdown"></span>
<ul class="dropdown-menu pull-right"></ul>
</div>
"""
_init_card_scroll = ($deck)->
return """
<div id="#{$deck.attr("id")}-nav" class="btn-group #{_css_variables.classes.card_jump_scroll}">
<span class="dropdown-toggle control jump-card" data-toggle="dropdown"></span>
<ul class="dropdown-menu pull-right"></ul>
</div>
"""
init = ->
__col_max = $deck.data 'col-max'
_init_deck_header($deck)
cards = $deck.children(_css_variables.selectors.card)
cards.each ->
$card = $(this)
if _should_remove_card_in_init($card, $deck)
$card.remove()
else
d =
id: __next_id++
row: parseInt $card.attr 'data-row'
col: parseInt $card.attr 'data-col'
row_span: parseInt $card.attr 'data-row-span'
col_span: parseInt $card.attr 'data-col-span'
__cards_by_id[d.id] = $card
__card_data_by_id[d.id] = d
_add_card($card, d)
$cheight = $(this).height()
$theight = $('.deckster-card-title',this).height() + 40
$('.deckster-card-title',this).css('margin-top',-$theight)
$(this).css('padding-top',$theight)
_apply_deck()
console.log "riw-max",$deck.attr("data-row-max")
cards.append "<div class='#{_css_variables.classes.controls}'></div>"
for callback in __event_callbacks[__events.inited] || []
break if callback($deck) == false
_create_jump_scroll_card $deck
_create_jump_scroll_deck 0xDEADBEEF
_adjust_adjacent_decks = ($deck) ->
###
deckId = $deck.attr("id")
specs = _window.__deck_mgr.lookup[deckId]
new_layout = {}
#copy decks up to deck being modified
for row in [1...specs.row_min]
new_layout[row] = _window.__deck_mgr.layout[row]
#add current deck
for row,cols of __deck
new_layout[specs.row_min-1+row] = {}
new_layout[specs.row_min-1+row][col] = deckId for col in [1..__col_max]
#add back buffer
newRow = __row_max+specs.row_min
new_layout[newRow] = {}
new_layout[newRow][i] = _css_variables.buffer for i in [1..__col_max]
# copy rest
# (note: our previous buffer, for this deck, will be copied over when iterating
# over the 'specs.row_max' row)
newRow += 1
prevId = -1
for row in [(specs.row_max+1).._window.__deck_mgr.row]
new_layout[newRow] = _window.__deck_mgr.layout[row]
# Update global deck placements
id = new_layout[newRow][1] #
if id != prevId and id != _css_variables.buffer
_window.__deck_mgr.lookup[id].row_min = newRow
_window.__deck_mgr.lookup[id].row_max = newRow
prevId = id
else
_window.__deck_mgr.lookup[prevId].row_max = newRow
newRow+=1
###
###
Update global variables.
-New overall max row (note: the for loop increments this value 1 extra time when exiting for-loop)
-New Layout
-Deck Max
###
###
_window.__deck_mgr.row = newRow-1
console.log("_window.__deck_mgr.row",_window.__deck_mgr.row)
_window.__deck_mgr.layout = new_layout # new layout
console.log("layout",_window.__deck_mgr.layout)
console.log("__row_max!",__row_max)
_window.__deck_mgr.lookup[deckId].row_max = specs.row_min+__row_max
###
#Update Page
$deck
.closest(_css_variables.selectors.deck_container)
.attr("data-row-max",__row_max+1)
return true
###
Adjust (if necessary) other decks when a particular deck is expanded/collapsed or its contents are moved around.
###
_on __events.card_collapsed, ($deck,$card)->
_adjust_adjacent_decks($deck)
_on __events.card_expanded, ($deck,$card)->
_adjust_adjacent_decks($deck)
_on __events.card_moved, ($deck,$card) ->
_adjust_adjacent_decks($deck)
_on __events.inited, ($deck)->
###
col_min = 1 # Should only be 1 as we will only be scrolling vertically
deckId = $deck.attr("id")
#How many decks "rows" are there currently?
if _window.__deck_mgr.row?
#Start at the next available row
row_min = _window.__deck_mgr.row+1
else
row_min = 1
#Max width for this deck
col_max = __col_max
#There's an extra row between decks to act as a buffer
row_max = row_min+__row_max
_window.__deck_mgr.layout = _window.__deck_mgr.layout || {}
for y in [row_min..row_max]
for x in [col_min..col_max]
unless _window.__deck_mgr.layout[y]
_window.__deck_mgr.layout[y] = {}
_window.__deck_mgr.layout[y][x] = if y == row_max then _css_variables.buffer else $deck.attr("id")
#Maximum number of rows
_window.__deck_mgr.row = row_max
_window.__deck_mgr.lookup = _window.__deck_mgr.lookup || {}
#Record results
_window.__deck_mgr.lookup[deckId] =
"row_min":row_min
"row_max":row_max
"col_max":col_max
"col_min":col_min
###
#Adding Height to Deck via CSS (add extra row for buffer)
$deck
.closest(_css_variables.selectors.deck_container)
.attr("data-row-max",__row_max+1)
#console.log("done init window layout",_window.__deck_mgr.layout)
return true
#Persist Deck
if options['persist'] && options['persist'] == true
_on __events.inited, ($deck) ->
$deck.closest(_css_variables.selectors.deck_container)
.find(_css_variables.selectors.deck_controls).append(_init_persistence())
$("#save").bind("click",_saveDeckRemotly)
$("#load").bind("click",_loadRemoteDeck)
_init_persistence = ()->
return """
<span>
<button id = "save">Save</button>
<button id = "load">Load</button>
</span>
"""
_saveDeckRemotly = ()->
persistance = _document.__deck_mgr.persistance
### Save DECK ###
deckId = $deck.attr("id")
$deckClone = $deck.clone(true)
$deckClone.find(_css_variables.selectors.controls).remove()
$deckClone.find(_css_variables.selectors.title).remove()
$deckClone.find(_css_variables.selectors.card_content+"[data-url]").html("")
### Save Removed Cards ###
$dropdown = $deck.closest(_css_variables.selectors.deck_container).find(_css_variables.selectors.removed_dropdown)
removedCardClones = null
$dropdown.find("a").each((index)->
$link = $(this)
#removedCardClones = removedCardClones || {}
temp = $link.attr("id").indexOf(_css_variables.classes.removed_card_button)+
_css_variables.classes.removed_card_button.length+1
cardId = parseInt $link.attr("id").substring(temp)
$card = __cards_by_id[cardId].clone().attr("data-is-removed","true")
#removedCardClones[cardId] = $card.clone()[0].outerHTML
$card.find(_css_variables.selectors.controls).remove()
$card.find(_css_variables.selectors.title).remove()
$card.find(_css_variables.selectors.card_content+"[data-url]").html("")
$deckClone.append($card)
)
deckClone = $deckClone[0].outerHTML
deckId = $deck.attr("id")
### Create REST call ###
if persistance?
url =
"url":persistance.url
"type": "POST" #if __is_saved then "PUT" else "POST"
"success":(data,status,response)->
console.log("successfully saved deck preferences")
#_localMgr("save":true)
__is_saved = true
"error":(response,status,exception)->
console.log("unsuccessfully saved deck preferences")
url.data = {}
url.data[deckId] = {}
url.data[deckId].layout = deckClone
if removedCardClones?
url.data[deckId].removedCards = JSON.stringify(removedCardClones)
console.log("URL UNSTRINGIFYED",url)
console.log("URL: "+JSON.stringify(url));
_ajax(url)
#else
### Only Persist Locally ###
#_localMgr("save":true)
_reset_deck = ()->
__next_id = 1
__deck = {}
__cards_by_id = {}
__card_data_by_id = {}
__col_max = 0
__row_max = 0
_add_callback = ($card)->
_loadRemoteDeck = ()->
#unless _localMgr("load":false)
persistance = _document.__deck_mgr.persistance
url =
"url":persistance.url
"type":"GET"
"success":(data,status,response)->
str = "No Deck Saved"
if(data._id == "undefined")
__is_saved = false
_reset_deck()
init()
else
str = "Loading Saved Deck"
deckId = $deck.attr("id")
layout = data[deckId].layout
### Add back saved deck ###
if $deck.closest(_css_variables.selectors.deck_container).length > 0
console.log("Replacing Deck Container")
$deck.closest(_css_variables.selectors.deck_container)
.replaceWith(layout)
else
console.log("Replacing Deck")
$deck.replaceWith(layout)
console.log(str)
_reset_deck()
### Set new deck handle ###
$deck = $("#"+deckId)
__is_saved = true
### INIT ###
init()
### Add back removed cards
if data[deckId].removedCards?
data[deckId].removedCards = JSON.parse(data[deckId].removedCards)
$dropdown = $deck.closest(_css_variables.selectors.deck_container).find(_css_variables.selectors.removed_dropdown);
###
idsToRemove = []
$.each($deck.find("[data-is-removed='true'] "+_css_variables.selectors.remove_handle),
(index)->
$(this).trigger('click')
idsToRemove
.push(parseInt $(this).closest(_css_variables.selectors.card).data("card-id"))
)
$.each(idsToRemove,(index)->
__cards_by_id[idsToRemove[index]].removeAttr("data-is-removed")
)
###
cardId = __next_id++
$card = $(cardHTML)
$card.attr("id",cardId)
__cards_by_id[cardId] = $card
__card_data_by_id[cardId] =
"id":cardId
"row":parseInt $card.attr("data-row")
"col":parseInt $card.attr("data-col")
"row_span": parseInt $card.attr("data-row-span")
"col_span":parseInt $card.attr("data-col-span")
$dropdown.find('ul')
.append(_get_removed_card_li_tag(cardId, _get_title($card)));
$dropdown.find('#' + _css_variables.classes.removed_card_button + '-' + cardId).click ->
temp = $(this).attr("id").indexOf(_css_variables.classes.removed_card_button)+
_css_variables.classes.removed_card_button.length+1
cardId = parseInt $(this).attr("id").substring(temp)
console.log("cardId::",cardId)
_move_to_open_position(cardId,$dropdown)
if ($dropdown.is(":hidden"))
$dropdown.show()
###
#_localMgr("save":true)
"error":(response,status,exception)->
str = "Error Loading Deck"
console.log(str)
init()
#_localMgr("save":true)
return _ajax(url)
_localMgr = (option)->
return false
if typeof(Storage)?
if option.load and localStorage.deck
console.log("Loading Locally Saved Deck")
console.log("DECK "+JSON.parse(localStorage.deck))
__deck = JSON.parse(localStorage.deck)
_apply_deck()
return true
else if option.save?
console.log("Saving Deck Locally")
localStorage.deck = JSON.stringify(__deck)
return true
return false
# Deckster Drag
if options['draggable'] && options['draggable'] == true
__$active_drag_card = undefined
__active_drag_card_drag_data = undefined
_on __events.inited, ($deck) ->
controls = "<a title='Drag' class='#{_css_variables.classes.drag_handle} control drag'></a>"
$deck.find(_css_variables.selectors.controls).append controls
_on __events.inited, ($deck) ->
_bind_drag_controls(this)
_create_box = ($card,clazz)->
$div = $("<div/>")
.addClass(clazz)
.addClass("deckster-card")
.attr("data-col",$card.attr("data-col"))
.attr("data-row",$card.attr("data-row"))
.attr("data-col-span",$card.attr("data-col-span"))
.attr("data-row-span",$card.attr("data-row-span"))
return $div
_bind_drag_controls = (deck) ->
$deck.find(_css_variables.selectors.drag_handle).on "mousedown", (e) ->
$drag_handle = $(this)
__$active_drag_card = $drag_handle.parents(_css_variables.selectors.card)
__$active_drag_card.addClass('draggable')
__$active_drag_card.css 'z-index', 1000
#Shadowbox
$deck.append(_create_box(__$active_drag_card,"shadowbox"))
__$active_drag_card.css 'z-index','1000'
__active_drag_card_drag_data =
height: __$active_drag_card.outerHeight()
width: __$active_drag_card.outerWidth()
pos_y: __$active_drag_card.offset().top + __$active_drag_card.outerHeight() - e.pageY
pos_x: __$active_drag_card.offset().left + __$active_drag_card.outerWidth() - e.pageX
__active_drag_card_drag_data['original_top'] = e.pageY + __active_drag_card_drag_data['pos_y'] - __active_drag_card_drag_data['height']
__active_drag_card_drag_data['original_left'] = e.pageX + __active_drag_card_drag_data['pos_x'] - __active_drag_card_drag_data['width']
e.preventDefault();
$deck.on 'mousemove', (e) ->
if __$active_drag_card?
new_top = e.pageY + __active_drag_card_drag_data['pos_y'] - __active_drag_card_drag_data['height']
new_left = e.pageX + __active_drag_card_drag_data['pos_x'] - __active_drag_card_drag_data['width']
original_left = __active_drag_card_drag_data['original_left']
original_top = __active_drag_card_drag_data['original_top']
$shadowbox = $(".shadowbox")
top = parseInt $shadowbox.attr("data-row")
left = parseInt $shadowbox.attr("data-col")
messages = []
if new_top - original_top < -200
__active_drag_card_drag_data['original_top'] = __active_drag_card_drag_data['original_top']-200
_move_card(__$active_drag_card,"up")
top -= 1
messages.push 'UP'
if new_top - original_top > 200
__active_drag_card_drag_data['original_top'] = __active_drag_card_drag_data['original_top']+200
_move_card(__$active_drag_card,"down")
top += 1
messages.push 'DOWN'
if new_left - original_left < -300
__active_drag_card_drag_data['original_left'] = __active_drag_card_drag_data['original_left']-300
_move_card(__$active_drag_card,"left")
left -= 1
messages.push 'LEFT'
if new_left - original_left > 300
__active_drag_card_drag_data['original_left'] = __active_drag_card_drag_data['original_left']+300
_move_card(__$active_drag_card,"right")
left += 1
messages.push 'RIGHT'
console.log messages.join(' ') if messages.length > 0
$shadowbox.attr("data-col",left)
$shadowbox.attr("data-row",top)
__$active_drag_card.offset { top: new_top, left: new_left }
$deck.on 'mouseup', (e) ->
if __$active_drag_card?
__$active_drag_card.removeClass('draggable')
__$active_drag_card.css 'top', ''
__$active_drag_card.css 'left', ''
__$active_drag_card.css 'z-index', ''
__$active_drag_card.css 'opacity','1'
$(".shadowbox").fadeOut("slow",()->
$(this).remove()
)
__$active_drag_card = undefined
__active_drag_card_drag_data = undefined
_move_card = ($card, direction) ->
id = parseInt $card.data('card-id')
d = __card_data_by_id[id]
console.log "data", d
switch direction
when 'left' then _force_card_to_position $card, d, { row: d.row, col: d.col - 1}
when 'right' then _force_card_to_position $card, d, { row: d.row, col: d.col + 1}
when 'up' then _force_card_to_position $card, d, { row: d.row - 1, col: d.col}
when 'down' then _force_card_to_position $card, d, { row: d.row + 1, col: d.col}
_apply_deck()
console.log("new Deck",__deck)
# Deckster Expand
if options['expandable'] && options['expandable'] == true
_on __events.inited, ($deck) ->
controls = """
<a title="Expand" class='#{_css_variables.classes.expand_handle} control expand'></a>
<a title="Collapse" class='#{_css_variables.classes.collapse_handle} control collapse' style='display:none;'></a>
"""
$deck.find(_css_variables.selectors.controls).each((index)->
$card = $(this).closest(_css_variables.selectors.card)
###Hide Expand Control if necessary ###
if (parseInt($card.data("col-expand")) > 0 or parseInt($card.data("row-expand")) > 0)
$(this).append controls
)
$deck.find(_css_variables.selectors.expand_handle).click ->
_expand_on_click(this)
$deck.find(_css_variables.selectors.collapse_handle).click ->
_collapse_on_click(this)
_expand_on_click = (element) ->
$expand_handle = $(element)
$card = $expand_handle.parents(_css_variables.selectors.card)
id = parseInt $card.attr 'data-card-id'
d = __card_data_by_id[id]
console.log ['Expand <<<', $card, d, { row: d.row, col: d.col }]
$card.attr 'data-original-col', d.col
$card.attr 'data-original-row-span', d.row_span
$card.attr 'data-original-col-span', d.col_span
if $card.data("col-expand")?
expandColTo = parseInt($card.data("col-expand"))
expandColTo = if expandColTo > __col_max then __col_max else expandColTo
expandColTo = if expandColTo? and expandColTo > 0 then expandColTo else d.col_span
expandRowTo = parseInt($card.data("row-expand")) if $card.data("row-expand")?
expandRowTo = if expandRowTo? and expandRowTo > 0 then expandRowTo else d.row_span
d['row_span'] = expandRowTo
d['col'] = if (expandColTo-1)+d.col <= __col_max then d.col else 1
d['col_span'] = expandColTo
if d.col_span == $card.data('original-col-span') and d.row_span == $card.data('original-row-span')
return;
console.log ['Expand >>>', $card, d, { row: d.row, col: d.col }]
_force_card_to_position $card, d, { row: d.row, col: d.col }
_apply_deck()
$expand_handle.hide()
$expand_handle.siblings(_css_variables.selectors.collapse_handle).show()
for callback in __event_callbacks[__events.card_expanded] || []
break if callback($deck,$card) == false
_collapse_on_click = (element) ->
$collapse_handle = $(element)
$card = $collapse_handle.parents(_css_variables.selectors.card)
id = parseInt $card.attr 'data-card-id'
d = __card_data_by_id[id]
d.col = parseInt $card.attr 'data-original-col'
d.row_span = parseInt $card.attr 'data-original-row-span'
d.col_span = parseInt $card.attr 'data-original-col-span'
$card.attr 'data-original-col', ''