From 1553a0a4d1f04b5c67b6f5175868adb631934578 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 11:55:21 -0800 Subject: [PATCH 01/13] Convert editable_table_row from coffee to JS --- .../views/tables/editable_table_row.js | 61 +++++++++++++++++++ .../views/tables/editable_table_row.js.coffee | 45 -------------- 2 files changed, 61 insertions(+), 45 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js delete mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js b/backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js new file mode 100644 index 00000000000..d5df23cf09f --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js @@ -0,0 +1,61 @@ +Spree.Views.Tables.EditableTableRow = Backbone.View.extend({ + events: { + "select2-open": "onEdit", + "focus input": "onEdit", + "click [data-action=save]": "onSave", + "click [data-action=cancel]": "onCancel", + 'keyup input': 'onKeypress' + }, + + onEdit: function(e) { + if (this.$el.hasClass('editing')) { + return; + } + this.$el.addClass('editing'); + this.$el.find('input, select').each(function() { + var $input = $(this); + $input.data('original-value', $input.val()); + }); + }, + + onCancel: function(e) { + e.preventDefault(); + this.$el.removeClass("editing"); + this.$el.find('input, select').each(function() { + var $input = $(this); + var originalValue = $input.data('original-value'); + $input.val(originalValue).change(); + }); + }, + + onSave: function(e) { + e.preventDefault(); + var view = this; + Spree.ajax(this.$el.find('.actions [data-action=save]').attr('href'), { + data: this.$el.find('select, input').serialize(), + dataType: 'json', + method: 'put', + success: function(response) { + view.$el.removeClass("editing"); + }, + error: function(response) { + show_flash('error', response.responseJSON.error); + } + }); + }, + + ENTER_KEY: 13, + ESC_KEY: 27, + + onKeypress: function(e) { + var key = e.keyCode || e.which; + switch (key) { + case this.ENTER_KEY: + this.onSave(e); + break; + case this.ESC_KEY: + this.onCancel(e); + break; + } + } +}); diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js.coffee b/backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js.coffee deleted file mode 100644 index bda612739d6..00000000000 --- a/backend/app/assets/javascripts/spree/backend/views/tables/editable_table_row.js.coffee +++ /dev/null @@ -1,45 +0,0 @@ -Spree.Views.Tables.EditableTableRow = Backbone.View.extend - events: - "select2-open": "onEdit" - "focus input": "onEdit" - "click [data-action=save]": "onSave" - "click [data-action=cancel]": "onCancel" - 'keyup input': 'onKeypress' - - onEdit: (e) -> - return if @$el.hasClass('editing') - @$el.addClass('editing') - - @$el.find('input, select').each -> - $input = $(this) - $input.data 'original-value', $input.val() - - onCancel: (e) -> - e.preventDefault() - @$el.removeClass("editing") - - @$el.find('input, select').each -> - $input = $(this) - originalValue = $input.data('original-value') - $input.val(originalValue).change() - - onSave: (e) -> - e.preventDefault() - - Spree.ajax @$el.find('.actions [data-action=save]').attr('href'), - data: @$el.find('select, input').serialize() - dataType: 'json' - method: 'put' - success: (response) => - @$el.removeClass("editing") - error: (response) => - show_flash 'error', response.responseJSON.error - - ENTER_KEY: 13 - ESC_KEY: 27 - - onKeypress: (e) -> - key = e.keyCode || e.which - switch key - when @ENTER_KEY then @onSave(e) - when @ESC_KEY then @onCancel(e) From c20d3738732cc4cbdb6e63f42099b8095b5101f8 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 11:57:49 -0800 Subject: [PATCH 02/13] Convert style_guide from coffee to plain JS --- .../app/assets/javascripts/spree/backend/style_guide.coffee | 2 -- backend/app/assets/javascripts/spree/backend/style_guide.js | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 backend/app/assets/javascripts/spree/backend/style_guide.coffee create mode 100644 backend/app/assets/javascripts/spree/backend/style_guide.js diff --git a/backend/app/assets/javascripts/spree/backend/style_guide.coffee b/backend/app/assets/javascripts/spree/backend/style_guide.coffee deleted file mode 100644 index 7f674663829..00000000000 --- a/backend/app/assets/javascripts/spree/backend/style_guide.coffee +++ /dev/null @@ -1,2 +0,0 @@ -Spree.ready -> - $(".style-guide-nav, .style-guide-sidebar").stick_in_parent() diff --git a/backend/app/assets/javascripts/spree/backend/style_guide.js b/backend/app/assets/javascripts/spree/backend/style_guide.js new file mode 100644 index 00000000000..7c12f992603 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/style_guide.js @@ -0,0 +1,3 @@ +Spree.ready(function() { + $(".style-guide-nav, .style-guide-sidebar").stick_in_parent(); +}); From 5826bf46ee9c3972d12f8b8e5c4b87cc3b4ac083 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 11:59:51 -0800 Subject: [PATCH 03/13] Convert editable_table to JS from coffeescript --- .../javascripts/spree/backend/components/editable_table.js | 5 +++++ .../spree/backend/components/editable_table.js.coffee | 3 --- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/components/editable_table.js delete mode 100644 backend/app/assets/javascripts/spree/backend/components/editable_table.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/components/editable_table.js b/backend/app/assets/javascripts/spree/backend/components/editable_table.js new file mode 100644 index 00000000000..f81f48284ac --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/components/editable_table.js @@ -0,0 +1,5 @@ +Spree.ready(function() { + $('.inline-editable-table tr').each(function() { + Spree.Views.Tables.EditableTable.add($(this)); + }); +}); diff --git a/backend/app/assets/javascripts/spree/backend/components/editable_table.js.coffee b/backend/app/assets/javascripts/spree/backend/components/editable_table.js.coffee deleted file mode 100644 index 87761482328..00000000000 --- a/backend/app/assets/javascripts/spree/backend/components/editable_table.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -Spree.ready -> - $('.inline-editable-table tr').each -> - Spree.Views.Tables.EditableTable.add $(this) From 063a440cfe87e1fcb7ce4423cce55f05a7180721 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:02:25 -0800 Subject: [PATCH 04/13] Convert payments/edit to plain JS from coffee --- .../javascripts/spree/backend/payments/edit.js | 16 ++++++++++++++++ .../spree/backend/payments/edit.js.coffee | 7 ------- 2 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/payments/edit.js delete mode 100644 backend/app/assets/javascripts/spree/backend/payments/edit.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/payments/edit.js b/backend/app/assets/javascripts/spree/backend/payments/edit.js new file mode 100644 index 00000000000..f7de95fa4f9 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/payments/edit.js @@ -0,0 +1,16 @@ +Spree.ready(function() { + var order_id = $('#payments').data('order-id'); + + $('tr.payment').each(function() { + var payment_id = $(this).data('payment-id'); + var model = new Spree.Models.Payment({ + id: payment_id, + order_id: order_id + }); + + new Spree.Views.Payment.PaymentRow({ + el: this, + model: model + }); + }); +}); diff --git a/backend/app/assets/javascripts/spree/backend/payments/edit.js.coffee b/backend/app/assets/javascripts/spree/backend/payments/edit.js.coffee deleted file mode 100644 index f773204dae6..00000000000 --- a/backend/app/assets/javascripts/spree/backend/payments/edit.js.coffee +++ /dev/null @@ -1,7 +0,0 @@ -Spree.ready -> - order_id = $('#payments').data('order-id') - - $('tr.payment').each -> - payment_id = $(@).data('payment-id') - model = new Spree.Models.Payment({id: payment_id, order_id: order_id}) - new Spree.Views.Payment.PaymentRow({el: @, model: model}) From 1eed213e52849b5e8152bb6e96ad051d638974ee Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:04:06 -0800 Subject: [PATCH 05/13] Convert progress.coffee to plain JS --- .../app/assets/javascripts/spree/backend/progress.coffee | 7 ------- backend/app/assets/javascripts/spree/backend/progress.js | 9 +++++++++ 2 files changed, 9 insertions(+), 7 deletions(-) delete mode 100644 backend/app/assets/javascripts/spree/backend/progress.coffee create mode 100644 backend/app/assets/javascripts/spree/backend/progress.js diff --git a/backend/app/assets/javascripts/spree/backend/progress.coffee b/backend/app/assets/javascripts/spree/backend/progress.coffee deleted file mode 100644 index ac037972ca5..00000000000 --- a/backend/app/assets/javascripts/spree/backend/progress.coffee +++ /dev/null @@ -1,7 +0,0 @@ -Spree.ready -> - $(document).ajaxStart -> - $("#progress").show() - - $(document).ajaxStop -> - $("#progress").hide() - diff --git a/backend/app/assets/javascripts/spree/backend/progress.js b/backend/app/assets/javascripts/spree/backend/progress.js new file mode 100644 index 00000000000..e142e9b5a19 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/progress.js @@ -0,0 +1,9 @@ +Spree.ready(function() { + $(document).ajaxStart(function() { + $("#progress").show(); + }); + + $(document).ajaxStop(function() { + $("#progress").hide(); + }); +}); From 83ac0d173dd36a7ce97886b0af6eb5f020ef1292 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:05:28 -0800 Subject: [PATCH 06/13] Convert navigation.coffee to JS --- .../javascripts/spree/backend/navigation.coffee | 10 ---------- .../assets/javascripts/spree/backend/navigation.js | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 10 deletions(-) delete mode 100644 backend/app/assets/javascripts/spree/backend/navigation.coffee create mode 100644 backend/app/assets/javascripts/spree/backend/navigation.js diff --git a/backend/app/assets/javascripts/spree/backend/navigation.coffee b/backend/app/assets/javascripts/spree/backend/navigation.coffee deleted file mode 100644 index 3bc6181cbd1..00000000000 --- a/backend/app/assets/javascripts/spree/backend/navigation.coffee +++ /dev/null @@ -1,10 +0,0 @@ -navHeight = -> - $('.admin-nav-header').outerHeight() + $('.admin-nav-menu').outerHeight() + $('.admin-nav-footer').outerHeight() - -checkSideBarFit = -> - $('.admin-nav').toggleClass('fits', navHeight() < $(window).height()) - -Spree.ready -> - $(".admin-nav-sticky, .admin-nav").stick_in_parent() - checkSideBarFit() - $(window).on('resize', checkSideBarFit) diff --git a/backend/app/assets/javascripts/spree/backend/navigation.js b/backend/app/assets/javascripts/spree/backend/navigation.js new file mode 100644 index 00000000000..5662bd8f69d --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/navigation.js @@ -0,0 +1,14 @@ +Spree.ready(function() { + var navHeight = function() { + return $('.admin-nav-header').outerHeight() + $('.admin-nav-menu').outerHeight() + $('.admin-nav-footer').outerHeight(); + }; + + var checkSideBarFit = function() { + $('.admin-nav').toggleClass('fits', navHeight() < $(window).height()); + }; + + $(".admin-nav-sticky, .admin-nav").stick_in_parent(); + + checkSideBarFit(); + $(window).on('resize', checkSideBarFit); +}); From c6c740c15e0032e61ef5724195cb8fd7d74729dc Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:06:26 -0800 Subject: [PATCH 07/13] Convert adjustments.js.coffee to plain JS --- .../javascripts/spree/backend/adjustments.js | 26 +++++++++++++++++++ .../spree/backend/adjustments.js.coffee | 17 ------------ 2 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/adjustments.js delete mode 100644 backend/app/assets/javascripts/spree/backend/adjustments.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/adjustments.js b/backend/app/assets/javascripts/spree/backend/adjustments.js new file mode 100644 index 00000000000..6716b2a5f3d --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/adjustments.js @@ -0,0 +1,26 @@ +Spree.ready(function() { + $('[data-hook=adjustments_new_coupon_code] #add_coupon_code').click(function() { + if ($("#coupon_code").val().length === 0) { + return; + } + + Spree.ajax({ + type: 'PUT', + url: Spree.routes.apply_coupon_code(window.order_number), + data: { + coupon_code: $("#coupon_code").val(), + token: Spree.api_key + }, + success: function() { + window.location.reload(); + }, + error: function(msg) { + if (msg.responseJSON["error"]) { + show_flash('error', msg.responseJSON["error"]); + } else { + show_flash('error', "There was a problem adding this coupon code."); + } + } + }); + }); +}); diff --git a/backend/app/assets/javascripts/spree/backend/adjustments.js.coffee b/backend/app/assets/javascripts/spree/backend/adjustments.js.coffee deleted file mode 100644 index 4fdd2f95b6c..00000000000 --- a/backend/app/assets/javascripts/spree/backend/adjustments.js.coffee +++ /dev/null @@ -1,17 +0,0 @@ -Spree.ready( -> - $('[data-hook=adjustments_new_coupon_code] #add_coupon_code').click -> - return if $("#coupon_code").val().length == 0 - Spree.ajax - type: 'PUT' - url: Spree.routes.apply_coupon_code(order_number) - data: - coupon_code: $("#coupon_code").val() - token: Spree.api_key - success: -> - window.location.reload() - error: (msg) -> - if msg.responseJSON["error"] - show_flash 'error', msg.responseJSON["error"] - else - show_flash 'error', "There was a problem adding this coupon code." -) From d6f0169d7544ea34e0a74e844fbffc8e7d55f0e9 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:07:42 -0800 Subject: [PATCH 08/13] Convert flash.coffee to plain JS --- .../javascripts/spree/backend/flash.coffee | 17 ---------------- .../assets/javascripts/spree/backend/flash.js | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 17 deletions(-) delete mode 100644 backend/app/assets/javascripts/spree/backend/flash.coffee create mode 100644 backend/app/assets/javascripts/spree/backend/flash.js diff --git a/backend/app/assets/javascripts/spree/backend/flash.coffee b/backend/app/assets/javascripts/spree/backend/flash.coffee deleted file mode 100644 index 6a62d751d79..00000000000 --- a/backend/app/assets/javascripts/spree/backend/flash.coffee +++ /dev/null @@ -1,17 +0,0 @@ -showTime = 5000 -fadeOutTime = 500 - -Spree.ready -> - # Make flash messages dissapear - # We only want to target the flash messages which are initially on the page. - # Otherwise we risk hiding messages added by show_flash - $initialFlash = $(".flash") - setTimeout (-> - $initialFlash.fadeOut(fadeOutTime) - ), showTime - -window.show_flash = (type, message) -> - $flashWrapper = $(".js-flash-wrapper") - flash_div = $("
") - $flashWrapper.prepend(flash_div) - flash_div.html(message).show().delay(showTime).fadeOut(fadeOutTime) diff --git a/backend/app/assets/javascripts/spree/backend/flash.js b/backend/app/assets/javascripts/spree/backend/flash.js new file mode 100644 index 00000000000..c911fc0f0aa --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/flash.js @@ -0,0 +1,20 @@ +(function() { + var showTime = 5000; + + var fadeOutTime = 500; + + Spree.ready(function() { + var $initialFlash; + $initialFlash = $(".flash"); + setTimeout((function() { + $initialFlash.fadeOut(fadeOutTime); + }), showTime); + }); + + window.show_flash = function(type, message) { + var $flashWrapper = $(".js-flash-wrapper"); + var flash_div = $("
"); + $flashWrapper.prepend(flash_div); + flash_div.html(message).show().delay(showTime).fadeOut(fadeOutTime); + }; +})(); From 9a8b3d562e85862399d31c49c411febffea72642 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:09:20 -0800 Subject: [PATCH 09/13] Convert gateway.js.coffee to plain JS --- .../javascripts/spree/backend/gateway.js | 22 +++++++++++++++++++ .../spree/backend/gateway.js.coffee | 21 ------------------ 2 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/gateway.js delete mode 100644 backend/app/assets/javascripts/spree/backend/gateway.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/gateway.js b/backend/app/assets/javascripts/spree/backend/gateway.js new file mode 100644 index 00000000000..532ce36004d --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/gateway.js @@ -0,0 +1,22 @@ +Spree.ready(function() { + var $gateway_type = $('select.js-gateway-type'); + var $preference_source = $('select.js-preference-source'); + var original_gtwy_type = $gateway_type.val(); + var original_preference_source = $preference_source.val(); + + var render = function() { + var gateway_type = $gateway_type.val(); + var preference_source = $preference_source.val(); + $('.js-preference-source-wrapper').toggle(gateway_type === original_gtwy_type); + if (gateway_type === original_gtwy_type && preference_source === original_preference_source) { + $('.js-gateway-settings').show(); + $('.js-gateway-settings-warning').hide(); + } else { + $('.js-gateway-settings').hide(); + $('.js-gateway-settings-warning').show(); + } + }; + $gateway_type.change(render); + $preference_source.change(render); + render(); +}); diff --git a/backend/app/assets/javascripts/spree/backend/gateway.js.coffee b/backend/app/assets/javascripts/spree/backend/gateway.js.coffee deleted file mode 100644 index 4003e662a64..00000000000 --- a/backend/app/assets/javascripts/spree/backend/gateway.js.coffee +++ /dev/null @@ -1,21 +0,0 @@ -Spree.ready -> - $gateway_type = $('select.js-gateway-type') - $preference_source = $('select.js-preference-source') - - original_gtwy_type = $gateway_type.val() - original_preference_source = $preference_source.val() - render = -> - gateway_type = $gateway_type.val() - preference_source = $preference_source.val() - - $('.js-preference-source-wrapper').toggle(gateway_type == original_gtwy_type) - if gateway_type == original_gtwy_type && preference_source == original_preference_source - $('.js-gateway-settings').show() - $('.js-gateway-settings-warning').hide() - else - $('.js-gateway-settings').hide() - $('.js-gateway-settings-warning').show() - - $gateway_type.change(render) - $preference_source.change(render) - render() From 21826410a7eb06ef02265ed3d9ba1dbcaef2430d Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:12:31 -0800 Subject: [PATCH 10/13] Convert editable_table.js.coffee to plain JS Also simplifies. It doesn't need to be a class since it is never initialized. --- .../javascripts/spree/backend/namespaces.js | 3 ++- .../spree/backend/views/tables/editable_table.js | 15 +++++++++++++++ .../backend/views/tables/editable_table.js.coffee | 14 -------------- 3 files changed, 17 insertions(+), 15 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js delete mode 100644 backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/namespaces.js b/backend/app/assets/javascripts/spree/backend/namespaces.js index 066dc5594c8..d7a355cd764 100644 --- a/backend/app/assets/javascripts/spree/backend/namespaces.js +++ b/backend/app/assets/javascripts/spree/backend/namespaces.js @@ -6,6 +6,7 @@ _.extend(window.Spree, { Order: {}, Cart: {}, Zones: {}, - Payment: {} + Payment: {}, + Tables: {} } }) diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js b/backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js new file mode 100644 index 00000000000..454c65d3f8d --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js @@ -0,0 +1,15 @@ +Spree.Views.Tables.EditableTable = { + add: function($el) { + return new Spree.Views.Tables.EditableTableRow({ + el: $el + }); + }, + + append: function(html) { + var $row = $(html); + $('#images-table').removeClass('hidden').find('tbody').append($row); + $row.find('.select2').select2(); + $('.no-objects-found').hide(); + return this.add($row); + } +}; diff --git a/backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js.coffee b/backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js.coffee deleted file mode 100644 index 4989a2130f0..00000000000 --- a/backend/app/assets/javascripts/spree/backend/views/tables/editable_table.js.coffee +++ /dev/null @@ -1,14 +0,0 @@ -Spree.Views.Tables ||= {} - -class Spree.Views.Tables.EditableTable - @add: ($el) -> - new Spree.Views.Tables.EditableTableRow el: $el - - @append: (html) -> - $row = $(html) - - $('#images-table').removeClass('hidden').find('tbody').append($row) - $row.find('.select2').select2() - $('.no-objects-found').hide() - - @add($row) From 5b19bd7c1a8c328d9bf4e43b4072c11c953507c7 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:18:35 -0800 Subject: [PATCH 11/13] Convert backbone-overrides.js.coffee to plain JS --- .../spree/backend/backbone-overrides.js | 25 +++++++++++++++++++ .../backend/backbone-overrides.js.coffee | 23 ----------------- 2 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/backbone-overrides.js delete mode 100644 backend/app/assets/javascripts/spree/backend/backbone-overrides.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/backbone-overrides.js b/backend/app/assets/javascripts/spree/backend/backbone-overrides.js new file mode 100644 index 00000000000..cb4c3a40b0f --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/backbone-overrides.js @@ -0,0 +1,25 @@ +Backbone.ajax = Spree.ajax; + +Backbone._sync = Backbone.sync; + +Backbone.sync = function(method, model, options) { + var beforeSend = options.beforeSend; + options.beforeSend = function(xhr) { + var token = $('meta[name="csrf-token"]').attr('content'); + if (token) { + xhr.setRequestHeader('X-CSRF-Token', token); + } + if (beforeSend) { + return beforeSend.apply(this, arguments); + } + }; + + if (options.data == null && model && model.paramRoot && (method === 'create' || method === 'update' || method === 'patch')) { + options.contentType = "application/json"; + var data = {}; + data[model.paramRoot] = model.toJSON(options); + options.data = JSON.stringify(data); + } + + return Backbone._sync(method, model, options); +}; diff --git a/backend/app/assets/javascripts/spree/backend/backbone-overrides.js.coffee b/backend/app/assets/javascripts/spree/backend/backbone-overrides.js.coffee deleted file mode 100644 index a49ff31dcc5..00000000000 --- a/backend/app/assets/javascripts/spree/backend/backbone-overrides.js.coffee +++ /dev/null @@ -1,23 +0,0 @@ -Backbone.ajax = Spree.ajax - -oldSync = Backbone.sync -Backbone.sync = (method, model, options) -> - beforeSend = options.beforeSend - options.beforeSend = (xhr) -> - token = $('meta[name="csrf-token"]').attr('content') - xhr.setRequestHeader('X-CSRF-Token', token) if token - beforeSend.apply(this, arguments) if beforeSend - - # Allow for submitting requests the "rails way" - # E.g. { model_name: model_attributes } - # conditional and contentType are the same as vanilla backbone, save the - # paramRoot check. - postMethods = ['update', 'create', 'patch'] - if model?.paramRoot and !options.data and method in postMethods - options.contentType = "application/json" - data = {} - data[model.paramRoot] = model.toJSON(options) - options.data = JSON.stringify(data) - - oldSync method, model, options - From 447d7fdd38254ea6abaee73538c1348ac32091fa Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 31 Jan 2018 12:23:20 -0800 Subject: [PATCH 12/13] Convert variant_autocomplete.js.coffee to plain JS --- .../spree/backend/variant_autocomplete.js | 60 +++++++++++++++++++ .../backend/variant_autocomplete.js.coffee | 39 ------------ 2 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 backend/app/assets/javascripts/spree/backend/variant_autocomplete.js delete mode 100644 backend/app/assets/javascripts/spree/backend/variant_autocomplete.js.coffee diff --git a/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js b/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js new file mode 100644 index 00000000000..61213f2d9b0 --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js @@ -0,0 +1,60 @@ +(function() { + var variantTemplate = HandlebarsTemplates["variants/autocomplete"]; + + var formatVariantResult = function(variant) { + return variantTemplate({ + variant: variant + }); + }; + + $.fn.variantAutocomplete = function(searchOptions) { + if (searchOptions == null) { + searchOptions = {}; + } + this.select2({ + placeholder: Spree.translations.variant_placeholder, + minimumInputLength: 3, + initSelection: function(element, callback) { + Spree.ajax({ + url: Spree.routes.variants_api + "/" + element.val(), + success: callback + }); + }, + ajax: { + url: Spree.routes.variants_api, + datatype: "json", + quietMillis: 500, + params: { + "headers": { + "X-Spree-Token": Spree.api_key + } + }, + data: function(term, page) { + var searchData = { + q: { + product_name_or_sku_cont: term + }, + token: Spree.api_key + }; + return _.extend(searchData, searchOptions); + }, + + results: function(data, page) { + window.variants = data["variants"]; + return { + results: data["variants"] + }; + } + }, + + formatResult: formatVariantResult, + formatSelection: function(variant, container, escapeMarkup) { + if (variant.options_text) { + return Select2.util.escapeMarkup(variant.name + " (" + variant.options_text + ")"); + } else { + return Select2.util.escapeMarkup(variant.name); + } + } + }); + }; +})(); diff --git a/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js.coffee b/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js.coffee deleted file mode 100644 index 411ff28b368..00000000000 --- a/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js.coffee +++ /dev/null @@ -1,39 +0,0 @@ -# variant autocompletion - -variantTemplate = HandlebarsTemplates["variants/autocomplete"] - -formatVariantResult = (variant) -> - variantTemplate( - variant: variant - ) - -$.fn.variantAutocomplete = (searchOptions = {}) -> - @select2 - placeholder: Spree.translations.variant_placeholder - minimumInputLength: 3 - initSelection: (element, callback) -> - Spree.ajax - url: Spree.routes.variants_api + "/" + element.val() - success: callback - ajax: - url: Spree.routes.variants_api - datatype: "json" - quietMillis: 500 - params: { "headers": { "X-Spree-Token": Spree.api_key } } - data: (term, page) => - searchData = - q: - product_name_or_sku_cont: term - token: Spree.api_key - _.extend(searchData, searchOptions) - - results: (data, page) -> - window.variants = data["variants"] - results: data["variants"] - - formatResult: formatVariantResult - formatSelection: (variant, container, escapeMarkup) -> - if variant.options_text - Select2.util.escapeMarkup("#{variant.name} (#{variant.options_text})") - else - Select2.util.escapeMarkup(variant.name) From 2d4740c191c29292a9544df0fa3dcbec8800d7d8 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 5 Feb 2018 10:57:14 -0800 Subject: [PATCH 13/13] s/original_gtwy_type/original_gateway_type/g --- backend/app/assets/javascripts/spree/backend/gateway.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/app/assets/javascripts/spree/backend/gateway.js b/backend/app/assets/javascripts/spree/backend/gateway.js index 532ce36004d..018617bd14e 100644 --- a/backend/app/assets/javascripts/spree/backend/gateway.js +++ b/backend/app/assets/javascripts/spree/backend/gateway.js @@ -1,14 +1,14 @@ Spree.ready(function() { var $gateway_type = $('select.js-gateway-type'); var $preference_source = $('select.js-preference-source'); - var original_gtwy_type = $gateway_type.val(); + var original_gateway_type = $gateway_type.val(); var original_preference_source = $preference_source.val(); var render = function() { var gateway_type = $gateway_type.val(); var preference_source = $preference_source.val(); - $('.js-preference-source-wrapper').toggle(gateway_type === original_gtwy_type); - if (gateway_type === original_gtwy_type && preference_source === original_preference_source) { + $('.js-preference-source-wrapper').toggle(gateway_type === original_gateway_type); + if (gateway_type === original_gateway_type && preference_source === original_preference_source) { $('.js-gateway-settings').show(); $('.js-gateway-settings-warning').hide(); } else {