2014-12-09 14:37:44 -08:00
|
|
|
($ document).on "click", "button#ib-generate-question", ->
|
|
|
|
btn = ($ this)
|
|
|
|
btn.button "loading"
|
|
|
|
$.ajax
|
|
|
|
url: '/ajax/generate_question'
|
|
|
|
type: 'POST'
|
|
|
|
dataType: 'json'
|
|
|
|
success: (data, status, jqxhr) ->
|
|
|
|
if data.success
|
|
|
|
($ "div#entries").prepend(data.render) # TODO: slideDown or something
|
2014-12-21 07:40:10 -08:00
|
|
|
# GitHub issue #26:
|
|
|
|
del_all_btn = ($ "button#ib-delete-all")
|
|
|
|
del_all_btn.removeAttr 'disabled'
|
2014-12-30 07:20:47 -08:00
|
|
|
del_all_btn[0].dataset.ibCount = (Number del_all_btn[0].dataset.ibCount) + 1
|
2014-12-09 14:37:44 -08:00
|
|
|
error: (jqxhr, status, error) ->
|
|
|
|
console.log jqxhr, status, error
|
|
|
|
showNotification "An error occurred, a developer should check the console for details", false
|
|
|
|
complete: (jqxhr, status) ->
|
|
|
|
btn.button "reset"
|
|
|
|
|
2014-12-21 04:41:57 -08:00
|
|
|
|
|
|
|
($ document).on "click", "button#ib-delete-all", ->
|
2014-12-21 06:36:57 -08:00
|
|
|
btn = ($ this)
|
|
|
|
count = btn[0].dataset.ibCount
|
2015-01-03 08:36:55 -08:00
|
|
|
swal
|
2015-01-03 10:43:51 -08:00
|
|
|
title: "Really delete #{count} questions?"
|
|
|
|
text: "They will be gone forever."
|
2015-01-03 08:36:55 -08:00
|
|
|
type: "warning"
|
|
|
|
showCancelButton: true
|
|
|
|
confirmButtonColor: "#DD6B55"
|
2015-01-03 10:43:51 -08:00
|
|
|
confirmButtonText: "Delete"
|
2015-01-03 10:49:47 -08:00
|
|
|
closeOnConfirm: true
|
2015-01-03 08:36:55 -08:00
|
|
|
, ->
|
2014-12-21 04:41:57 -08:00
|
|
|
btn.button "loading"
|
2014-12-21 05:11:35 -08:00
|
|
|
succ = no
|
2014-12-21 04:41:57 -08:00
|
|
|
$.ajax
|
|
|
|
url: '/ajax/delete_all_inbox'
|
|
|
|
type: 'POST'
|
|
|
|
dataType: 'json'
|
|
|
|
success: (data, status, jqxhr) ->
|
|
|
|
if data.success
|
2014-12-21 05:11:35 -08:00
|
|
|
succ = yes
|
2014-12-21 04:41:57 -08:00
|
|
|
entries = ($ "div#entries")
|
|
|
|
entries.slideUp 400, ->
|
|
|
|
entries.html("Nothing to see here.")
|
|
|
|
entries.fadeIn()
|
|
|
|
error: (jqxhr, status, error) ->
|
|
|
|
console.log jqxhr, status, error
|
|
|
|
showNotification "An error occurred, a developer should check the console for details", false
|
|
|
|
complete: (jqxhr, status) ->
|
2014-12-21 05:11:35 -08:00
|
|
|
if succ
|
2014-12-30 07:20:47 -08:00
|
|
|
# and now: a (broken?) re-implementation of Bootstrap's button.js
|
|
|
|
btn.html btn.data('resetText')
|
|
|
|
btn.removeClass 'disabled'
|
2014-12-21 06:36:57 -08:00
|
|
|
btn[0].dataset.ibCount = 0
|
2014-12-21 04:41:57 -08:00
|
|
|
|
|
|
|
|
2014-12-08 06:42:34 -08:00
|
|
|
$(document).on "keydown", "textarea[name=ib-answer]", (evt) ->
|
|
|
|
iid = $(this)[0].dataset.id
|
2015-01-06 15:52:19 -08:00
|
|
|
if evt.keyCode == 13 and (evt.ctrlKey or evt.metaKey)
|
2014-12-08 06:42:34 -08:00
|
|
|
# trigger warning:
|
|
|
|
$("button[name=ib-answer][data-ib-id=#{iid}]").trigger 'click'
|
|
|
|
|
2014-12-21 04:41:57 -08:00
|
|
|
|
2014-12-08 06:42:34 -08:00
|
|
|
$(document).on "click", "button[name=ib-answer]", ->
|
|
|
|
btn = $(this)
|
|
|
|
btn.button "loading"
|
|
|
|
iid = btn[0].dataset.ibId
|
|
|
|
$("textarea[name=ib-answer][data-id=#{iid}]").attr "readonly", "readonly"
|
2014-12-13 06:17:50 -08:00
|
|
|
|
|
|
|
shareTo = []
|
|
|
|
($ "input[type=checkbox][name=ib-share][data-ib-id=#{iid}]:checked").each (i, share) ->
|
|
|
|
shareTo.push share.dataset.service
|
|
|
|
|
2014-12-08 06:42:34 -08:00
|
|
|
$.ajax
|
2014-12-13 06:17:50 -08:00
|
|
|
url: '/ajax/answer'
|
2014-12-08 06:42:34 -08:00
|
|
|
type: 'POST'
|
|
|
|
data:
|
|
|
|
id: iid
|
|
|
|
answer: $("textarea[name=ib-answer][data-id=#{iid}]").val()
|
2014-12-13 06:17:50 -08:00
|
|
|
share: JSON.stringify shareTo
|
2015-01-03 09:40:39 -08:00
|
|
|
inbox: true
|
2014-12-08 06:42:34 -08:00
|
|
|
success: (data, status, jqxhr) ->
|
|
|
|
if data.success
|
|
|
|
$("div.inbox-box[data-id=#{iid}]").slideUp()
|
|
|
|
showNotification data.message, data.success
|
|
|
|
error: (jqxhr, status, error) ->
|
|
|
|
console.log jqxhr, status, error
|
|
|
|
showNotification "An error occurred, a developer should check the console for details", false
|
|
|
|
complete: (jqxhr, status) ->
|
|
|
|
btn.button "reset"
|
|
|
|
$("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly"
|
|
|
|
|
2014-12-21 04:41:57 -08:00
|
|
|
|
2014-12-08 06:42:34 -08:00
|
|
|
$(document).on "click", "button[name=ib-destroy]", ->
|
2015-01-03 10:49:47 -08:00
|
|
|
btn = $(this)
|
2015-01-03 08:36:55 -08:00
|
|
|
swal
|
2015-01-03 10:43:51 -08:00
|
|
|
title: "Really delete?"
|
|
|
|
text: "This question will be gone forever."
|
2015-01-03 08:36:55 -08:00
|
|
|
type: "warning"
|
|
|
|
showCancelButton: true
|
|
|
|
confirmButtonColor: "#DD6B55"
|
2015-01-03 10:43:51 -08:00
|
|
|
confirmButtonText: "Delete"
|
2015-01-03 10:49:47 -08:00
|
|
|
closeOnConfirm: true
|
2015-01-03 08:36:55 -08:00
|
|
|
, ->
|
2014-12-08 06:45:15 -08:00
|
|
|
btn.button "loading"
|
|
|
|
iid = btn[0].dataset.ibId
|
|
|
|
$("textarea[name=ib-answer][data-id=#{iid}]").attr "readonly", "readonly"
|
|
|
|
$.ajax
|
|
|
|
url: '/ajax/delete_inbox'
|
|
|
|
type: 'POST'
|
|
|
|
data:
|
|
|
|
id: iid
|
|
|
|
success: (data, status, jqxhr) ->
|
|
|
|
if data.success
|
|
|
|
$("div.inbox-box[data-id=#{iid}]").slideUp()
|
|
|
|
showNotification data.message, data.success
|
|
|
|
error: (jqxhr, status, error) ->
|
|
|
|
console.log jqxhr, status, error
|
|
|
|
showNotification "An error occurred, a developer should check the console for details", false
|
|
|
|
complete: (jqxhr, status) ->
|
|
|
|
btn.button "reset"
|
2015-01-06 15:52:19 -08:00
|
|
|
$("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly"
|
2015-01-27 09:25:20 -08:00
|
|
|
|
|
|
|
# Toggle button
|
|
|
|
$(document).on "click", "button[name=ib-options]", ->
|
|
|
|
btn = $(this)
|
|
|
|
ibid = btn[0].dataset.ib_id
|
|
|
|
state = btn[0].dataset.state
|
|
|
|
optionBox = $("#ib-options-#{ibid}")
|
|
|
|
|
|
|
|
switch state
|
|
|
|
when 'hidden'
|
|
|
|
optionBox.slideDown()
|
|
|
|
btn[0].dataset.state = 'shown'
|
|
|
|
when 'shown'
|
|
|
|
optionBox.slideUp()
|
|
|
|
btn[0].dataset.state = 'hidden'
|