Guest User

Untitled

a guest
Nov 19th, 2019
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ReportCards
  2.   @init: ->
  3.     @bind()
  4.  
  5.   @bind: ->
  6.     $('#personal-table').on 'blur', '.report-card', @changeWorkStatus
  7.  
  8.   @changeWorkStatus: ->
  9.     position = $(this)
  10.     reportCardId = position.data('report-card-id')
  11.     personalId = position.parent().data('personal-id')
  12.     worksTime = position.text().trim()
  13.  
  14.     if reportCardId == undefined && personalId == undefined
  15.       position.parent().find('td:first').addClass('error')
  16.       return
  17.  
  18.     if reportCardId == undefined
  19.       if worksTime != ''
  20.         url = '/report_cards'
  21.         ajaxSendReportCard(url, 'POST', personalId, reportCardId, position)
  22.     else
  23.       url = '/report_cards/' + reportCardId
  24.       if worksTime == '0' || (worksTime == '' && reportCardId)
  25.         ajaxDeleteReportCard(url, position)
  26.       else
  27.         ajaxSendReportCard(url, 'PUT', personalId, reportCardId, position)
  28.     return
  29.  
  30.   ajaxSendReportCard = (url, method, personalId, reportCardId, position) ->
  31.     worksTime = position.text().trim()
  32.     workDate = position.data('work-date')
  33.  
  34.     $.ajax url,
  35.       method: method
  36.       dataType: 'json'
  37.       data:
  38.         report_card:
  39.           work_date: workDate
  40.           personnel_id: personalId
  41.           works_time: worksTime
  42.         id: reportCardId
  43.       success: (data) ->
  44.         position.attr('data-report-card-id', data.report_card.id)
  45.         position.removeClass('error')
  46.       error: (data) ->
  47.         position.addClass('error')
  48.         console.log(data.responseJSON.message)
  49.     return
  50.  
  51.   ajaxDeleteReportCard = (url, position) ->
  52.  
  53.     $.ajax url,
  54.       method: 'DELETE'
  55.       dataType: 'json'
  56.       success: ->
  57.         position.removeAttr('data-report-card-id')
  58.         position.removeClass('error')
  59.       error: (data) ->
  60.         position.addClass('error')
  61.         console.log(data.responseJSON.message)
  62.     return
  63.  
  64. $(document).on 'turbolinks:load', ->
  65.   ReportCards.init()
RAW Paste Data