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
|
|
|
|
2013-01-28 16:57:05 +01:00
|
|
|
userforms.appendToURL = function(url, pathsegmenttobeadded) {
|
|
|
|
var parts = url.match(/([^\?#]*)?(\?[^#]*)?(#.*)?/);
|
|
|
|
for(var i in parts) if(!parts[i]) parts[i] = '';
|
|
|
|
return parts[1] + pathsegmenttobeadded + parts[2] + parts[3];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
$.entwine('udf', function($){
|
2010-09-08 12:35:43 +02:00
|
|
|
|
2012-05-10 07:52:12 +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
|
|
|
|
*/
|
2012-05-07 07:12:44 +02:00
|
|
|
$('div.FieldEditor .MenuHolder .action').entwine({
|
|
|
|
onclick: function(e) {
|
2015-06-30 07:10:20 +02:00
|
|
|
var theform = $("#Form_EditForm"); // edit from page
|
|
|
|
if ( theform.length < 1 ) theform = $("#Form_ItemEditForm"); // edit from modeladmin
|
|
|
|
var form = theform,
|
2013-03-21 11:32:05 +01:00
|
|
|
length = $(".FieldInfo").length + 1,
|
|
|
|
fieldType = $(this).siblings("select").val(),
|
2015-06-30 07:10:20 +02:00
|
|
|
formData = form.serialize()+'&NewID='+ length +"&Type="+ fieldType,
|
2013-03-21 11:32:05 +01:00
|
|
|
fieldEditor = $(this).closest('.FieldEditor');
|
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
e.preventDefault();
|
2013-03-21 11:32:05 +01:00
|
|
|
|
|
|
|
if($("#Fields").hasClass('readonly') || !fieldType) {
|
2012-05-07 07:12:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2013-03-21 11:32:05 +01:00
|
|
|
|
2012-05-07 07:12:44 +02:00
|
|
|
// Due to some very weird behaviout of jquery.metadata, the url have to be double quoted
|
2012-09-11 11:32:09 +02:00
|
|
|
var addURL = fieldEditor.attr('data-add-url').substr(1, fieldEditor.attr('data-add-url').length-2);
|
|
|
|
|
2012-05-07 07:12:44 +02:00
|
|
|
$.ajax({
|
|
|
|
headers: {"X-Pjax" : 'Partial'},
|
|
|
|
type: "POST",
|
|
|
|
url: addURL,
|
|
|
|
data: formData,
|
|
|
|
success: function(data) {
|
|
|
|
$('#Fields_fields').append(data);
|
2013-03-21 11:32:05 +01:00
|
|
|
|
2012-05-07 07:12:44 +02:00
|
|
|
statusMessage(userforms.message('ADDED_FIELD'));
|
2013-03-21 11:32:05 +01:00
|
|
|
|
2012-05-07 07:12:44 +02:00
|
|
|
var name = $("#Fields_fields li.EditableFormField:last").attr("id").split(' ');
|
2012-09-11 11:32:09 +02:00
|
|
|
|
2012-05-07 07:12:44 +02:00
|
|
|
$("#Fields_fields select.fieldOption").append("<option value='"+ name[2] +"'>New "+ name[2] + "</option>");
|
2012-09-11 11:32:09 +02:00
|
|
|
$("#Fields_fields").sortable('refresh');
|
2012-05-07 07:12:44 +02:00
|
|
|
},
|
|
|
|
error: function(e) {
|
2013-10-30 14:21:28 +01:00
|
|
|
alert(ss.i18n._t('GRIDFIELD.ERRORINTRANSACTION'));
|
2012-05-07 07:12:44 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
});
|
2011-03-06 02:20:01 +01:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Delete a field from the user defined form
|
|
|
|
*/
|
|
|
|
$(".EditableFormField .delete").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var text = $(this).parents("li").find(".fieldInfo .text").val();
|
|
|
|
|
|
|
|
// Remove all the rules with relate to this field
|
|
|
|
$("#Fields_fields .customRules select.fieldOption option").each(function(i, element) {
|
|
|
|
if($(element).text() === text) {
|
|
|
|
// check to see if this is selected. If it is then just remove the whole rule
|
|
|
|
if($(element).parent('select.customRuleField').val() === $(element).val()) {
|
|
|
|
$(element).parents('li.customRule').remove();
|
|
|
|
} else {
|
|
|
|
// otherwise remove the option
|
|
|
|
$(element).remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$(this).parents(".EditableFormField").slideUp(function(){$(this).remove()})
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
});
|
2012-05-10 07:52:12 +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
|
|
|
|
*/
|
|
|
|
$('.EditableFormField .fieldInfo .text').entwine({
|
|
|
|
onchange: function(e) {
|
|
|
|
var value = $(this).val();
|
|
|
|
var name = $(this).parents("li").attr("id").split(' ');
|
|
|
|
$("#Fields_fields select.fieldOption option").each(function(i, domElement) {
|
|
|
|
if($(domElement).val() === name[2]) {
|
|
|
|
$(domElement).text(value);
|
|
|
|
}
|
|
|
|
});
|
2009-04-17 04:26:40 +02:00
|
|
|
}
|
2012-05-10 07:52:12 +02:00
|
|
|
});
|
2011-03-06 02:20:01 +01:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Show the more options popdown. Or hide it if we currently have it open
|
|
|
|
*/
|
|
|
|
$(".EditableFormField .moreOptions").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
var parent = $(this).parents(".EditableFormField");
|
|
|
|
if(!parent) {
|
|
|
|
return;
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
2012-05-10 07:52:12 +02:00
|
|
|
|
|
|
|
var extraOptions = parent.children(".extraOptions");
|
|
|
|
if(!extraOptions) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(extraOptions.hasClass('hidden')) {
|
|
|
|
$(this).addClass("showing");
|
|
|
|
$(this).html('Hide options');
|
|
|
|
extraOptions.removeClass('hidden');
|
|
|
|
} else {
|
|
|
|
$(this).removeClass("showing");
|
|
|
|
$(this).html('Show options');
|
|
|
|
extraOptions.addClass('hidden');
|
2009-04-24 00:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2010-05-31 11:43:52 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Add a suboption to a radio field or to a dropdown box for example
|
|
|
|
*/
|
|
|
|
$(".EditableFormField .addableOption").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Give the user some feedback
|
|
|
|
statusMessage(userforms.message('ADDING_OPTION'));
|
|
|
|
|
|
|
|
// variables
|
|
|
|
var options = $(this).parent("li");
|
2013-01-28 16:57:05 +01:00
|
|
|
var action = userforms.appendToURL($("#Form_EditForm").attr("action"), '/field/Fields/addoptionfield');
|
2012-05-10 07:52:12 +02:00
|
|
|
var parent = $(this).attr("rel");
|
2013-09-04 13:34:21 +02:00
|
|
|
var securityID = ($("input[name=SecurityID]").length > 0) ? $("input[name=SecurityID]").first().attr("value") : '';
|
2012-05-10 07:52:12 +02:00
|
|
|
|
|
|
|
// send ajax request to the page
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: action,
|
2013-09-04 13:34:21 +02:00
|
|
|
data: 'Parent='+ parent +'&SecurityID='+securityID,
|
2012-05-10 07:52:12 +02:00
|
|
|
// create a new field
|
|
|
|
success: function(msg){
|
|
|
|
options.before(msg);
|
|
|
|
statusMessage(userforms.message('ADDED_OPTION'));
|
|
|
|
},
|
|
|
|
|
|
|
|
// error creating new field
|
|
|
|
error: function(request, text, error) {
|
|
|
|
statusMessage(userforms.message('ERROR_CREATING_OPTION'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2009-04-17 04:26:40 +02:00
|
|
|
});
|
2010-07-07 11:17:37 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Delete a suboption such as an dropdown option or a
|
|
|
|
* checkbox field
|
|
|
|
*/
|
|
|
|
$(".EditableFormField .deleteOption").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// pass the deleted status onto the element
|
2014-08-06 05:13:52 +02:00
|
|
|
$(this).parents("li:first").find("[type=text]:first").attr("value", "field-node-deleted");
|
|
|
|
$(this).parents("li:first").hide();
|
2011-03-06 03:47:01 +01:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
// Give the user some feedback
|
|
|
|
statusMessage(userforms.message('REMOVED_OPTION'));
|
|
|
|
}
|
|
|
|
});
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Custom Rules Interface
|
|
|
|
*
|
|
|
|
* Hides the input text field if the conditionOption is 'IsBlank' or 'IsNotBlank'
|
|
|
|
*/
|
|
|
|
$("select.conditionOption").entwine({
|
|
|
|
onchange: function() {
|
|
|
|
var valueInput = $(this).siblings(".ruleValue");
|
|
|
|
if($(this).val() && $(this).val() !== "IsBlank" && $(this).val() !== "IsNotBlank") {
|
|
|
|
valueInput.removeClass("hidden");
|
|
|
|
} else {
|
|
|
|
valueInput.addClass("hidden");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2009-04-24 00:52:08 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Delete a custom rule
|
|
|
|
*/
|
|
|
|
$(".customRules .deleteCondition").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$(this).parent("li").fadeOut().remove();
|
|
|
|
}
|
2009-04-24 00:52:08 +02:00
|
|
|
});
|
2009-05-06 06:11:34 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
/**
|
|
|
|
* Adding a custom rule to a given form
|
|
|
|
*/
|
|
|
|
$(".customRules .addCondition").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Give the user some feedback
|
|
|
|
statusMessage(userforms.message('ADDING_RULE'));
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
newRule.children(".customRuleField").each(function(i, domElement) {
|
|
|
|
var currentName = domElement.name.split("][");
|
|
|
|
currentName[3] = currentName[2];
|
|
|
|
currentName[2] = currentRules.children().size() + 1;
|
|
|
|
domElement.name = currentName.join("][");
|
|
|
|
});
|
2009-05-06 06:11:34 +02:00
|
|
|
|
2012-05-10 07:52:12 +02:00
|
|
|
// remove hidden tag
|
|
|
|
newRule.removeClass("hidden");
|
|
|
|
|
|
|
|
// update the fields dropdown
|
2015-02-10 03:32:44 +01:00
|
|
|
var optionChildren = newRule.children("select.fieldOption");
|
|
|
|
optionChildren.empty();
|
2012-05-10 07:52:12 +02:00
|
|
|
|
2015-02-10 03:32:44 +01:00
|
|
|
$("#Fields_fields li.EditableFormField").each(function () {
|
|
|
|
var name = $(this).attr("id").split(' ');
|
|
|
|
var option = $("<option></option>")
|
|
|
|
.attr('value', name[2])
|
|
|
|
.text($(this).find(".text").val());
|
|
|
|
optionChildren
|
|
|
|
.append(option);
|
2012-05-10 07:52:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// append to the list
|
|
|
|
currentRules.append(newRule);
|
|
|
|
}
|
2009-04-24 00:52:08 +02:00
|
|
|
});
|
|
|
|
});
|
2009-04-17 04:26:40 +02:00
|
|
|
});
|
2015-06-30 07:10:20 +02:00
|
|
|
})(jQuery);
|