Skip to content
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

Fix taxons page with new Spree.ajax calls (and cleanup) #157

Merged
merged 12 commits into from
Jul 27, 2015
1 change: 0 additions & 1 deletion backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//= require modernizr
//= require jquery.cookie
//= require jquery.jstree/jquery.jstree
//= require jquery.alerts/jquery.alerts
//= require jquery.powertip
//= require jquery.vAlign
//= require equalize
Expand Down
70 changes: 27 additions & 43 deletions backend/app/assets/javascripts/spree/backend/taxonomy.js.coffee
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
handle_ajax_error = (XMLHttpRequest, textStatus, errorThrown) ->
$.jstree.rollback(last_rollback)
$("#ajax_error").show().html("<strong>" + server_error + "</strong><br />" + taxonomy_tree_error)
base_url = null

tree_error_handler = (data) ->
rollback = data.rlbk
(XMLHttpRequest, textStatus, errorThrown) ->
$.jstree.rollback(rollback)
$("#ajax_error").show().html("<strong>#{server_error}</strong><br />" + taxonomy_tree_error)

handle_move = (e, data) ->
last_rollback = data.rlbk
position = data.rslt.cp
node = data.rslt.o
new_parent = data.rslt.np

url = "#{base_url}/#{node.prop("id")}"
Spree.ajax
type: "POST",
type: "PUT",
dataType: "json",
url: url,
data: ({_method: "put", "taxon[parent_id]": new_parent.prop("id"), "taxon[child_index]": position }),
error: handle_ajax_error
data: ({"taxon[parent_id]": new_parent.prop("id"), "taxon[child_index]": position }),
error: tree_error_handler(data)

true

handle_create = (e, data) ->
last_rollback = data.rlbk
node = data.rslt.obj
name = data.rslt.name
position = data.rslt.position
Expand All @@ -28,68 +30,58 @@ handle_create = (e, data) ->
Spree.ajax
type: "POST",
dataType: "json",
url: base_url.toString(),
url: base_url,
data: {
"taxon[name]": name,
"taxon[parent_id]": new_parent.prop("id"),
"taxon[child_index]": position,
token: Spree.api_key
},
error: handle_ajax_error,
error: tree_error_handler(data)
success: (data,result) ->
node.prop('id', data.id)

handle_rename = (e, data) ->
last_rollback = data.rlbk
node = data.rslt.obj
name = data.rslt.new_name

url = "#{base_url}/#{node.prop("id")}"

Spree.ajax
type: "POST",
type: "PUT",
dataType: "json",
url: url,
data: {
_method: "put",
"taxon[name]": name,
},
error: handle_ajax_error
error: tree_error_handler(data)

handle_delete = (e, data) ->
last_rollback = data.rlbk
node = data.rslt.obj
delete_url = "#{base_url}/#{node.prop("id")}"
jConfirm Spree.translations.are_you_sure_delete, Spree.translations.confirm_delete, (r) ->
if r
Spree.ajax
type: "POST",
dataType: "json",
url: delete_url.toString(),
data: {
_method: "delete",
token: Spree.api_key
},
error: handle_ajax_error
else
$.jstree.rollback(last_rollback)
last_rollback = null

root = exports ? this
root.setup_taxonomy_tree = (taxonomy_id) ->
if confirm(Spree.translations.are_you_sure_delete)
Spree.ajax
type: "DELETE",
dataType: "json",
url: delete_url,
error: tree_error_handler(data)
else
$.jstree.rollback(data.rlbk)

@setup_taxonomy_tree = (taxonomy_id) ->
if taxonomy_id != undefined
# this is defined within admin/taxonomies/edit
root.base_url = Spree.routes.taxonomy_taxons_path
base_url = Spree.routes.taxonomy_taxons_path

Spree.ajax
url: Spree.url(base_url.path().replace("/taxons", "/jstree")).toString(),
url: base_url.replace("/taxons", "/jstree"),
success: (taxonomy) ->
last_rollback = null

