2015-01-31 04:50:05 +00:00
|
|
|
$ = jQuery
|
|
|
|
|
context = window
|
|
|
|
|
context.JK ||= {};
|
|
|
|
|
|
2015-02-06 06:00:07 +00:00
|
|
|
context.JK.SiteValidator = class SiteValidator
|
2015-01-31 18:07:51 +00:00
|
|
|
|
2015-06-03 21:34:19 +00:00
|
|
|
constructor: (site_type, success_callback, fail_callback, parent) ->
|
2015-01-31 04:50:05 +00:00
|
|
|
@EVENTS = context.JK.EVENTS
|
|
|
|
|
@rest = context.JK.Rest()
|
2015-02-04 07:32:07 +00:00
|
|
|
@site_type = site_type
|
2015-06-03 21:34:19 +00:00
|
|
|
@input_div = $(".site_validator."+site_type+"_validator", parent)
|
2015-06-04 23:55:15 +00:00
|
|
|
@data_input = @input_div.find('input')
|
|
|
|
|
|
2015-01-31 08:41:56 +00:00
|
|
|
@logger = context.JK.logger
|
2015-02-01 03:43:58 +00:00
|
|
|
@spinner = @input_div.find('span.spinner-small')
|
|
|
|
|
@checkmark = @input_div.find('.validate-checkmark')
|
2015-02-15 07:50:04 +00:00
|
|
|
this.setSiteStatus(null)
|
|
|
|
|
this.showFormatStatus()
|
2015-02-15 12:27:48 +00:00
|
|
|
@is_rec_src = false
|
|
|
|
|
@deferred_status_check = null
|
|
|
|
|
@is_validating = false
|
2015-03-29 14:06:39 +00:00
|
|
|
@success_callback = success_callback
|
|
|
|
|
@fail_callback = fail_callback
|
2015-01-31 04:50:05 +00:00
|
|
|
|
|
|
|
|
init: () =>
|
2015-01-31 18:45:10 +00:00
|
|
|
this.renderErrors({})
|
2015-02-01 03:43:58 +00:00
|
|
|
@spinner.hide()
|
2015-01-31 18:07:51 +00:00
|
|
|
validator = this
|
2015-02-10 07:15:56 +00:00
|
|
|
@data_input.on 'blur', ->
|
2015-02-15 13:31:45 +00:00
|
|
|
validator.didBlur()
|
2015-02-10 07:15:56 +00:00
|
|
|
@data_input.on 'focus', ->
|
2015-02-15 07:50:04 +00:00
|
|
|
validator.showFormatStatus()
|
2015-02-15 12:27:48 +00:00
|
|
|
@data_input.on 'change', ->
|
|
|
|
|
@site_status = null
|
2015-01-31 18:07:51 +00:00
|
|
|
|
2015-02-15 07:50:04 +00:00
|
|
|
dataToValidate: () =>
|
2015-06-04 23:55:15 +00:00
|
|
|
url = @data_input.val()
|
2015-03-26 02:20:43 +00:00
|
|
|
if url && 0 < url.length
|
2015-01-31 18:07:51 +00:00
|
|
|
url.substring(0,2000)
|
|
|
|
|
else
|
|
|
|
|
null
|
2015-01-31 05:22:58 +00:00
|
|
|
|
2015-02-15 07:50:04 +00:00
|
|
|
showFormatStatus: () =>
|
|
|
|
|
data = this.dataToValidate()
|
2015-01-31 08:59:25 +00:00
|
|
|
yn = true
|
2015-02-12 08:16:54 +00:00
|
|
|
if data && ('url' == @site_type || @is_rec_src)
|
2015-02-04 07:32:07 +00:00
|
|
|
regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
|
2015-02-15 07:50:04 +00:00
|
|
|
yn = regexp.test(this.dataToValidate())
|
|
|
|
|
unless yn
|
2015-02-01 03:43:58 +00:00
|
|
|
@checkmark.hide()
|
2015-01-31 08:41:56 +00:00
|
|
|
yn
|
|
|
|
|
|
2015-06-04 23:55:15 +00:00
|
|
|
didBlur: () =>
|
2015-02-15 07:50:04 +00:00
|
|
|
if this.showFormatStatus()
|
|
|
|
|
this.validateSite()
|
2015-02-10 07:15:56 +00:00
|
|
|
|
2015-02-15 07:50:04 +00:00
|
|
|
validateSite: () =>
|
2015-02-15 23:10:29 +00:00
|
|
|
unless data = this.dataToValidate()
|
2015-03-30 01:22:12 +00:00
|
|
|
if @success_callback
|
|
|
|
|
@success_callback(@input_div)
|
2015-02-15 23:10:29 +00:00
|
|
|
return null
|
2015-02-15 07:50:04 +00:00
|
|
|
this.setSiteStatus(null)
|
2015-02-01 03:43:58 +00:00
|
|
|
@spinner.show()
|
2015-02-15 23:10:29 +00:00
|
|
|
@rest.validateUrlSite(data, @site_type)
|
|
|
|
|
.done(this.processSiteCheckSucceed)
|
2015-01-31 18:07:51 +00:00
|
|
|
.fail(this.processSiteCheckFail)
|
|
|
|
|
|
2015-02-15 23:10:29 +00:00
|
|
|
processSiteCheckSucceed: (response) =>
|
2015-02-01 03:43:58 +00:00
|
|
|
@spinner.hide()
|
2015-02-12 08:31:37 +00:00
|
|
|
|
|
|
|
|
if 'Valid Site' == response.message
|
2015-02-15 12:27:48 +00:00
|
|
|
this.setSiteStatus(true)
|
2015-02-12 08:31:37 +00:00
|
|
|
this.renderErrors({})
|
2015-02-15 12:27:48 +00:00
|
|
|
if @deferred_status_check
|
2015-02-15 13:31:45 +00:00
|
|
|
@deferred_status_check.resolve()
|
2015-03-29 14:06:39 +00:00
|
|
|
if @success_callback
|
|
|
|
|
@success_callback(@input_div)
|
2015-01-31 18:07:51 +00:00
|
|
|
else
|
2015-02-15 12:27:48 +00:00
|
|
|
this.setSiteStatus(false)
|
2015-02-12 08:31:37 +00:00
|
|
|
this.renderErrors(response)
|
2015-02-15 12:27:48 +00:00
|
|
|
if @deferred_status_check
|
2015-02-15 13:31:45 +00:00
|
|
|
@deferred_status_check.reject()
|
2015-03-29 14:06:39 +00:00
|
|
|
if @fail_callback
|
|
|
|
|
@fail_callback(@input_div)
|
|
|
|
|
|
2015-02-15 13:31:45 +00:00
|
|
|
@deferred_status_check = null
|
2015-04-02 03:09:16 +00:00
|
|
|
@logger.debug("site_status = " + @site_status)
|
2015-01-31 18:07:51 +00:00
|
|
|
|
2015-01-31 18:45:10 +00:00
|
|
|
processSiteCheckFail: (response) =>
|
2015-01-31 18:07:51 +00:00
|
|
|
@logger.error("site check error")
|
2015-02-15 12:27:48 +00:00
|
|
|
this.setSiteStatus(false)
|
|
|
|
|
if @deferred_status_check
|
2015-02-15 13:31:45 +00:00
|
|
|
@deferred_status_check.reject()
|
|
|
|
|
@deferred_status_check = null
|
2015-01-31 18:45:10 +00:00
|
|
|
|
2015-02-15 07:50:04 +00:00
|
|
|
setSiteStatus: (status) =>
|
|
|
|
|
@site_status = status
|
|
|
|
|
@spinner.hide()
|
2015-02-15 12:27:48 +00:00
|
|
|
if true == status
|
2015-02-15 07:50:04 +00:00
|
|
|
@checkmark.show()
|
|
|
|
|
else
|
|
|
|
|
@checkmark.hide()
|
2015-06-04 23:55:15 +00:00
|
|
|
|
|
|
|
|
siteIsValid: () =>
|
2015-02-15 12:27:48 +00:00
|
|
|
this.setSiteStatus(true)
|
2015-06-04 23:55:15 +00:00
|
|
|
|
|
|
|
|
siteIsInvalid: () =>
|
2015-02-15 12:27:48 +00:00
|
|
|
this.setSiteStatus(false)
|
2015-06-04 23:55:15 +00:00
|
|
|
|
|
|
|
|
renderErrors: (errors) =>
|
2015-02-10 05:47:07 +00:00
|
|
|
errdiv = @input_div.find('.error')
|
2015-01-31 18:45:10 +00:00
|
|
|
if errmsg = context.JK.format_errors("site", errors)
|
|
|
|
|
errdiv.show()
|
|
|
|
|
errdiv.html(errmsg)
|
|
|
|
|
else
|
|
|
|
|
errdiv.hide()
|
|
|
|
|
errdiv.html('')
|
2015-02-15 12:27:48 +00:00
|
|
|
|
|
|
|
|
state: () =>
|
|
|
|
|
dfr = $.Deferred()
|
|
|
|
|
if null == @site_status
|
|
|
|
|
@deferred_status_check = dfr
|
2015-06-04 23:55:15 +00:00
|
|
|
this.validateSite()
|
2015-02-15 12:27:48 +00:00
|
|
|
else
|
|
|
|
|
if true == @site_status
|
|
|
|
|
dfr.resolve()
|
|
|
|
|
else
|
|
|
|
|
dfr.reject()
|
|
|
|
|
return dfr.promise()
|
2015-02-15 23:10:29 +00:00
|
|
|
|
2015-06-04 23:55:15 +00:00
|
|
|
|
|
|
|
|
context.JK.RecordingSourceValidator = class RecordingSourceValidator extends SiteValidator
|
|
|
|
|
constructor: (site_type, success_callback, fail_callback, parent) ->
|
2015-06-03 21:34:19 +00:00
|
|
|
super(site_type, success_callback, fail_callback, parent)
|
2015-02-15 12:27:48 +00:00
|
|
|
@recording_sources = []
|
|
|
|
|
@is_rec_src = true
|
2015-02-15 23:10:29 +00:00
|
|
|
@add_btn = @input_div.find('a.add-recording-source')
|
2015-04-02 03:09:16 +00:00
|
|
|
@site_success_callback = success_callback
|
|
|
|
|
@site_fail_callback = fail_callback
|
2015-02-15 12:27:48 +00:00
|
|
|
|
|
|
|
|
init: (sources) =>
|
|
|
|
|
super()
|
|
|
|
|
if sources
|
|
|
|
|
@recording_sources = sources
|
2015-06-04 23:55:15 +00:00
|
|
|
@add_btn.off('click').on 'click', =>
|
2015-02-15 23:10:29 +00:00
|
|
|
this.attemptAdd()
|
2015-02-15 12:27:48 +00:00
|
|
|
|
2015-02-15 23:10:29 +00:00
|
|
|
processSiteCheckSucceed: (response) =>
|
2015-02-18 06:33:06 +00:00
|
|
|
super(response)
|
2015-02-15 23:10:29 +00:00
|
|
|
@add_btn.removeClass('disabled')
|
2015-04-02 03:09:16 +00:00
|
|
|
|
|
|
|
|
if @site_status
|
2015-04-04 06:31:12 +00:00
|
|
|
@recording_sources.push({ url: response.data, recording_id: response.recording_id, recording_title: response.recording_title })
|
2015-04-02 03:09:16 +00:00
|
|
|
if @site_success_callback
|
|
|
|
|
@site_success_callback(@input_div)
|
2015-04-04 06:31:12 +00:00
|
|
|
else
|
|
|
|
|
if @site_fail_callback
|
|
|
|
|
@site_fail_callback(@input_div)
|
2015-02-15 23:10:29 +00:00
|
|
|
|
|
|
|
|
processSiteCheckFail: (response) =>
|
2015-02-18 06:33:06 +00:00
|
|
|
super(response)
|
2015-02-15 23:10:29 +00:00
|
|
|
@add_btn.removeClass('disabled')
|
2015-04-02 03:09:16 +00:00
|
|
|
if @site_fail_callback
|
|
|
|
|
@site_fail_callback(@input_div)
|
2015-02-15 23:10:29 +00:00
|
|
|
|
2015-06-04 23:55:15 +00:00
|
|
|
didBlur: () =>
|
2015-02-15 23:10:29 +00:00
|
|
|
# do nothing, validate on add only
|
2015-02-15 12:27:48 +00:00
|
|
|
|
2015-02-15 23:10:29 +00:00
|
|
|
validateSite: () =>
|
2015-02-15 12:27:48 +00:00
|
|
|
@add_btn.addClass('disabled')
|
2015-02-15 23:10:29 +00:00
|
|
|
super()
|
|
|
|
|
|
|
|
|
|
attemptAdd: () =>
|
2015-02-16 01:59:16 +00:00
|
|
|
if data = this.dataToValidate()
|
|
|
|
|
unless this.containsRecordingUrl(data)
|
|
|
|
|
this.validateSite()
|
2015-02-15 23:10:29 +00:00
|
|
|
|
|
|
|
|
removeRecordingId: (recording_id) =>
|
|
|
|
|
start_len = @recording_sources.length
|
2015-06-04 23:55:15 +00:00
|
|
|
@recording_sources = @recording_sources.filter (src) ->
|
|
|
|
|
src["recording_id"] isnt recording_id.toString()
|
2015-02-15 23:10:29 +00:00
|
|
|
start_len != @recording_sources.length
|
|
|
|
|
|
|
|
|
|
containsRecordingUrl: (url) =>
|
|
|
|
|
vals = $.grep @recording_sources, (src_data) ->
|
2015-06-04 23:55:15 +00:00
|
|
|
src_data['url'] is url
|
|
|
|
|
|
2015-02-15 23:10:29 +00:00
|
|
|
0 < vals.length
|
2015-02-15 12:27:48 +00:00
|
|
|
|
2015-04-01 02:32:41 +00:00
|
|
|
recordingSources: () =>
|
|
|
|
|
@recording_sources
|
|
|
|
|
|