From c60e986dda6302d25f74486a042067344b151184 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 2 Mar 2017 14:34:17 -0800 Subject: [PATCH 1/2] JS window.Spree should not be a class Previously we were declaring Spree to be a CoffeeScript class. This was harmless, because it was compiled into an empty function, but also unnecessary, because it had no class methods and was never used as a class. --- .../assets/javascripts/spree.js.coffee.erb | 112 +++++++++--------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/core/app/assets/javascripts/spree.js.coffee.erb b/core/app/assets/javascripts/spree.js.coffee.erb index a0d2427e70c..0051f63783d 100644 --- a/core/app/assets/javascripts/spree.js.coffee.erb +++ b/core/app/assets/javascripts/spree.js.coffee.erb @@ -1,64 +1,66 @@ #= require jsuri -class window.Spree - @ready: (callback) -> - if typeof Turbolinks isnt 'undefined' and Turbolinks.supported - jQuery(document).on 'turbolinks:load', -> callback(jQuery) - else - jQuery(document).ready(callback) +window.Spree = {} - @mountedAt: -> - "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>" +Spree.ready = (callback) -> + if typeof Turbolinks isnt 'undefined' and Turbolinks.supported + jQuery(document).on 'turbolinks:load', -> callback(jQuery) + else + jQuery(document).ready(callback) - @pathFor: (path) -> - locationOrigin = "#{window.location.protocol}//#{window.location.hostname}" + (if window.location.port then ":#{window.location.port}" else "") - "#{locationOrigin}#{@mountedAt()}#{path}" +Spree.mountedAt = -> + "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>" - # Helper function to take a URL and add query parameters to it - # Uses the JSUri library from here: https://github.com/derek-watson/jsUri - # Thanks to Jake Moffat for the suggestion: https://twitter.com/jakeonrails/statuses/321776992221544449 - @url: (uri, query) -> - if Spree.env == 'development' || Spree.env == 'test' - console.warn 'Spree.url is deprecated, please use Spree.ajax for your request instead.' - if uri.path == undefined - uri = new Uri(uri) - if query - $.each query, (key, value) -> - uri.addQueryParam(key, value) - return uri +Spree.pathFor = (path) -> + locationOrigin = "#{window.location.protocol}//#{window.location.hostname}" + (if window.location.port then ":#{window.location.port}" else "") + "#{locationOrigin}#{Spree.mountedAt()}#{path}" - # This function automatically appends the API token - # for the user to the end of any URL. - # Immediately after, this string is then passed to jQuery.ajax. - # - # Spree.ajax works in two ways to support common jQuery syntax: - # - # Spree.ajax("url", {settings: 'go here'}) - # or: - # Spree.ajax({url: "url", settings: 'go here'}) - # - # Spree.getJSON has the same method signature as $.getJSON - @ajax: (url, options) -> - if typeof(url) == "object" - options = url - url = undefined +# Helper function to take a URL and add query parameters to it +# Uses the JSUri library from here: https://github.com/derek-watson/jsUri +# Thanks to Jake Moffat for the suggestion: https://twitter.com/jakeonrails/statuses/321776992221544449 +Spree.url = (uri, query) -> + if Spree.env == 'development' || Spree.env == 'test' + console.warn 'Spree.url is deprecated, please use Spree.ajax for your request instead.' + if uri.path == undefined + uri = new Uri(uri) + if query + $.each query, (key, value) -> + uri.addQueryParam(key, value) + return uri - options = options || {} +# This function automatically appends the API token +# for the user to the end of any URL. +# Immediately after, this string is then passed to jQuery.ajax. +# +# Spree.ajax works in two ways to support common jQuery syntax: +# +# Spree.ajax("url", {settings: 'go here'}) +# or: +# Spree.ajax({url: "url", settings: 'go here'}) +# +# Spree.getJSON has the same method signature as $.getJSON +Spree.ajax = (url, options) -> + if typeof(url) == "object" + options = url + url = undefined - options = $.extend(options, { headers: { "X-Spree-Token": Spree.api_key } }) - $.ajax(url, options) + options = options || {} - @routes: - states_search: @pathFor('api/states') - apply_coupon_code: (order_id) -> - Spree.pathFor("api/orders/#{order_id}/apply_coupon_code") + options = $.extend(options, { headers: { "X-Spree-Token": Spree.api_key } }) + $.ajax(url, options) - @getJSON: (url, data, success) -> - if typeof data is 'function' - success = data - data = undefined - @ajax( - dataType: "json", - url: url, - data: data, - success: success - ) +Spree.routes = { + states_search: Spree.pathFor('api/states') + apply_coupon_code: (order_id) -> + Spree.pathFor("api/orders/#{order_id}/apply_coupon_code") +} + +Spree.getJSON = (url, data, success) -> + if typeof data is 'function' + success = data + data = undefined + Spree.ajax( + dataType: "json", + url: url, + data: data, + success: success + ) From 6803a3e9cf30b408bf23da32ca78a1a7e8d1040e Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 2 Mar 2017 15:09:00 -0800 Subject: [PATCH 2/2] Convert spree.js.coffee to plain JS This file's history was already replaced by the previous commit, so we might as well convert to plain JS at the same time. This is the only CoffeeScript (now only JS) file in the solidus_core gem, so it can now in theory be used in an app without needing to pull in CoffeeScript (assuming the app doesn't include solidus_frontend or soldius_backend). --- .../assets/javascripts/spree.js.coffee.erb | 66 ----------------- core/app/assets/javascripts/spree.js.erb | 72 +++++++++++++++++++ 2 files changed, 72 insertions(+), 66 deletions(-) delete mode 100644 core/app/assets/javascripts/spree.js.coffee.erb create mode 100644 core/app/assets/javascripts/spree.js.erb diff --git a/core/app/assets/javascripts/spree.js.coffee.erb b/core/app/assets/javascripts/spree.js.coffee.erb deleted file mode 100644 index 0051f63783d..00000000000 --- a/core/app/assets/javascripts/spree.js.coffee.erb +++ /dev/null @@ -1,66 +0,0 @@ -#= require jsuri -window.Spree = {} - -Spree.ready = (callback) -> - if typeof Turbolinks isnt 'undefined' and Turbolinks.supported - jQuery(document).on 'turbolinks:load', -> callback(jQuery) - else - jQuery(document).ready(callback) - -Spree.mountedAt = -> - "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>" - -Spree.pathFor = (path) -> - locationOrigin = "#{window.location.protocol}//#{window.location.hostname}" + (if window.location.port then ":#{window.location.port}" else "") - "#{locationOrigin}#{Spree.mountedAt()}#{path}" - -# Helper function to take a URL and add query parameters to it -# Uses the JSUri library from here: https://github.com/derek-watson/jsUri -# Thanks to Jake Moffat for the suggestion: https://twitter.com/jakeonrails/statuses/321776992221544449 -Spree.url = (uri, query) -> - if Spree.env == 'development' || Spree.env == 'test' - console.warn 'Spree.url is deprecated, please use Spree.ajax for your request instead.' - if uri.path == undefined - uri = new Uri(uri) - if query - $.each query, (key, value) -> - uri.addQueryParam(key, value) - return uri - -# This function automatically appends the API token -# for the user to the end of any URL. -# Immediately after, this string is then passed to jQuery.ajax. -# -# Spree.ajax works in two ways to support common jQuery syntax: -# -# Spree.ajax("url", {settings: 'go here'}) -# or: -# Spree.ajax({url: "url", settings: 'go here'}) -# -# Spree.getJSON has the same method signature as $.getJSON -Spree.ajax = (url, options) -> - if typeof(url) == "object" - options = url - url = undefined - - options = options || {} - - options = $.extend(options, { headers: { "X-Spree-Token": Spree.api_key } }) - $.ajax(url, options) - -Spree.routes = { - states_search: Spree.pathFor('api/states') - apply_coupon_code: (order_id) -> - Spree.pathFor("api/orders/#{order_id}/apply_coupon_code") -} - -Spree.getJSON = (url, data, success) -> - if typeof data is 'function' - success = data - data = undefined - Spree.ajax( - dataType: "json", - url: url, - data: data, - success: success - ) diff --git a/core/app/assets/javascripts/spree.js.erb b/core/app/assets/javascripts/spree.js.erb new file mode 100644 index 00000000000..6351918c769 --- /dev/null +++ b/core/app/assets/javascripts/spree.js.erb @@ -0,0 +1,72 @@ +//= require jsuri + +window.Spree = {}; + +Spree.ready = function(callback) { + if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) { + jQuery(document).on('turbolinks:load', function() { + callback(jQuery); + }); + } else { + jQuery(document).ready(callback); + } +}; + +Spree.mountedAt = function() { + return "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>"; +}; + +Spree.pathFor = function(path) { + var locationOrigin; + locationOrigin = (window.location.protocol + "//" + window.location.hostname) + (window.location.port ? ":" + window.location.port : ""); + return locationOrigin + Spree.mountedAt() + path; +}; + +Spree.url = function(uri, query) { + if (Spree.env === 'development' || Spree.env === 'test') { + console.warn('Spree.url is deprecated, please use Spree.ajax for your request instead.'); + } + if (uri.path === undefined) { + uri = new Uri(uri); + } + if (query) { + $.each(query, function(key, value) { + return uri.addQueryParam(key, value); + }); + } + return uri; +}; + +Spree.ajax = function(url, options) { + if (typeof url === "object") { + options = url; + url = undefined; + } + options = options || {}; + options = $.extend(options, { + headers: { + "X-Spree-Token": Spree.api_key + } + }); + return $.ajax(url, options); +}; + +Spree.routes = { + states_search: Spree.pathFor('api/states'), + apply_coupon_code: function(order_id) { + return Spree.pathFor("api/orders/" + order_id + "/apply_coupon_code"); + } +}; + +Spree.getJSON = function(url, data, success) { + if (typeof data === 'function') { + success = data; + data = undefined; + } + return Spree.ajax({ + dataType: "json", + url: url, + data: data, + success: success + }); +};