-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1618 from jhawthorn/smart_calculator
Allow changing calculator types and attributes without reloading page
- Loading branch information
Showing
12 changed files
with
250 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 24 additions & 14 deletions
38
backend/app/assets/javascripts/spree/backend/calculator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
Spree.CalculatorEditView = Backbone.View.extend({ | ||
events: { | ||
"change .js-calculator-type": "render", | ||
}, | ||
|
||
initialize: function() { | ||
this.render(); | ||
}, | ||
|
||
render: function() { | ||
var selected_class = this.$('.js-calculator-type option:selected').val(); | ||
this.$('.js-calculator-preferences').each(function() { | ||
var selected = ($(this).data('calculator-type') === selected_class); | ||
$(this).find(':input').prop("disabled", !selected); | ||
$(this).toggle(selected); | ||
}); | ||
} | ||
}) | ||
|
||
$(function() { | ||
var calculator_select = $('select#calc_type') | ||
var original_calc_type = calculator_select.prop('value'); | ||
$('.calculator-settings-warning').hide(); | ||
calculator_select.change(function() { | ||
if (calculator_select.prop('value') == original_calc_type) { | ||
$('div.calculator-settings').show(); | ||
$('.calculator-settings-warning').hide(); | ||
$('.calculator-settings').find('input,textarea').prop("disabled", false); | ||
} else { | ||
$('div.calculator-settings').hide(); | ||
$('.calculator-settings-warning').show(); | ||
$('.calculator-settings').find('input,texttarea').prop("disabled", true); | ||
} | ||
$('.js-calculator-fields').each(function() { | ||
new Spree.CalculatorEditView({ | ||
el: this | ||
}); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 19 additions & 20 deletions
39
...p/views/spree/admin/promotions/actions/_promotion_calculators_with_custom_fields.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,33 @@ | ||
<div class="col-xs-12"> | ||
<div class="calculator-fields row"> | ||
<div class="calculator-fields js-calculator-fields row"> | ||
|
||
<div class="col-xs-6"> | ||
<div class="field"> | ||
<% field_name = "#{param_prefix}[calculator_type]" %> | ||
<%= label_tag field_name, Spree::Calculator.model_name.human %> | ||
<%= select_tag field_name, | ||
options_from_collection_for_select(calculators, :to_s, :description, promotion_action.calculator.type), | ||
:class => 'type-select select2 fullwidth' %> | ||
<% if promotion_action.calculator.respond_to?(:preferences) %> | ||
<span class="warning info"><%= Spree.t(:calculator_settings_warning) %></span> | ||
<% end %> | ||
:class => 'type-select js-calculator-type select2 fullwidth' %> | ||
</div> | ||
</div> | ||
|
||
<% unless promotion_action.new_record? %> | ||
<div class="col-xs-6"> | ||
<div class="settings field"> | ||
<% type_name = promotion_action.calculator.type.demodulize.underscore %> | ||
<% if lookup_context.exists?("fields", | ||
["spree/admin/promotions/calculators/#{type_name}"], true) %> | ||
<%= render "spree/admin/promotions/calculators/#{type_name}/fields", | ||
calculator: promotion_action.calculator, prefix: param_prefix %> | ||
<% else %> | ||
<%= render "spree/admin/promotions/calculators/default_fields", | ||
calculator: promotion_action.calculator, prefix: param_prefix %> | ||
<% end %> | ||
<%= hidden_field_tag "#{param_prefix}[calculator_attributes][id]", promotion_action.calculator.id %> | ||
</div> | ||
<div class="col-xs-6"> | ||
<div class="settings field"> | ||
<% calculators.each do |calculator_class| %> | ||
<% calculator = promotion_action.calculator.class == calculator_class ? promotion_action.calculator : calculator_class.new %> | ||
<div class="js-calculator-preferences" data-calculator-type="<%= calculator_class %>"> | ||
<% type_name = calculator.type.demodulize.underscore %> | ||
<% if lookup_context.exists?("fields", | ||
["spree/admin/promotions/calculators/#{type_name}"], true) %> | ||
<%= render "spree/admin/promotions/calculators/#{type_name}/fields", | ||
calculator: calculator, prefix: param_prefix %> | ||
<% else %> | ||
<%= render "spree/admin/promotions/calculators/default_fields", | ||
calculator: calculator, prefix: param_prefix %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 10 additions & 13 deletions
23
backend/app/views/spree/admin/shared/_calculator_fields.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.