2015-07-03 23:47:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-08-21 00:17:14 +00:00
|
|
|
angular.module('forms').directive('editFormDirective', ['$rootScope', '$q', '$http', '$timeout', 'TimeCounter', 'Auth', 'FormFields',
|
|
|
|
function ($rootScope, $q, $http, $timeout, TimeCounter, Auth, FormFields) {
|
2015-07-03 23:47:14 +00:00
|
|
|
return {
|
2015-08-18 21:44:36 +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-08-07 21:02:44 +00:00
|
|
|
|
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-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-07-21 23:25:45 +00:00
|
|
|
/*
|
|
|
|
** FormFields (ui-sortable) drag-and-drop configuration
|
|
|
|
*/
|
|
|
|
$scope.dropzone = {
|
|
|
|
handle: ' .handle'
|
2015-08-07 21:02:44 +00:00
|
|
|
};
|
2015-07-21 23:25:45 +00:00
|
|
|
|
2015-07-27 18:11:43 +00:00
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// $scope.draggable = {
|
|
|
|
// connectWith: ".dropzone",
|
|
|
|
// start: function (e, ui) {
|
|
|
|
// // $scope.$apply(function() {
|
|
|
|
// // $scope.dragging = true
|
|
|
|
// // });
|
|
|
|
// $('.dropzone').sortable('refresh');
|
|
|
|
// },
|
|
|
|
// update: function (e, ui) {
|
|
|
|
// var isInDropzone = $(e.target).parentsUntil('.panel-group').hasClass('dropzone');
|
|
|
|
|
|
|
|
// console.log('isInDropzone: '+isInDropzone);
|
|
|
|
// //Disable drag and drop if we aren't in dropzone
|
|
|
|
// if(!isInDropzone){
|
|
|
|
// ui.item.sortable.cancel();
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
// stop: function (e, ui) {
|
|
|
|
// var isInDropzone = $(e.target).parentsUntil('.panel-group').hasClass('dropzone');
|
|
|
|
|
|
|
|
// //Disable drag and drop if we aren't in dropzone
|
|
|
|
// if(isInDropzone){
|
|
|
|
// console.log($(e.target));
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // if (ui.item.sortable.droptarget === undefined) {
|
|
|
|
// // $scope.$apply($scope.dragging = false);
|
|
|
|
// // return;
|
|
|
|
// // }else if (ui.item.sortable.droptarget[0].classList[0] === "dropzone") {
|
|
|
|
// // // run code when item is dropped in the dropzone
|
|
|
|
// // $scope.$apply($scope.dragging = false);
|
|
|
|
// // }else{
|
|
|
|
// // // $scope.$apply($scope.dragging = false);
|
|
|
|
// // }
|
|
|
|
// // console.log('has class .dropzone :'+);
|
|
|
|
// // if ($(e.target).hasClass('dropzone') && ui.item.sortable.droptarget && e.target != ui.item.sortable.droptarget[0] ) {
|
|
|
|
// // // restore original types
|
2015-08-21 00:17:14 +00:00
|
|
|
// // $scope.addField.types = FormFields.types;
|
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-06 04:29:05 +00:00
|
|
|
// console.log($scope.addField.types[i].name === fieldType);
|
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++;
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.addField.types[i].lastAddedID);
|
2015-07-04 03:25:25 +00:00
|
|
|
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-06 05:52:59 +00:00
|
|
|
console.log('\n\n---------\nAdded field CLIENT');
|
|
|
|
console.log(newField);
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// deletes particular field on button click
|
2015-07-04 01:04:39 +00:00
|
|
|
$scope.deleteField = function (hashKey){
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.myform.form_fields);
|
2015-07-07 02:52:55 +00:00
|
|
|
for(var i = 0; i < $scope.myform.form_fields.length; i++){
|
2015-07-21 23:25:45 +00:00
|
|
|
// console.log($scope.myform.form_fields[i].$$hashKey === hashKey);
|
2015-07-07 02:52:55 +00:00
|
|
|
if($scope.myform.form_fields[i].$$hashKey === hashKey){
|
|
|
|
$scope.myform.form_fields.splice(i, 1);
|
2015-07-03 23:47:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-08-05 22:46:33 +00:00
|
|
|
$scope.duplicateField = function (field_index){
|
|
|
|
console.log('field_index: '+field_index);
|
|
|
|
var field = $scope.addNewField(false, $scope.myform.form_fields[field_index].fieldType);
|
|
|
|
field.title = $scope.myform.form_fields[field_index].title;
|
|
|
|
console.log($scope.myform.form_fields[field_index]);
|
|
|
|
|
|
|
|
//Insert field at selected index
|
|
|
|
$scope.myform.form_fields.splice(field_index+1, 0, field);
|
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-08-18 21:44:36 +00:00
|
|
|
// add new Button to the startPage/EndPage
|
|
|
|
$scope.addButton = function (newButtons){
|
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-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-08-18 21:44:36 +00:00
|
|
|
// delete particular Button
|
|
|
|
$scope.deleteButton = function(button){
|
|
|
|
var hashKey = _.chain(button.$$hashKey).words().last().parseInt().value();
|
2015-08-07 22:30:41 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
for(var i = 0; i < $scope.myform.startPage.buttons.length; i++){
|
2015-08-21 00:17:14 +00:00
|
|
|
var currHashKey = _.chain($scope.myform.startPage.buttons[i].$$hashKey).words().last().parseInt().value();
|
2015-08-07 22:30:41 +00:00
|
|
|
|
2015-08-18 21:44:36 +00:00
|
|
|
if(currHashKey === hashKey){
|
|
|
|
$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
|
|
|
|
$scope.addOption = function (field){
|
2015-07-21 23:25:45 +00:00
|
|
|
if(!field.fieldOptions) field.fieldOptions = [];
|
2015-07-03 23:47:14 +00:00
|
|
|
|
|
|
|
var lastOptionID = 0;
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
if(field.fieldOptions[field.fieldOptions.length-1])
|
|
|
|
lastOptionID = field.fieldOptions[field.fieldOptions.length-1].option_id;
|
2015-07-03 23:47:14 +00:00
|
|
|
|
|
|
|
// new option's id
|
|
|
|
var option_id = lastOptionID + 1;
|
|
|
|
|
|
|
|
var newOption = {
|
|
|
|
'option_id' : option_id,
|
|
|
|
'option_title' : 'Option ' + option_id,
|
|
|
|
'option_value' : option_id
|
|
|
|
};
|
|
|
|
|
2015-07-21 23:25:45 +00:00
|
|
|
// put new option into fieldOptions array
|
|
|
|
field.fieldOptions.push(newOption);
|
2015-07-03 23:47:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// delete particular option
|
|
|
|
$scope.deleteOption = function (field, option){
|
2015-07-21 23:25:45 +00:00
|
|
|
for(var i = 0; i < field.fieldOptions.length; i++){
|
|
|
|
if(field.fieldOptions[i].option_id === option.option_id){
|
|
|
|
field.fieldOptions.splice(i, 1);
|
2015-07-03 23:47:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
}
|
|
|
|
]);
|