conf =
json_data:
data: taxonomy,
ajax:
headers: { "X-Spree-Token": Spree.api_key }
url: (e) ->
"#{base_url}/#{e.prop('id')}/jstree"
themes:
Expand Down Expand Up @@ -126,11 +118,3 @@ root.setup_taxonomy_tree = (taxonomy_id) ->
.bind("rename.jstree", handle_rename)
.bind "loaded.jstree", ->
$(this).jstree("core").toggle_node($('.jstree-icon').first())

$("#taxonomy_tree a").on "dblclick", (e) ->
$("#taxonomy_tree").jstree("rename", this)

# surpress form submit on enter/return
$(document).keypress (e) ->
if e.keyCode == 13
e.preventDefault()
2 changes: 0 additions & 2 deletions backend/app/assets/stylesheets/spree/backend.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.

*= require jquery.alerts/jquery.alerts
*= require jquery.alerts/jquery.alerts.spree
*= require responsive-tables
*= require normalize
*= require skeleton
Expand Down
1 change: 0 additions & 1 deletion backend/app/views/spree/admin/shared/_head.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<%= render "spree/admin/shared/translations" %>

<%= javascript_tag do -%>
jQuery.alerts.dialogClass = 'spree';
var AUTH_TOKEN = "<%= form_authenticity_token %>";
Spree.api_key = "<%= try_spree_current_user.try(:spree_api_key) %>";
Spree.env = "<%= Rails.env %>";
Expand Down
30 changes: 13 additions & 17 deletions backend/app/views/spree/admin/taxonomies/edit.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,23 @@
<%= form_for [:admin, @taxonomy] do |f| %>
<fieldset class="no-border-top">
<%= render :partial => 'form', :locals => { :f => f } %>
<div>
<%= label_tag nil, Spree.t(:tree) %><br />
<script>
Spree.routes.taxonomy_taxons_path = "<%= spree.api_taxonomy_taxons_path(@taxonomy) %>";
Spree.routes.admin_taxonomy_taxons_path = "<%= spree.admin_taxonomy_taxons_path(@taxonomy) %>";
Spree.routes.jstree_theme_path = "<%= stylesheet_path("jquery.jstree/themes/apple/style.css") %>";
</script>
<div id="taxonomy_tree" class="tree"></div>
</div>
<div id="progress" style="display:none;">
<%= image_tag 'select2-spinner.gif', :title => 'Spinner', :style => "vertical-align:bottom;" %> <%= Spree.t(:updating) %>..
</div>

<div class="info"><%= Spree.t(:taxonomy_tree_instruction) %></div>

<br>

<div class="filter-actions actions">
<%= button Spree.t('actions.update'), 'refresh' %>
<span class="or"><%= Spree.t(:or) %></span>
<%= button_link_to Spree.t('actions.cancel'), admin_taxonomies_path, :icon => 'remove' %>
</div>
</fieldset>


<div>
<%= label_tag nil, Spree.t(:tree) %><br />
<script>
Spree.routes.taxonomy_taxons_path = "<%= spree.api_taxonomy_taxons_path(@taxonomy) %>";
Spree.routes.admin_taxonomy_taxons_path = "<%= spree.admin_taxonomy_taxons_path(@taxonomy) %>";
Spree.routes.jstree_theme_path = "<%= stylesheet_path("jquery.jstree/themes/apple/style.css") %>";
</script>
<div id="taxonomy_tree" class="tree"></div>

<div class="info"><%= Spree.t(:taxonomy_tree_instruction) %></div>
</div>
<% end %>
1 change: 0 additions & 1 deletion backend/lib/spree/backend/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Engine < ::Rails::Engine
jquery.jstree/themes/apple/*
fontawesome-webfont*
select2_locale*
jquery.alerts/images/*
]
end
end
Expand Down
7 changes: 7 additions & 0 deletions backend/spec/features/admin/taxons_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
click_button "Update"
expect(page).to have_content("Taxon \"Shirt\" has been successfully updated!")
end

it "can see taxon tree", js: true do
taxonomy = create :taxonomy

visit spree.edit_admin_taxonomy_path(taxonomy)
expect(page).to have_content('Brand')
end
end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.