Skip to content

Instantly share code, notes, and snippets.

@valterbarros
Created August 23, 2018 14:07
Show Gist options
  • Save valterbarros/da3efed5d7397018c59f14c86f364c9a to your computer and use it in GitHub Desktop.
Save valterbarros/da3efed5d7397018c59f14c86f364c9a to your computer and use it in GitHub Desktop.
A feature that know lives only in my mind not in my company production
function update_total(data) {
data.total = data.value * data.quantity
}
var registryProxy
// Go through each of our data properties
registry_observer = function(data) {
track_keys = ['value', 'quantity']
track_keys.forEach(function(key) {
var internalValue = data[key]
Object.defineProperty(data, key, {
get: function() {
return internalValue
},
set: function(new_val) {
if (track_keys.includes(key)) {
var treated_value = StantFormat.convertCurrencyToFloat(new_val)
internalValue = treated_value
update_total(data)
if (this.debug) {
console.log('set triggered with', new_val)
}
}
}
})
})
}
class @ConfigurePreplanningToCalculateTotal
@reset_plannings_resume: ->
window.preplannings_data_resume = []
@remove_element_from_preplanning_data_resume: (removed_element) ->
window.preplannings_data_resume = window.preplannings_data_resume.filter (el) ->
return false if el == removed_element
return true
$('.js-preplanning-value').trigger('change')
@init: (service, preplanning_section_html) ->
treated_html = set_preplanning_resume_data(service, preplanning_section_html)
handle_quantity_and_value_change_and_update_total_general()
return treated_html
set_preplanning_resume_data = (service, preplanning_section_html) ->
preplanning_section_html = parse_html_to_dom_object(preplanning_section_html)
value = StantFormat.convertCurrencyToFloat(service.value) || 0
quantity = StantFormat.convertCurrencyToFloat(service.quantity) || 0
preplanning_data = {
value: value,
quantity: quantity,
total: value * quantity,
debug: false
}
registry_observer(preplanning_data)
window.preplannings_data_resume.push(preplanning_data)
preplanning_section_html[0].preplanning_data = preplanning_data
return preplanning_section_html
parse_html_to_dom_object = (html) ->
return $.parseHTML(html)
handle_quantity_and_value_change_and_update_total_general = ->
$('.js-services-form').on 'change', ['.js-preplanning-value js-preplanning-quantity'], (e) ->
input = e.target
preplannig_data = $(input).parents('.js-services-preplanning-row').get(0).preplanning_data
sum = 0
if input.classList.contains('js-preplanning-value')
preplannig_data.value = input.value
else
preplannig_data.quantity = input.value
for current_input_element in window.preplannings_data_resume
sum += current_input_element.total
sum = sum.toFixed(2)
$('#js-text-planning').val(StantFormat.converteFloatMoeda(sum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment