2015-07-03 23:47:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-28 23:47:50 +00:00
|
|
|
angular.module('forms').directive('editFormDirective', ['$rootScope', 'FormFields',
|
2016-04-27 20:02:01 +00:00
|
|
|
function ($rootScope, FormFields) {
|
2015-07-03 23:47:14 +00:00
|
|
|
return {
|
2016-05-05 19:12:40 +00:00
|
|
|
templateUrl: 'modules/forms/admin/views/directiveViews/form/edit-form.client.view.html',
|
2015-07-06 04:29:05 +00:00
|
|
|
restrict: 'E',
|
|
|
|
scope: {
|
2016-04-29 06:00:41 +00:00
|
|
|
myform:'='
|
2015-07-06 04:29:05 +00:00
|
|
|
},
|
2015-07-03 23:47:14 +00:00
|
|
|
controller: function($scope){
|
2016-06-08 23:12:48 +00:00
|
|
|
|
|
|
|
console.log($scope.myform);
|
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
|
|
|
|
*/
|
2016-05-17 01:27:37 +00:00
|
|
|
//Setup UI-Sortable
|
|
|
|
$scope.sortableOptions = {
|
|
|
|
appendTo: '.dropzone',
|
2016-05-17 01:53:17 +00:00
|
|
|
cursorAt: { top: -155 },
|
|
|
|
forceHelperSize: true,
|
|
|
|
forcePlaceholderSize: true
|
2016-05-17 01:27:37 +00:00
|
|
|
};
|
|
|
|
|
2016-06-01 01:06:45 +00:00
|
|
|
/*
|
|
|
|
** Setup Angular-Input-Star Shape Dropdown
|
|
|
|
*/
|
|
|
|
//Populate Name to Font-awesomeName Conversion Map
|
|
|
|
$scope.select2FA = {
|
|
|
|
'Heart': 'Heart',
|
|
|
|
'Star': 'Star',
|
|
|
|
'thumbs-up': 'Thumbs Up',
|
|
|
|
'thumbs-down':'Thumbs Down',
|
|
|
|
'Circle': 'Circle',
|
|
|
|
'Square':'Square',
|
|
|
|
'Check Circle': 'Checkmark',
|
|
|
|
'Smile Outlined': 'Smile',
|
|
|
|
'Hourglass': 'Hourglass',
|
|
|
|
'bell': 'Bell',
|
|
|
|
'Paper Plane': 'Paper Plane',
|
|
|
|
'Comment': 'Chat Bubble',
|
|
|
|
'Trash': 'Trash Can'
|
|
|
|
};
|
2016-05-17 01:27:37 +00:00
|
|
|
|
2015-08-05 22:24:24 +00:00
|
|
|
//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]);
|
2016-04-29 06:00:41 +00:00
|
|
|
}
|
2015-10-30 18:40:02 +00:00
|
|
|
|
|
|
|
//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
|
|
|
|
*/
|
2016-06-05 02:01:39 +00:00
|
|
|
$scope.dropzone = {
|
|
|
|
handle: '.handle',
|
|
|
|
containment: '.dropzoneContainer',
|
|
|
|
cursor: 'grabbing'
|
|
|
|
};
|
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
|
|
|
|
2016-06-04 07:00:31 +00:00
|
|
|
// increment lastAddedID counter
|
2015-07-03 23:47:14 +00:00
|
|
|
$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++){
|
2016-04-29 06:00:41 +00:00
|
|
|
if($scope.addField.types[i].name === fieldType){
|
2015-07-04 03:25:25 +00:00
|
|
|
$scope.addField.types[i].lastAddedID++;
|
2016-04-29 06:00:41 +00:00
|
|
|
fieldTitle = $scope.addField.types[i].value+$scope.addField.types[i].lastAddedID;
|
2015-07-04 03:25:25 +00:00
|
|
|
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
|
|
|
};
|
2016-04-29 06:00:41 +00:00
|
|
|
|
2016-06-01 01:06:45 +00:00
|
|
|
if($scope.showAddOptions(newField)){
|
|
|
|
newField.fieldOptions = [];
|
|
|
|
newField.fieldOptions.push({
|
2016-06-05 00:48:49 +00:00
|
|
|
'option_id' : Math.floor(100000*Math.random()), //Generate pseudo-random option id
|
2016-06-01 01:06:45 +00:00
|
|
|
'option_title' : 'Option 0',
|
|
|
|
'option_value' : 'Option 0'
|
|
|
|
});
|
|
|
|
}
|
2016-06-04 07:00:31 +00:00
|
|
|
|
2016-06-05 00:48:49 +00:00
|
|
|
|
2015-08-21 00:17:14 +00:00
|
|
|
if(modifyForm){
|
2016-06-05 00:48:49 +00:00
|
|
|
//Add newField to form_fields array
|
2015-08-05 22:46:33 +00:00
|
|
|
$scope.myform.form_fields.push(newField);
|
|
|
|
}
|
2016-04-29 06:00:41 +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){
|
2016-04-29 06:00:41 +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-06-01 01:06:45 +00:00
|
|
|
//console.log(field_index);
|
|
|
|
//console.log(currField);
|
2016-04-29 06:00:41 +00:00
|
|
|
|
2016-04-16 21:56:01 +00:00
|
|
|
if(currField.fieldType === 'checkbox' || currField.fieldType === 'dropdown' || currField.fieldType === 'radio'){
|
2016-06-01 01:06:45 +00:00
|
|
|
if(!currField.fieldOptions){
|
|
|
|
$scope.myform.form_fields[field_index].fieldOptions = [];
|
|
|
|
}
|
2015-07-03 23:47:14 +00:00
|
|
|
|
2016-06-01 01:06:45 +00:00
|
|
|
var lastOptionID = $scope.myform.form_fields[field_index].fieldOptions.length+1;
|
2015-07-03 23:47:14 +00:00
|
|
|
|
2015-11-23 19:19:02 +00:00
|
|
|
// new option's id
|
|
|
|
|
|
|
|
var newOption = {
|
|
|
|
'option_id' : Math.floor(100000*Math.random()),
|
|
|
|
'option_title' : 'Option '+lastOptionID,
|
2016-04-29 06:00:41 +00:00
|
|
|
'option_value' : 'Option ' +lastOptionID
|
2015-11-23 19:19:02 +00:00
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
2016-06-01 01:06:45 +00:00
|
|
|
// decides whether field options block will be shown (true for dropdown and radio fields)
|
|
|
|
$scope.showRatingOptions = function (field){
|
|
|
|
if(field.fieldType === 'rating'){
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2016-04-29 06:00:41 +00:00
|
|
|
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
|
|
|
}
|
2016-04-16 21:56:01 +00:00
|
|
|
]);
|