2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Javascript required to power the user defined forms.
|
|
|
|
*
|
2011-03-06 02:20:01 +01:00
|
|
|
* Rewritten from the prototype FieldEditor and constantly
|
|
|
|
* being refactored to be be less specific on the UDF dom.
|
2009-04-17 04:26:40 +02:00
|
|
|
*/
|
2009-04-15 06:23:43 +02:00
|
|
|
(function($) {
|
|
|
|
$(document).ready(function() {
|
2011-03-06 02:20:01 +01:00
|
|
|
/**
|
|
|
|
* Namespace
|
|
|
|
*/
|
|
|
|
var userforms = userforms || {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Messages from UserForms are translatable using i18n.
|
|
|
|
*/
|
|
|
|
userforms.messages = {
|
|
|
|
CONFIRM_DELETE_ALL_SUBMISSIONS: 'All submissions will be permanently removed. Continue?',
|
|
|
|
ERROR_CREATING_FIELD: 'Error creating field',
|
|
|
|
ADDING_FIELD: 'Adding new field',
|
|
|
|
ADDED_FIELD: 'Added new field',
|
|
|
|
HIDE_OPTIONS: 'Hide options',
|
|
|
|
SHOW_OPTIONS: 'Show options',
|
|
|
|
ADDING_OPTION: 'Adding option',
|
|
|
|
ADDED_OPTION: 'Added option',
|
|
|
|
ERROR_CREATING_OPTION: 'Error creating option',
|
|
|
|
REMOVED_OPTION: 'Removed option',
|
|
|
|
ADDING_RULE: 'Adding rule'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a given translatable string from a passed key. Keys
|
|
|
|
* should be all caps without any spaces.
|
|
|
|
*/
|
|
|
|
userforms.message = function() {
|
|
|
|
en = arguments[1] || userforms.messages[arguments[0]];
|
|
|
|
|
|
|
|
return ss.i18n._t("UserForms."+ arguments[0], en);
|
|
|
|
};
|
|
|
|
|
2010-08-06 01:30:13 +02:00
|
|
|
/**
|
|
|
|
* Update the sortable properties of the form as a function
|
2011-03-06 02:20:01 +01:00
|
|
|
* since the application will need to refresh the UI dynamically based
|
|
|
|
* on a number of factors including when the user adds a page or
|
|
|
|
* swaps between pages
|
|
|
|
*
|
2010-08-06 01:30:13 +02:00
|
|
|
*/
|
2011-03-06 02:20:01 +01:00
|
|
|
userforms.update = function() {
|
2010-10-13 23:07:53 +02:00
|
|
|
$("#Fields_fields").sortable({
|
|
|
|
handle: '.fieldHandler',
|
|
|
|
cursor: 'pointer',
|
|
|
|
items: 'li.EditableFormField',
|
|
|
|
placeholder: 'removed-form-field',
|
|
|
|
opacity: 0.6,
|
|
|
|
revert: 'true',
|
|
|
|
change : function (event, ui) {
|
|
|
|
$("#Fields_fields").sortable('refreshPositions');
|
|
|
|
},
|
|
|
|
update : function (event, ui) {
|
|
|
|
var sort = 1;
|
2010-09-28 04:07:28 +02:00
|
|
|
|
2010-10-13 23:07:53 +02:00
|
|
|
$("li.EditableFormField").each(function() {
|
|
|
|
$(this).find(".sortHidden").val(sort++);
|
|
|
|
});
|
|
|
|
}
|
2010-08-06 01:30:13 +02:00
|
|
|
});
|
|
|
|
|
2010-10-13 23:07:53 +02:00
|
|
|
$(".editableOptions").sortable({
|
|
|
|
handle: '.handle',
|
|
|
|
cursor:'pointer',
|
|
|
|
items: 'li',
|
|
|
|
placeholder: 'removed-form-field',
|
|
|
|
opacity: 0.6,
|
|
|
|
revert: true,
|
|
|
|
change : function (event, ui) {
|
|
|
|
$(this).sortable('refreshPositions');
|
|
|
|
},
|
|
|
|
update : function (event, ui) {
|
|
|
|
var sort = 1;
|
|
|
|
$(".editableOptions li").each(function() {
|
2011-03-06 02:20:01 +01:00
|
|
|
$(this).find(".sortOptionHidden").val(sort++);
|
2010-10-13 23:07:53 +02:00
|
|
|
});
|
|
|
|
}
|
2010-08-06 01:30:13 +02:00
|
|
|
});
|
2011-03-06 02:20:01 +01:00
|
|
|
};
|
2010-10-13 23:07:53 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-05 09:57:34 +01:00
|
|
|
* Workaround for not refreshing the sort.
|
|
|
|
*
|
|
|
|
* TODO: better solution would to not fire this on every hover but needs to
|
|
|
|
* ensure it doesn't have edge cases. The sledge hammer approach.
|
2010-10-13 23:07:53 +02:00
|
|
|
*/
|
2011-12-05 09:57:34 +01:00
|
|
|
$(".fieldHandler, .handle").live('hover', function() {
|
2011-03-06 02:20:01 +01:00
|
|
|
userforms.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kick off the UserForms UI
|
|
|
|
*/
|
|
|
|
userforms.update();
|
|
|
|
|
2010-08-06 01:30:13 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/*--------------------- SUBMISSIONS ------------------------ */
|
|
|
|
|
|
|
|
/**
|
2010-10-12 05:21:48 +02:00
|
|
|
* Delete a given Submission from the form
|
2009-04-17 04:26:40 +02:00
|
|
|
*/
|
2011-03-06 02:20:01 +01:00
|
|
|
$("#userforms-submissions .deleteSubmission").live('click', function(event) {
|
2010-10-12 05:21:48 +02:00
|
|
|
event.preventDefault();
|
2010-09-08 12:35:43 +02:00
|
|
|
|
2010-10-12 05:21:48 +02:00
|
|
|
var deletedSubmission = $(this);
|
2009-04-15 06:23:43 +02:00
|
|
|
$.post($(this).attr('href'), function(data) {
|
2010-09-08 12:35:43 +02:00
|
|
|
deletedSubmission.parents('div.userform-submission').fadeOut();
|
2009-04-15 06:23:43 +02:00
|
|
|
});
|
2010-10-12 05:21:48 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all submissions and fade them out if successful
|
|
|
|
*/
|
2011-03-06 02:20:01 +01:00
|
|
|
$("#userforms-submissions .deleteAllSubmissions").live('click', function(event) {
|
2010-10-12 05:21:48 +02:00
|
|
|
event.preventDefault();
|
|
|
|
|
2011-03-06 02:20:01 +01:00
|
|
|
if(!confirm(userforms.message('CONFIRM_DELETE_ALL_SUBMISSIONS'))) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-12 05:21:48 +02:00
|
|
|
|
2011-03-06 02:20:01 +01:00
|
|
|
var self = this;
|
2010-10-12 05:21:48 +02:00
|
|
|
$.post($(this).attr('href'), function(data) {
|
2011-03-06 02:20:01 +01:00
|
|
|
$(self).parents('#userforms-submissions').children().fadeOut();
|
2010-10-12 05:21:48 +02:00
|
|
|
});
|
|
|
|
|
2009-04-15 06:23:43 +02:00
|
|
|
return false;
|
|
|
|
});
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
/*-------------------- FIELD EDITOR ----------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of a field in the current form
|
|
|
|
* area. the type information should all be on this object
|
|
|
|
*/
|
|
|
|
|
2010-05-21 05:59:01 +02:00
|
|
|
$("div.FieldEditor .MenuHolder .action").live('click',function() {
|
2009-04-27 04:30:32 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
// if this form is readonly...
|
2010-08-06 01:30:13 +02:00
|
|
|
if($("#Fields").hasClass('readonly')) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
// Give the user some feedback
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ADDING_FIELD'));
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
// variables
|
|
|
|
var action = $("#Form_EditForm").attr("action") + '/field/Fields/addfield';
|
|
|
|
var length = $(".FieldInfo").length + 1;
|
2009-04-18 04:49:46 +02:00
|
|
|
var securityID = ($("#SecurityID").length > 0) ? '&SecurityID='+$("#SecurityID").attr("value") : '';
|
2009-04-27 02:20:20 +02:00
|
|
|
var type = $(this).siblings("select").val();
|
2009-04-27 04:30:32 +02:00
|
|
|
|
2010-07-07 11:17:37 +02:00
|
|
|
// send ajax request to the page
|
2009-04-17 04:26:40 +02:00
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: action,
|
|
|
|
data: 'NewID='+ length +"&Type="+ type + securityID,
|
|
|
|
|
|
|
|
// create a new field
|
|
|
|
success: function(msg){
|
|
|
|
$('#Fields_fields').append(msg);
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ADDED_FIELD'));
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2010-08-06 01:30:13 +02:00
|
|
|
// update the internal lists
|
2009-04-24 00:52:08 +02:00
|
|
|
var name = $("#Fields_fields li.EditableFormField:last").attr("id").split(' ');
|
|
|
|
|
2009-06-25 08:55:42 +02:00
|
|
|
$("#Fields_fields select.fieldOption").append("<option value='"+ name[2] +"'>New "+ name[2] + "</option>");
|
2009-04-17 04:26:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// error creating new field
|
|
|
|
error: function(request, text, error) {
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ERROR_CREATING_FIELD'));
|
2009-04-17 04:26:40 +02:00
|
|
|
}
|
|
|
|
});
|
2010-05-31 11:43:52 +02:00
|
|
|
|
2010-10-13 23:07:53 +02:00
|
|
|
$("#Fields_fields").sortable('refresh');
|
2010-05-31 11:43:52 +02:00
|
|
|
|
2009-04-27 02:20:20 +02:00
|
|
|
return false;
|
2009-04-17 04:26:40 +02:00
|
|
|
});
|
2010-05-31 09:57:00 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Upon renaming a field we should go through and rename all the
|
|
|
|
* fields in the select fields to use this new field title. We can
|
|
|
|
* just worry about the title text - don't mess around with the keys
|
|
|
|
*/
|
2010-05-21 05:59:01 +02:00
|
|
|
$('.EditableFormField .fieldInfo .text').live('change', function() {
|
2009-04-24 00:52:08 +02:00
|
|
|
var value = $(this).val();
|
|
|
|
var name = $(this).parents("li").attr("id").split(' ');
|
2011-03-06 02:20:01 +01:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
$("#Fields_fields select.fieldOption option").each(function(i, domElement) {
|
2011-03-06 02:20:01 +01:00
|
|
|
if($(domElement).val() === name[2]) {
|
2009-04-24 00:52:08 +02:00
|
|
|
$(domElement).text(value);
|
|
|
|
}
|
|
|
|
});
|
2010-07-07 11:17:37 +02:00
|
|
|
});
|
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
|
|
|
* Show the more options popdown. Or hide it if we
|
|
|
|
* currently have it open
|
|
|
|
*/
|
2010-05-21 05:59:01 +02:00
|
|
|
$(".EditableFormField .moreOptions").live('click',function() {
|
2009-04-17 04:26:40 +02:00
|
|
|
var parentID = $(this).parents(".EditableFormField");
|
|
|
|
if(parentID) {
|
|
|
|
var extraOptions = parentID.children(".extraOptions");
|
|
|
|
if(extraOptions) {
|
|
|
|
if(extraOptions.hasClass('hidden')) {
|
2011-03-06 02:20:01 +01:00
|
|
|
$(this).html(userforms.message('HIDE_OPTIONS'));
|
2009-04-21 07:10:39 +02:00
|
|
|
$(this).addClass("showing");
|
2009-04-17 04:26:40 +02:00
|
|
|
extraOptions.removeClass('hidden').show();
|
|
|
|
}
|
|
|
|
else {
|
2011-03-06 02:20:01 +01:00
|
|
|
$(this).html(userforms.message('SHOW_OPTIONS'));
|
2009-04-21 07:10:39 +02:00
|
|
|
$(this).removeClass("showing");
|
2009-04-17 04:26:40 +02:00
|
|
|
extraOptions.addClass('hidden').hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a field from the user defined form
|
|
|
|
*/
|
2010-05-21 05:59:01 +02:00
|
|
|
$(".EditableFormField .delete").live('click', function() {
|
2009-04-24 00:52:08 +02:00
|
|
|
// remove all the rules with relate to this field
|
|
|
|
var text = $(this).parents("li").find(".fieldInfo .text").val();
|
2011-03-06 02:20:01 +01:00
|
|
|
|
|
|
|
$("#Fields_fields .customRules select.fieldOption option").each(function(i, ele) {
|
|
|
|
if($(ele).text() === text) {
|
2009-04-24 00:52:08 +02:00
|
|
|
|
|
|
|
// check to see if this is selected. If it is then just remove the whole rule
|
2011-03-06 02:20:01 +01:00
|
|
|
if($(ele).parent('select.customRuleField').val() === $(ele).val()) {
|
|
|
|
$(ele).parents('li.customRule').remove();
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
else {
|
2011-03-06 02:20:01 +01:00
|
|
|
// otherwise remove the option
|
|
|
|
$(ele).remove();
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2010-05-31 11:43:52 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
$(this).parents(".EditableFormField").remove();
|
2010-05-31 11:43:52 +02:00
|
|
|
|
2009-04-20 01:22:22 +02:00
|
|
|
return false;
|
2009-04-17 04:26:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a suboption to a radio field or to a dropdown box
|
|
|
|
* for example
|
|
|
|
*/
|
2010-05-21 05:59:01 +02:00
|
|
|
$(".EditableFormField .addableOption").live('click', function() {
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
// Give the user some feedback
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ADDING_OPTION'));
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
// variables
|
|
|
|
var options = $(this).parent("li");
|
|
|
|
var action = $("#Form_EditForm").attr("action") + '/field/Fields/addoptionfield';
|
|
|
|
var parent = $(this).attr("rel");
|
|
|
|
|
2010-07-07 11:17:37 +02:00
|
|
|
// send ajax request to the page
|
2009-04-17 04:26:40 +02:00
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: action,
|
2009-05-06 05:34:40 +02:00
|
|
|
data: 'Parent='+ parent,
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
// create a new field
|
|
|
|
success: function(msg){
|
|
|
|
options.before(msg);
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ADDED_OPTION'));
|
2009-04-17 04:26:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// error creating new field
|
|
|
|
error: function(request, text, error) {
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ERROR_CREATING_OPTION'));
|
2009-04-17 04:26:40 +02:00
|
|
|
}
|
|
|
|
});
|
2010-07-07 11:17:37 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a suboption such as an dropdown option or a
|
|
|
|
* checkbox field
|
|
|
|
*/
|
2010-05-21 05:59:01 +02:00
|
|
|
$(".EditableFormField .deleteOption").live('click', function() {
|
2009-04-17 04:26:40 +02:00
|
|
|
// pass the deleted status onto the element
|
2009-05-06 05:34:40 +02:00
|
|
|
$(this).parent("li").children("[type=text]").attr("value", "field-node-deleted");
|
|
|
|
$(this).parent("li").hide();
|
2009-04-17 04:26:40 +02:00
|
|
|
|
|
|
|
// Give the user some feedback
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('REMOVED_OPTION'));
|
2009-04-17 04:26:40 +02:00
|
|
|
return false;
|
|
|
|
});
|
2011-03-06 03:47:01 +01:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Custom Rules Interface
|
|
|
|
*/
|
2011-03-06 03:47:01 +01:00
|
|
|
$("body").delegate("select.conditionOption", 'change', function() {
|
2009-04-24 00:52:08 +02:00
|
|
|
var valueInput = $(this).siblings(".ruleValue");
|
2011-03-06 02:20:01 +01:00
|
|
|
|
|
|
|
if($(this).val() && $(this).val() !== "IsBlank" && $(this).val() !== "IsNotBlank") {
|
2009-08-14 06:47:30 +02:00
|
|
|
valueInput.removeClass("hidden");
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
else {
|
2009-08-14 06:47:30 +02:00
|
|
|
valueInput.addClass("hidden");
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
});
|
2010-05-31 09:57:00 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Delete a custom rule
|
|
|
|
*/
|
2010-05-21 05:59:01 +02:00
|
|
|
$(".customRules .deleteCondition").live('click', function() {
|
2009-04-24 00:52:08 +02:00
|
|
|
$(this).parent("li").fadeOut().remove();
|
2009-07-16 00:22:07 +02:00
|
|
|
|
|
|
|
return false;
|
2009-04-24 00:52:08 +02:00
|
|
|
});
|
2010-05-31 09:57:00 +02:00
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
/**
|
|
|
|
* Adding a custom rule to a given form
|
|
|
|
*/
|
2010-06-20 08:31:34 +02:00
|
|
|
$(".customRules .addCondition").live('click', function() {
|
2009-04-24 00:52:08 +02:00
|
|
|
// Give the user some feedback
|
2011-03-06 02:20:01 +01:00
|
|
|
statusMessage(userforms.message('ADDING_RULE'));
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
// get the fields li which to duplicate
|
|
|
|
var currentRules = $(this).parent("li").parent("ul");
|
|
|
|
var defaultRule = currentRules.children("li.hidden:first");
|
|
|
|
var newRule = defaultRule.clone();
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
newRule.children(".customRuleField").each(function(i, domElement) {
|
2009-04-24 00:52:08 +02:00
|
|
|
var currentName = domElement.name.split("][");
|
|
|
|
currentName[3] = currentName[2];
|
2009-05-06 05:34:40 +02:00
|
|
|
currentName[2] = currentRules.children().size() + 1;
|
2009-04-24 00:52:08 +02:00
|
|
|
domElement.name = currentName.join("][");
|
|
|
|
});
|
2009-05-06 06:11:34 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
// remove hidden tag
|
|
|
|
newRule.removeClass("hidden");
|
|
|
|
|
|
|
|
// update the fields dropdown
|
|
|
|
newRule.children("select.fieldOption").empty();
|
2009-05-06 06:11:34 +02:00
|
|
|
|
2009-05-06 05:34:40 +02:00
|
|
|
$("#Fields_fields li.EditableFormField").each(function (i, domElement) {
|
2009-05-06 06:11:34 +02:00
|
|
|
var name = $(domElement).attr("id").split(' ');
|
|
|
|
newRule.children("select.fieldOption").append("<option value='"+ name[2] + "'>"+ $(domElement).find(".text").val() + "</option>");
|
2009-04-24 00:52:08 +02:00
|
|
|
});
|
2009-05-06 05:34:40 +02:00
|
|
|
|
|
|
|
// append to the list
|
|
|
|
currentRules.append(newRule);
|
|
|
|
|
2009-04-24 00:52:08 +02:00
|
|
|
return false;
|
|
|
|
});
|
2009-04-17 04:26:40 +02:00
|
|
|
});
|
2011-03-06 02:20:01 +01:00
|
|
|
})(jQuery);
|