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 a0d2427e70c..00000000000 --- a/core/app/assets/javascripts/spree.js.coffee.erb +++ /dev/null @@ -1,64 +0,0 @@ -#= 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) - - @mountedAt: -> - "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>" - - @pathFor: (path) -> - locationOrigin = "#{window.location.protocol}//#{window.location.hostname}" + (if window.location.port then ":#{window.location.port}" else "") - "#{locationOrigin}#{@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 - @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 - @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) - - @routes: - states_search: @pathFor('api/states') - apply_coupon_code: (order_id) -> - Spree.pathFor("api/orders/#{order_id}/apply_coupon_code") - - @getJSON: (url, data, success) -> - if typeof data is 'function' - success = data - data = undefined - @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 + }); +};