Skip to content

Instantly share code, notes, and snippets.

@torsday
Created January 21, 2015 19:07
Show Gist options
  • Save torsday/62fec31c4f698cce1e54 to your computer and use it in GitHub Desktop.
Save torsday/62fec31c4f698cce1e54 to your computer and use it in GitHub Desktop.
ReadingConnection.js
function ReadingConnection() {
$('.isn_expand_button').click(function () {
if ($('#' + 'reading_connection_' + this.id).is(':visible')) {
$('#' + 'reading_connection_' + this.id).hide();
$(this).addClass("isn_expand_button");
$(this).removeClass("isn_collapse_button");
$(this).html("Expand")
} else {
$('#' + 'reading_connection_' + this.id).show();
$(this).addClass("isn_collapse_button");
$(this).removeClass("isn_expand_button");
$(this).html("Collapse")
}
});
}
function ReadingConnectionShowAll() {
if ($('ul.info_list').length > 0) {
$('.expand_all').on('click', function(e) {
e.preventDefault();
if ($(this).hasClass('active') == true) {
$('.reading_connection_question').hide();
$('.isn_collapse_button').addClass("isn_expand_button");
$('.isn_expand_button').removeClass("isn_collapse_button");
$('.isn_expand_button').html("Expand");
$(this).html('Expand All').removeClass('pressed active');
}
else {
$('.reading_connection_question').show();
$('.isn_collapse_button').removeClass("isn_expand_button");
$('.isn_expand_button').addClass("isn_collapse_button");
$('.isn_expand_button').html("Collapse");
$(this).html('Collapse All').addClass('pressed active');
}
}).bind(this);
}
}
function textareaResize() {
$('textarea').autoResize({
minHeight: 40
}
);
}
function promptForUnsavedWork() {
if ($('#reading_connection_content').length > 0) {
$('a').on('click', function (e) {
$('.notebook_save_button_js').each(function(){
if ($(this).attr('data-dirty') == 'true' && $(this).text() == "Save Your Answer"){
$(this).closest('.connection_question_container').addClass('highlight');
var confirmation_answer = confirm("You have unsaved work. Are you sure you want to leave?");
if (confirmation_answer) {
} else {
e.preventDefault();
}
}
});
$('.notebook_drawing_save_button_js').each(function(){
if ($(this).attr('data-dirty') == 'true'){
$(this).closest('.connection_question_container_image').addClass('highlight');
var confirmation_answer = confirm("You have unsaved work. Are you sure you want to leave?");
if (confirmation_answer) {
} else {
e.preventDefault();
}
}
});
$('.notebook_grid_save_button_js').each(function(){
if ($(this).attr('data-dirty') == 'true'){
$(this).closest('.connection_question_container').addClass('highlight');
var confirmation_answer = confirm("You have unsaved work. Are you sure you want to leave?");
if (confirmation_answer) {
} else {
e.preventDefault();
}
}
});
});
}
}
function writing_prompt_key_listener() {
$(document).bind('keydown', function(e){
var obj_id = e.target.id;
if (obj_id && (obj_id.indexOf("$") == -1)) {
if ($('#' + obj_id).hasClass('writing_question_answer')) {
$('#' + obj_id).closest('.connection_question_container').find('.connection_answer_save_button').attr('data-dirty', 'true');
// $('#' + obj_id).closest('.connection_question_container').addClass('highlight');
}
}
});
}
function check_for_auto_number() {
$('#auto_number_js').on('click', function () {
console.log($(this).attr('data-url'));
var checked;
if ($(this).is(':checked')) {
checked = true;
} else {
checked = false;
}
$.ajax({
type: "PATCH",
url: $(this).data('url'),
data: {
section: {
automatic_notebook_numbering: checked
}
}
})
.fail(function() {
$('#flash').removeClass('notice').addClass('alert').html('<p>Failed to update Notebook automatic numbering.</p>');
$('#flash').stop(true, true).show().delay(3000).fadeOut(500);
})
.done(function() {
$('#flash').removeClass('alert').addClass('notice').html('<p>Successfully updated Notebook automatic numbering.</p>');
$('#flash').stop(true, true).show().delay(3000).fadeOut(500);
});
});
}
$(textareaResize);
$(ReadingConnection);
$(ReadingConnectionShowAll);
$(promptForUnsavedWork);
$(writing_prompt_key_listener());
$(document).ready(function () {
if ($('#auto_number_js').length) {
check_for_auto_number();
}
});
$(document).bind('page:change', function () {
textareaResize();
ReadingConnection();
ReadingConnectionShowAll();
promptForUnsavedWork();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment