-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Admin cart page revamp #1710
Admin cart page revamp #1710
Changes from all commits
f8fe5c6
fa75ba2
88fe43e
9563773
b75d78f
78bc407
4dab19d
2de9d05
6e82dc5
9ed8b34
12ea658
ee7d883
d98b617
e4b0af9
7c5fd3b
bb6093f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,98 @@ | ||
editing = (e) -> | ||
e.preventDefault() | ||
$(e.delegateTarget).addClass('editing') | ||
|
||
editingDone = (e) -> | ||
e.preventDefault() | ||
$(e.delegateTarget).removeClass('editing') | ||
|
||
onSaveLineItem = (e) -> | ||
e.preventDefault() | ||
line_item = $(this).closest('.line-item') | ||
line_item_id = line_item.data('line-item-id') | ||
quantity = parseInt(line_item.find('input.line_item_quantity').val()) | ||
adjustLineItem(line_item_id, quantity) | ||
editingDone(e) | ||
|
||
onDeleteLineItem = (e) -> | ||
e.preventDefault() | ||
return unless confirm(Spree.translations.are_you_sure_delete) | ||
line_item = $(this).closest('.line-item') | ||
line_item_id = line_item.data('line-item-id') | ||
deleteLineItem(line_item_id) | ||
editingDone(e) | ||
|
||
$(document).ready -> | ||
$('.line-item') | ||
.on('click', '.edit-line-item', editing) | ||
.on('click', '.cancel-line-item', editingDone) | ||
.on('click', '.save-line-item', onSaveLineItem) | ||
.on('click', '.delete-line-item', onDeleteLineItem) | ||
|
||
lineItemURL = (id) -> | ||
"#{Spree.routes.line_items_api(order_number)}/#{id}.json" | ||
|
||
adjustLineItem = (line_item_id, quantity) -> | ||
url = lineItemURL(line_item_id) | ||
Spree.ajax( | ||
type: "PUT", | ||
url: url, | ||
data: | ||
line_item: | ||
quantity: quantity | ||
).done (msg) -> | ||
window.Spree.advanceOrder() | ||
|
||
deleteLineItem = (line_item_id) -> | ||
url = lineItemURL(line_item_id) | ||
Spree.ajax( | ||
type: "DELETE" | ||
url: url | ||
).done (msg) -> | ||
$('#line-item-' + line_item_id).remove() | ||
if $('.line-items tr.line-item').length == 0 | ||
$('.line-items').remove() | ||
window.Spree.advanceOrder() | ||
|
||
Spree.CartLineItemView = Backbone.View.extend | ||
tagName: 'tr' | ||
className: 'line-item' | ||
|
||
initialize: (options) -> | ||
@editing = options.editing || @model.isNew() | ||
@noCancel = options.noCancel | ||
|
||
events: | ||
'click .edit-line-item': 'onEdit' | ||
'click .cancel-line-item': 'onCancel' | ||
'click .save-line-item': 'onSave' | ||
'click .delete-line-item': 'onDelete' | ||
'change .js-select-variant': 'onChangeVariant' | ||
|
||
onEdit: (e) -> | ||
e.preventDefault() | ||
@editing = true | ||
@render() | ||
|
||
onCancel: (e) -> | ||
e.preventDefault() | ||
@trigger('cancel') | ||
if @model.isNew() | ||
@remove() | ||
else | ||
@editing = false | ||
@render() | ||
|
||
validate: () -> | ||
@$('[name=quantity]').toggleClass 'error', !@$('[name=quantity]').val() | ||
@$('.select2-container').toggleClass 'error', !@$('[name=variant_id]').val() | ||
|
||
!@$('.select2-container').hasClass('error') && !@$('[name=quantity]').hasClass('error') | ||
|
||
onSave: (e) -> | ||
e.preventDefault() | ||
return unless @validate() | ||
attrs = { | ||
quantity: parseInt(@$('input.line_item_quantity').val()) | ||
} | ||
if @model.isNew() | ||
attrs['variant_id'] = @$("[name=variant_id]").val() | ||
@model.save attrs, | ||
patch: true, | ||
success: => | ||
window.Spree.advanceOrder() | ||
@editing = false | ||
@render() | ||
|
||
onDelete: (e) -> | ||
e.preventDefault() | ||
return unless confirm(Spree.translations.are_you_sure_delete) | ||
@remove() | ||
@model.destroy | ||
success: => | ||
window.Spree.advanceOrder() | ||
|
||
render: -> | ||
line_item = @model.attributes | ||
image = line_item.variant && line_item.variant.images[0] | ||
html = HandlebarsTemplates['orders/line_item']( | ||
line_item: line_item, | ||
image: image, | ||
editing: @editing, | ||
isNew: @model.isNew(), | ||
noCancel: @noCancel | ||
) | ||
el = @$el.html(html) | ||
@$("[name=variant_id]").variantAutocomplete({ in_stock_only: true }) | ||
|
||
$ -> | ||
if $("table.line-items").length | ||
url = Spree.routes.orders_api + "/" + order_number | ||
lineItemModel = Backbone.Model.extend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we put this model into its own file and use the Spree.Model namespace discussed in #1715? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't be done here. At this point the model needs to be defined where we have access to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okidoki |
||
urlRoot: Spree.routes.line_items_api(order_number) | ||
|
||
add_button = $('.js-add-line-item') | ||
add_button.click -> | ||
add_button.prop("disabled", true) | ||
view = new Spree.CartLineItemView(model: new lineItemModel()) | ||
view.render() | ||
view.on('cancel', (event) -> add_button.prop("disabled", false)) | ||
$("table.line-items > tbody").append(view.el) | ||
|
||
Spree.ajax(url: url).done (result) -> | ||
for line_item in result.line_items | ||
model = new lineItemModel(line_item) | ||
view = new Spree.CartLineItemView(model: model) | ||
view.render() | ||
$("table.line-items > tbody").append(view.el) | ||
|
||
add_button.prop("disabled", !result.line_items.length) | ||
if !result.line_items.length | ||
view = new Spree.CartLineItemView(model: new lineItemModel(), noCancel: true) | ||
view.render() | ||
$("table.line-items > tbody").append(view.el) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{{#if isNew}} | ||
<td class="line-item-select-variant" colspan="2"> | ||
<input type="hidden" name="variant_id" class="js-select-variant select-variant fullwidth"> | ||
</td> | ||
{{else}} | ||
<td class="line-item-image"> | ||
<img src="{{ image.mini_url }}"> | ||
</td> | ||
<td class="line-item-name"> | ||
{{ line_item.variant.name }}<br> | ||
{{#if line_item.variant.options_text }} | ||
{{ line_item.variant.options_text }}<br/> | ||
{{/if}} | ||
<strong>{{ t "sku" }}:</strong> {{ line_item.variant.sku }} | ||
</td> | ||
{{/if}} | ||
<td class="line-item-price align-center"> | ||
{{ line_item.single_display_amount }} | ||
</td> | ||
{{#if editing}} | ||
<td class="line-item-qty-edit"> | ||
<input type="number" name="quantity" value="{{ line_item.quantity }}" min="0" class="line_item_quantity" size="5"> | ||
</td> | ||
{{else}} | ||
<td class="line-item-qty-show align-center"> | ||
{{ line_item.quantity }} | ||
</td> | ||
{{/if}} | ||
<td class="line-item-total align-center"> | ||
{{ line_item.display_amount }} | ||
</td> | ||
<td class="cart-line-item-delete actions" data-hook="cart_line_item_delete"> | ||
{{#if editing}} | ||
<a class="save-line-item fa fa-ok no-text with-tip" href="#" title="{{ t "actions.save" }}"></a> | ||
{{#unless noCancel}} | ||
<a class="cancel-line-item fa fa-cancel no-text with-tip" href="#" title="{{ t "actions.cancel" }}"></a> | ||
{{/unless}} | ||
{{else}} | ||
<a class="edit-line-item fa fa-edit no-text with-tip" href="#" title="{{ t "actions.edit" }}"></a> | ||
<a class="delete-line-item fa fa-trash no-text with-tip" href="#" title="{{ t "actions.delete" }}"></a> | ||
{{/if}} | ||
</td> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,24 @@ | ||
<% if order.line_items.exists? %> | ||
<table class="line-items index" data-hook="line-items"> | ||
<colgroup> | ||
<col style="width: 10%;" /> | ||
<col style="width: 20%;" /> | ||
<col style="width: 20%;" /> | ||
<col style="width: 15%;" /> | ||
</colgroup> | ||
<table class="line-items index" data-hook="line-items"> | ||
<colgroup> | ||
<col style="width: 10%;" /> | ||
<col style="width: 45%;" /> | ||
<col style="width: 10%;" /> | ||
<col style="width: 10%;" /> | ||
<col style="width: 10%;" /> | ||
<col style="width: 15%;" /> | ||
</colgroup> | ||
|
||
<thead> | ||
<tr> | ||
<th colspan="2"><%= Spree::Product.human_attribute_name(:name) %></th> | ||
<th><%= Spree::LineItem.human_attribute_name(:price) %></th> | ||
<th><%= Spree::LineItem.human_attribute_name(:quantity) %></th> | ||
<th><%= Spree::LineItem.human_attribute_name(:total) %></th> | ||
<th class="orders-actions actions" data-hook="admin_order_form_line_items_header_actions"> </th> | ||
</tr> | ||
</thead> | ||
<thead> | ||
<tr> | ||
<th colspan="2"><%= Spree::Product.human_attribute_name(:name) %></th> | ||
<th><%= Spree::LineItem.human_attribute_name(:price) %></th> | ||
<th><%= Spree::LineItem.human_attribute_name(:quantity) %></th> | ||
<th><%= Spree::LineItem.human_attribute_name(:total) %></th> | ||
<th class="orders-actions actions" data-hook="admin_order_form_line_items_header_actions"> </th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% order.line_items.each do |item| %> | ||
<%= content_tag :tr, class: 'line-item', id: "line-item-#{item.id}", data: { line_item_id: item.id } do %> | ||
<td class="line-item-image"><%= image_tag item.variant.display_image.attachment(:mini) %></td> | ||
<td class="line-item-name"> | ||
<%= item.variant.product.name %><br><%= "(" + variant_options(item.variant) + ")" unless item.variant.option_values.empty? %> | ||
<% if item.variant.sku.present? %> | ||
<strong><%= Spree::Variant.human_attribute_name(:sku) %>:</strong> <%= item.variant.sku %> | ||
<% end %> | ||
</td> | ||
<td class="line-item-price align-center"><%= item.single_money.to_html %></td> | ||
<td class="line-item-qty-show align-center"> | ||
<%= item.quantity %> | ||
</td> | ||
<td class="line-item-qty-edit"> | ||
<%= number_field_tag :quantity, item.quantity, min: 0, class: "line_item_quantity", size: 5 %> | ||
</td> | ||
<td class="line-item-total align-center"><%= line_item_shipment_price(item, item.quantity) %></td> | ||
<td class="cart-line-item-delete actions" data-hook="cart_line_item_delete"> | ||
<% if can? :update, item %> | ||
<%= link_to '', '#', class: 'save-line-item fa fa-ok no-text with-tip', title: Spree.t('actions.save') %> | ||
<%= link_to '', '#', class: 'cancel-line-item fa fa-cancel no-text with-tip', title: Spree.t('actions.cancel') %> | ||
<%= link_to '', '#', class: 'edit-line-item fa fa-edit no-text with-tip', title: Spree.t('actions.edit') %> | ||
<%= link_to '', '#', class: 'delete-line-item fa fa-trash no-text with-tip', title: Spree.t('actions.delete') %> | ||
<% end %> | ||
</td> | ||
<% end %> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<% end %> | ||
<tbody> | ||
<%# Rendered by JS %> | ||
</tbody> | ||
</table> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can also introduce a Spree.Views namespace for views as we discussed for models in #1715?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather address this in the #1715, which is a follow up to this. This PR was intended only to be the UI change, with #1715 being a more complete switch to backbone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.