| 1 |
// $Id: quiz.admin.js,v 1.5 2009/08/13 16:37:12 sivaji Exp $
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Scripts for Quiz administration.
|
| 5 |
*/
|
| 6 |
|
| 7 |
var Quiz = Quiz || {};
|
| 8 |
Quiz.reg = /^([^\[]*)\[type:([\w -_]+), id:([0-9]+)\]$/;
|
| 9 |
|
| 10 |
// Key should be either always or random.
|
| 11 |
Quiz.addQuestion = function (key) {
|
| 12 |
var selector = '#edit-' + key + '-autocomplete';
|
| 13 |
var selectedItem = $(selector).val();
|
| 14 |
var matches = selectedItem.match(Quiz.reg);
|
| 15 |
var statusCode = (key == 'always' ? 1 : 2); // e.g. QUIZ_ALWAYS, QUIZ_RANDOM
|
| 16 |
|
| 17 |
if (!matches || matches.length < 4) {
|
| 18 |
alert('Error: The entered question was not found.');
|
| 19 |
return;
|
| 20 |
}
|
| 21 |
|
| 22 |
var title = Drupal.checkPlain(matches[1]);
|
| 23 |
var type = Drupal.checkPlain(matches[2]);
|
| 24 |
var nid = parseInt(matches[3]);
|
| 25 |
var weight = 0;
|
| 26 |
$('.question-order-weight-' + statusCode).each(function () {
|
| 27 |
var thisVal = parseInt($(this).val());
|
| 28 |
if (thisVal > weight) {
|
| 29 |
weight = thisVal;
|
| 30 |
}
|
| 31 |
});
|
| 32 |
++weight;
|
| 33 |
|
| 34 |
var out = Drupal.theme('addQuestion', nid, type, title, key, weight);
|
| 35 |
$('#questions-order-' + statusCode + ' tr:last').after(out);
|
| 36 |
|
| 37 |
var newRow = $('#questions-order-' + statusCode + ' tr:last').get(0);
|
| 38 |
|
| 39 |
var table = Drupal.tableDrag['questions-order-' + statusCode];
|
| 40 |
table.makeDraggable(newRow);
|
| 41 |
if (table.changed == false) {
|
| 42 |
table.changed = true;
|
| 43 |
$(Drupal.theme('tableDragChangedWarning')).insertAfter(table.table).hide().fadeIn('slow');
|
| 44 |
}
|
| 45 |
$(selector).val(''); // Zero out the value
|
| 46 |
Drupal.attachBehaviors();
|
| 47 |
};
|
| 48 |
|
| 49 |
Drupal.behaviors.attachRemoveAction = function () {
|
| 50 |
$('.rem-link').click(function (e) {
|
| 51 |
var $this = $(this);
|
| 52 |
var remID = $this.parents('tr').find('.question-order-weight').attr('id');
|
| 53 |
|
| 54 |
var matches = remID.match(/edit-weights-([a-zA-Z]+)-([0-9]+)/);
|
| 55 |
if (!matches || matches.length < 3) {
|
| 56 |
return false;
|
| 57 |
}
|
| 58 |
|
| 59 |
var remItem = matches[1] + '-' + matches[2];
|
| 60 |
|
| 61 |
var statusCode = (matches[1] == 'always') ? 1 : 0;
|
| 62 |
|
| 63 |
var remList = $('#edit-remove-from-quiz');
|
| 64 |
var orig = remList.val();
|
| 65 |
remList.val(remItem + ',' + orig);
|
| 66 |
|
| 67 |
$this.parents('tr').remove();
|
| 68 |
|
| 69 |
var table = Drupal.tableDrag['questions-order-' + statusCode];
|
| 70 |
if (!table.changed) {
|
| 71 |
table.changed;
|
| 72 |
$(Drupal.theme('tableDragChangedWarning')).insertAfter(table.table).hide().fadeIn('slow');
|
| 73 |
}
|
| 74 |
|
| 75 |
e.preventDefault();
|
| 76 |
return true;
|
| 77 |
});
|
| 78 |
};
|
| 79 |
|
| 80 |
Drupal.theme.prototype.addQuestion = function (nid, type, title, state, weight) {
|
| 81 |
var fieldID = state + '-' + nid;
|
| 82 |
var pre = (location.search.indexOf('?q=') == 0 ? '?q=' : Drupal.settings.basePath);
|
| 83 |
var nodeLink = pre + Drupal.encodeURIComponent('node/' + nid);
|
| 84 |
var remLink = pre + Drupal.encodeURIComponent('node/' + nid + '/questions/remove');
|
| 85 |
var stateCode = (state == 'always') ? 1: 2;
|
| 86 |
var actions = 'View'.link(nodeLink) + ' | ' + $('Remove'.link('#')).addClass('rem-link').parent().html();
|
| 87 |
|
| 88 |
return '<tr class="draggable">' +
|
| 89 |
'<td>' + title + '<span class="warning tabledrag-changed">*</span></td><td>' + type + '</td><td>' + actions +
|
| 90 |
'</td><td style="display:none">' +
|
| 91 |
'<div id="edit-weights-' + fieldID + '-wrapper" class="form-item">' +
|
| 92 |
'<input id="edit-weights-' + fieldID + '" class="form-text question-order-weight question-order-weight-' + stateCode + '" ' +
|
| 93 |
'type="text" value="' + weight + '" size="3" name="weights[' + fieldID + ']" maxlength="4"/>' +
|
| 94 |
'</div></td></tr>';
|
| 95 |
};
|
| 96 |
|
| 97 |
|
| 98 |
$(document).ready(function () {
|
| 99 |
|
| 100 |
// Stupid hack to get around bug in tableDrag (collapsed tableDrag tables cannot have hidden Weight fields)
|
| 101 |
$('fieldset.collapsible:last>legend>a').click();
|
| 102 |
|
| 103 |
// Effectively bind the autocomplete submit handler to the
|
| 104 |
// "Add question" button's submit handler (for both autocomplete fields).
|
| 105 |
|
| 106 |
|
| 107 |
$('#edit-always-autocomplete,#edit-random-autocomplete').keypress(function (e) {
|
| 108 |
if (e.which == 13) {
|
| 109 |
|
| 110 |
/* We could do something like this....
|
| 111 |
$this = $(this);
|
| 112 |
if ($this.val().length > 0) {
|
| 113 |
Quix.addQuestion();
|
| 114 |
}
|
| 115 |
*/
|
| 116 |
|
| 117 |
// Kill the 'return' handler before the form gets accidentally submitted.
|
| 118 |
e.preventDefault();
|
| 119 |
e.stopPropagation();
|
| 120 |
return true;
|
| 121 |
}
|
| 122 |
});
|
| 123 |
|
| 124 |
});
|