|
| 1 | +define [ |
| 2 | + 'underscore' |
| 3 | + 'compiled/views/DialogFormView' |
| 4 | + 'compiled/views/MoveDialogSelect' |
| 5 | + 'jst/MoveDialog' |
| 6 | + 'jst/EmptyDialogFormWrapper' |
| 7 | +], (_, DialogFormView, MoveDialogSelect, template, wrapper) -> |
| 8 | + |
| 9 | + class MoveDialogView extends DialogFormView |
| 10 | + setViewProperties: false |
| 11 | + |
| 12 | + defaults: |
| 13 | + width: 450 |
| 14 | + height: 340 |
| 15 | + |
| 16 | + # {bool} |
| 17 | + @optionProperty 'nested' |
| 18 | + |
| 19 | + # {Backbone.Collection} |
| 20 | + # Backbone Collection whose models contain a reference |
| 21 | + # to another Backbone Collection - referenced by @childKey |
| 22 | + # required if @nested |
| 23 | + @optionProperty 'parentCollection' |
| 24 | + |
| 25 | + # {string} |
| 26 | + # text used to describe the select |
| 27 | + # MUST BE I18N STRING |
| 28 | + # required if @nested |
| 29 | + @optionProperty 'parentLabelText' |
| 30 | + |
| 31 | + # {string} |
| 32 | + # The name of the attribute on @model |
| 33 | + # that stores the relation to @parentCollection |
| 34 | + @optionProperty 'parentKey' |
| 35 | + |
| 36 | + # {string} |
| 37 | + # key to use to get the child collection from a model |
| 38 | + # in the parent collection |
| 39 | + # so if assignments are stored on a model and you |
| 40 | + # access the collection like so: |
| 41 | + # `model.get('assignments')` |
| 42 | + # then the childKey would be 'assignments' |
| 43 | + # required if @nested |
| 44 | + @optionProperty 'childKey' |
| 45 | + |
| 46 | + # {string or function} |
| 47 | + # url to post to when saving the form |
| 48 | + # |
| 49 | + # if a function, will be called with this |
| 50 | + # view as context |
| 51 | + @optionProperty 'saveURL' |
| 52 | + |
| 53 | + events: _.extend({}, @::events, |
| 54 | + 'click .dialog_closer': 'close' |
| 55 | + 'change .move_select_parent_collection': 'updateListView' |
| 56 | + ) |
| 57 | + |
| 58 | + els: |
| 59 | + '.child_container': '$childContainer' |
| 60 | + '.form-dialog-content': '$content' |
| 61 | + |
| 62 | + template: template |
| 63 | + wrapperTemplate: wrapper |
| 64 | + |
| 65 | + openAgain: -> |
| 66 | + super |
| 67 | + @initChildViews() |
| 68 | + @dialog.option "close", @cleanup |
| 69 | + |
| 70 | + # creates @listView and @parentListView (if @nested) |
| 71 | + initChildViews: -> |
| 72 | + @listView = @parentListView = null |
| 73 | + if @nested and @parentCollection |
| 74 | + @listView = new MoveDialogSelect |
| 75 | + model: @model |
| 76 | + excludeModel: true |
| 77 | + lastList: true |
| 78 | + @parentListView = new MoveDialogSelect |
| 79 | + collection: @parentCollection |
| 80 | + model: @model |
| 81 | + labelText: @parentLabelText |
| 82 | + else |
| 83 | + @listView = new MoveDialogSelect |
| 84 | + model: @model |
| 85 | + excludeModel: true |
| 86 | + lastList: true |
| 87 | + |
| 88 | + @attachChildViews() |
| 89 | + |
| 90 | + |
| 91 | + # attaches child views to @$childContainer |
| 92 | + attachChildViews: -> |
| 93 | + container = @$childContainer.detach() |
| 94 | + if @parentListView |
| 95 | + container.append(@parentListView.render().el) |
| 96 | + container.append(@listView.render().el) |
| 97 | + @$content.append(container) |
| 98 | + |
| 99 | + cleanup: => |
| 100 | + @parentListView?.remove() |
| 101 | + @listView?.remove() |
| 102 | + @parentListView = @listView = null |
| 103 | + @dialog.option "close", null |
| 104 | + |
| 105 | + #lookup new collection, and set it on |
| 106 | + #the nested view |
| 107 | + updateListView: (e)-> |
| 108 | + return unless @nested |
| 109 | + groupId = $(e.currentTarget).val() |
| 110 | + group = @parentCollection.get(groupId) |
| 111 | + children = group.get(@childKey) |
| 112 | + |
| 113 | + @listView.setCollection(children) |
| 114 | + |
| 115 | + toJSON: -> |
| 116 | + data = @model.toView?() or super |
| 117 | + |
| 118 | + # should return an array of ids |
| 119 | + # in the order they should save |
| 120 | + getFormData: -> |
| 121 | + $select = @listView.$('select') |
| 122 | + selected = $select.val() |
| 123 | + vals = [] |
| 124 | + _.each $select.find('option'), (ele, i) -> |
| 125 | + {value} = ele |
| 126 | + vals.push value unless value == 'last' |
| 127 | + |
| 128 | + if selected == 'last' |
| 129 | + # just push model onto the end |
| 130 | + vals.push @model.id |
| 131 | + else |
| 132 | + # or find the index to insert it at and splice it in |
| 133 | + vals.splice _.indexOf(vals, selected), 0, @model.id |
| 134 | + vals |
| 135 | + |
| 136 | + |
| 137 | + # will always have data as we're |
| 138 | + # overriding getFormData |
| 139 | + # |
| 140 | + # getFromData should return a list |
| 141 | + # of ids |
| 142 | + saveFormData: (data) -> |
| 143 | + url = if typeof @saveURL is 'function' |
| 144 | + @saveURL.call @ |
| 145 | + else |
| 146 | + @saveURL |
| 147 | + $.post url, order: data.join ',' |
| 148 | + |
| 149 | + |
| 150 | + onSaveSuccess: (data) => |
| 151 | + # assume collID is an int |
| 152 | + collID = parseInt @parentListView?.value(), 10 |
| 153 | + newCollection = @parentCollection?.get(collID).get(@childKey) |
| 154 | + |
| 155 | + # there is a currentCollection, but it doesn't match the model's collection |
| 156 | + if newCollection and newCollection != @model.collection |
| 157 | + #we need to remove the model from the previous collection |
| 158 | + #and add it to to the new one |
| 159 | + @model.collection.remove @model |
| 160 | + newCollection.add @model |
| 161 | + # also update the relationship to the collection |
| 162 | + # if we know how |
| 163 | + if @parentKey |
| 164 | + @model.set @parentKey, collID |
| 165 | + |
| 166 | + positions = [1..newCollection.length] |
| 167 | + else |
| 168 | + newCollection = @model.collection |
| 169 | + #unfortunate thing we have to do for AssignmentGroups, |
| 170 | + #not sure about others... |
| 171 | + positions = newCollection.pluck 'position' |
| 172 | + |
| 173 | + #update all of the position attributes |
| 174 | + _.each data.order, (id, index) -> |
| 175 | + newCollection.get(id)?.set 'position', positions[index] |
| 176 | + |
| 177 | + newCollection.sort() |
| 178 | + # finally, call reset to trigger a re-render |
| 179 | + newCollection.reset newCollection.models |
| 180 | + |
| 181 | + # close the dialog |
| 182 | + super |
0 commit comments