2015-07-03 23:47:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-11-12 22:24:26 +00:00
|
|
|
angular.module('forms').directive('editFormDirective', ['$rootScope', 'FormFields',
|
|
|
|
function ($rootScope, FormFields) {
|
2015-07-03 23:47:14 +00:00
|
|
|
return {
|
2015-08-25 21:33:52 +00:00
|
|
|
templateUrl: 'modules/forms/views/directiveViews/form/edit-form.client.view.html',
|
2015-07-06 04:29:05 +00:00
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
2015-07-07 02:52:55 +00:00
|
|
|
myform:'=',
|
2015-07-06 04:29:05 +00:00
|
|
|
},
|
2015-07-03 23:47:14 +00:00
|
|
|
controller: function($scope){
|
2015-10-30 18:40:02 +00:00
|
|
|
var field_ids = _($scope.myform.form_fields).pluck('_id');
|
|
|
|
for(var i=0; i<field_ids.length; i++){
|
|
|
|
$scope.myform.plugins.oscarhost.settings.fieldMap[field_ids[i]] = null;
|
|
|
|
}
|
2015-08-05 22:24:24 +00:00
|
|
|
/*
|
|
|
|
** Initialize scope with variables
|
|
|
|
*/
|
|
|
|
//Populate AddField with all available form field types
|
|
|
|
$scope.addField = {};
|
2015-08-21 00:17:14 +00:00
|
|
|
$scope.addField.types = FormFields.types;
|
2015-08-05 22:24:24 +00:00
|
|
|
|
|
|
|
$scope.addField.types.forEach(function(type){
|
|
|
|
type.lastAddedID = 1;
|
|
|
|
return type;
|
|
|
|
});
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.lastButtonID = 0;
|
|
|
|
|
2015-08-07 21:02:44 +00:00
|
|
|
// Accordion settings
|
2015-08-05 22:24:24 +00:00
|
|
|
$scope.accordion = {};
|
|
|
|
$scope.accordion.oneAtATime = true;
|
|
|
|
|
2015-07-06 04:29:05 +00:00
|
|
|
//Populate local scope with rootScope methods/variables
|
|
|
|
$scope.update = $rootScope.update;
|
|
|
|
|
2015-10-30 18:40:02 +00:00
|
|
|
//Many-to-many Select for Mapping OscarhostFields -> FormFields
|
|
|
|
$scope.oscarFieldsLeft = function(field_id){
|
|
|
|
|
|
|
|
if($scope.myform && $scope.myform.plugins.oscarhost.settings.validFields.length > 0){
|
|
|
|
if(!$scope.myform.plugins.oscarhost.settings.fieldMap) $scope.myform.plugins.oscarhost.settings.fieldMap = {};
|
|
|
|
|
|
|
|
var oscarhostFields = $scope.myform.plugins.oscarhost.settings.validFields;
|
|
|
|
var currentFields = _($scope.myform.plugins.oscarhost.settings.fieldMap).invert().keys().value();
|
|
|
|
|
|
|
|
if( $scope.myform.plugins.oscarhost.settings.fieldMap.hasOwnProperty(field_id) ){
|
|
|
|
currentFields = _(currentFields).difference($scope.myform.plugins.oscarhost.settings.fieldMap[field_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get all oscarhostFields that haven't been mapped to a formfield
|
|
|
|
return _(oscarhostFields).difference(currentFields).value();
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
/*
|
|
|
|
** FormFields (ui-sortable) drag-and-drop configuration
|
|
|
|
*/
|
|
|
|
$scope.dropzone = {
|
2015-11-06 17:25:30 +00:00
|
|
|
handle: ' .handle',
|
|
|
|
containment: '.dropzoneContainer',
|
|
|
|
cursor: 'grabbing',
|
2015-08-07 21:02:44 +00:00
|
|
|
};
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
/*
|
|
|
|
** Field CRUD Methods
|
|
|
|
*/
|
2015-07-09 22:21:29 +00:00
|
|
|
// Add a new field
|
2015-08-21 00:17:14 +00:00
|
|
|
$scope.addNewField = function(modifyForm, fieldType){
|
2015-07-03 23:47:14 +00:00
|
|
|
|
|
|
|
// incr field_id counter
|
|
|
|
$scope.addField.lastAddedID++;
|
2015-07-04 03:25:25 +00:00
|
|
|
var fieldTitle;
|
2015-07-09 22:15:34 +00:00
|
|
|
|
2015-07-04 03:25:25 +00:00
|
|
|
for(var i = 0; i < $scope.addField.types.length; i++){
|
2015-07-27 18:11:43 +00:00
|
|
|
if($scope.addField.types[i].name === fieldType){
|
2015-07-04 03:25:25 +00:00
|
|
|
$scope.addField.types[i].lastAddedID++;
|
|
|
|
fieldTitle = $scope.addField.types[i].value+$scope.addField.types[i].lastAddedID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-03 23:47:14 +00:00
|
|
|
var newField = {
|
2015-08-21 00:17:14 +00:00
|
|
|
title: fieldTitle,
|
|
|
|
fieldType: fieldType,
|
|
|
|
fieldValue: '',
|
|
|
|
required: true,
|
|
|
|
disabled: false,
|
|
|
|
deletePreserved: false
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
2015-08-25 21:33:52 +00:00
|
|
|
// console.log('\n\n---------\nAdded field CLIENT');
|
|
|
|
// console.log(newField);
|
2015-10-30 18:40:02 +00:00
|
|
|
// newField._id = _.uniqueId();
|
2015-08-06 05:52:59 +00:00
|
|
|
|
2015-07-03 23:47:14 +00:00
|
|
|
// put newField into fields array
|
2015-08-21 00:17:14 +00:00
|
|
|
if(modifyForm){
|
2015-08-05 22:46:33 +00:00
|
|
|
$scope.myform.form_fields.push(newField);
|
|
|
|
}
|
2015-08-21 00:17:14 +00:00
|
|
|
return newField;
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
// Delete particular field on button click
|
|
|
|
$scope.deleteField = function (field_index){
|
2015-11-06 17:25:30 +00:00
|
|
|
|
2015-10-30 18:40:02 +00:00
|
|
|
//Delete field from field map
|
2015-11-23 21:06:02 +00:00
|
|
|
var currFieldId = $scope.myform.form_fields[field_index]._id;
|
2015-11-23 19:19:02 +00:00
|
|
|
if($scope.myform.hasOwnProperty('plugins.oscarhost.baseUrl')) delete $scope.myform.plugins.oscarhost.settings.fieldMap[currFieldId];
|
2015-10-30 18:40:02 +00:00
|
|
|
|
|
|
|
//Delete field
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.myform.form_fields.splice(field_index, 1);
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
2015-08-05 22:46:33 +00:00
|
|
|
$scope.duplicateField = function (field_index){
|
2015-11-06 17:25:30 +00:00
|
|
|
var currField = _.cloneDeep($scope.myform.form_fields[field_index]);
|
2015-11-13 00:52:14 +00:00
|
|
|
currField._id = 'cloned'+_.uniqueId();
|
|
|
|
currField.title += ' copy';
|
2015-08-05 22:46:33 +00:00
|
|
|
|
|
|
|
//Insert field at selected index
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.myform.form_fields.splice(field_index+1, 0, currField);
|
2015-07-04 01:04:39 +00:00
|
|
|
};
|
|
|
|
|
2015-08-07 22:30:41 +00:00
|
|
|
|
|
|
|
/*
|
2015-08-18 21:44:36 +00:00
|
|
|
** startPage Button Methods
|
2015-08-07 22:30:41 +00:00
|
|
|
*/
|
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
// add new Button to the startPage
|
2015-08-25 21:33:52 +00:00
|
|
|
$scope.addButton = function(){
|
2015-08-07 22:30:41 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
var newButton = {};
|
|
|
|
newButton.bgColor = '#ddd';
|
|
|
|
newButton.color = '#ffffff';
|
|
|
|
newButton.text = 'Button';
|
2015-11-23 19:19:02 +00:00
|
|
|
newButton._id = Math.floor(100000*Math.random());
|
2015-08-07 22:30:41 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
$scope.myform.startPage.buttons.push(newButton);
|
|
|
|
};
|
2015-08-07 22:30:41 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
// delete particular Button from startPage
|
2015-08-18 21:44:36 +00:00
|
|
|
$scope.deleteButton = function(button){
|
2015-08-25 21:33:52 +00:00
|
|
|
var currID;
|
2015-08-18 21:44:36 +00:00
|
|
|
for(var i = 0; i < $scope.myform.startPage.buttons.length; i++){
|
2015-11-23 19:19:02 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
currID = $scope.myform.startPage.buttons[i]._id;
|
|
|
|
console.log(currID);
|
2015-08-07 22:30:41 +00:00
|
|
|
|
2015-08-25 21:33:52 +00:00
|
|
|
if(currID === button._id){
|
2015-08-18 21:44:36 +00:00
|
|
|
$scope.myform.startPage.buttons.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-07 22:30:41 +00:00
|
|
|
};
|
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
/*
|
|
|
|
** Field Option Methods
|
|
|
|
*/
|
|
|
|
|
2015-07-03 23:47:14 +00:00
|
|
|
// add new option to the field
|
2015-11-23 19:19:02 +00:00
|
|
|
$scope.addOption = function(field_index){
|
|
|
|
var currField = $scope.myform.form_fields[field_index];
|
2016-04-17 02:45:17 +00:00
|
|
|
console.log(field_index);
|
2016-04-16 21:56:01 +00:00
|
|
|
console.log(currField);
|
|
|
|
|
|
|
|
if(currField.fieldType === 'checkbox' || currField.fieldType === 'dropdown' || currField.fieldType === 'radio'){
|
2015-11-23 19:19:02 +00:00
|
|
|
if(!currField.fieldOptions) $scope.myform.form_fields[field_index].fieldOptions = [];
|
2015-07-03 23:47:14 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
var lastOptionID = 0;
|
2015-07-03 23:47:14 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
if(currField.fieldOptions[currField.fieldOptions.length-1]){
|
|
|
|
lastOptionID = currField.fieldOptions[currField.fieldOptions.length-1].option_id;
|
|
|
|
}
|
2015-07-03 23:47:14 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
// new option's id
|
|
|
|
var option_id = lastOptionID + 1;
|
|
|
|
|
|
|
|
var newOption = {
|
|
|
|
'option_id' : Math.floor(100000*Math.random()),
|
|
|
|
'option_title' : 'Option '+lastOptionID,
|
|
|
|
'option_value' : 'Option ' +lastOptionID,
|
|
|
|
};
|
2015-07-03 23:47:14 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
// put new option into fieldOptions array
|
|
|
|
$scope.myform.form_fields[field_index].fieldOptions.push(newOption);
|
|
|
|
}
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// delete particular option
|
2015-11-23 19:19:02 +00:00
|
|
|
$scope.deleteOption = function (field_index, option){
|
|
|
|
var currField = $scope.myform.form_fields[field_index];
|
|
|
|
|
|
|
|
if(currField.fieldType === 'checkbox' || currField.fieldType === 'dropdown' || currField.fieldType === 'radio'){
|
|
|
|
for(var i = 0; i < currField.fieldOptions.length; i++){
|
|
|
|
if(currField.fieldOptions[i].option_id === option.option_id){
|
|
|
|
|
|
|
|
$scope.myform.form_fields[field_index].fieldOptions.splice(i, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2015-07-03 23:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// decides whether field options block will be shown (true for dropdown and radio fields)
|
|
|
|
$scope.showAddOptions = function (field){
|
2015-08-06 05:52:59 +00:00
|
|
|
if(field.fieldType === 'dropdown' || field.fieldType === 'checkbox' || field.fieldType === 'radio'){
|
2015-07-03 23:47:14 +00:00
|
|
|
return true;
|
2015-07-21 23:25:45 +00:00
|
|
|
} else {
|
2015-07-03 23:47:14 +00:00
|
|
|
return false;
|
2015-07-21 23:25:45 +00:00
|
|
|
}
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
},
|
2015-07-06 04:29:05 +00:00
|
|
|
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
|
|
|
}
|
2016-04-16 21:56:01 +00:00
|
|
|
]);
|