Files
Archetype/app/controllers/controller.js
T

344 lines
10 KiB
JavaScript
Raw Normal View History

2014-03-06 12:12:40 -07:00
angular.module("umbraco").controller("Imulus.ArchetypeController", function ($scope, $http, assetsService, angularHelper, notificationsService, $timeout) {
2014-01-10 17:20:35 -07:00
//$scope.model.value = "";
$scope.model.hideLabel = $scope.model.config.hideLabel == 1;
2014-01-10 17:20:35 -07:00
2014-01-19 08:14:27 -05:00
//get a reference to the current form
2014-01-15 17:31:31 -05:00
var form = angularHelper.getCurrentForm($scope);
2014-01-19 08:14:27 -05:00
//set the config equal to our prevalue config
$scope.model.config = $scope.model.config.archetypeConfig;
2014-01-19 08:14:27 -05:00
//ini the model
$scope.model.value = $scope.model.value || getDefaultModel($scope.model.config);
2014-01-10 17:20:35 -07:00
2014-01-19 08:14:27 -05:00
//ini the render model
2014-01-10 17:20:35 -07:00
$scope.archetypeRenderModel = {};
initArchetypeRenderModel();
//helper to get $eval the labelTemplate
$scope.getFieldsetTitle = function(fieldsetConfigModel, fieldsetIndex) {
var fieldset = $scope.archetypeRenderModel.fieldsets[fieldsetIndex];
2014-01-31 20:56:40 -07:00
var fieldsetConfig = $scope.getConfigFieldsetByAlias(fieldset.alias);
var template = fieldsetConfigModel.labelTemplate;
2014-01-31 20:56:40 -07:00
if (template.length < 1)
return fieldsetConfig.label;
var rgx = /{{(.*?)}}*/g;
var results;
var parsedTemplate = template;
while ((results = rgx.exec(template)) !== null) {
var propertyAlias = results[1];
var propertyValue = $scope.getPropertyValueByAlias(fieldset, propertyAlias);
parsedTemplate = parsedTemplate.replace(results[0], propertyValue);
}
return parsedTemplate;
};
2014-01-10 17:20:35 -07:00
2014-01-19 08:14:27 -05:00
//sort config
$scope.sortableOptions = {
2014-01-10 17:20:35 -07:00
axis: 'y',
cursor: "move",
handle: ".handle",
update: function (ev, ui) {
},
stop: function (ev, ui) {
2014-01-12 14:05:39 -05:00
2014-01-10 17:20:35 -07:00
}
};
2014-01-12 14:05:39 -05:00
//handles a fieldset add
$scope.addRow = function (fieldsetAlias, $index) {
if ($scope.canAdd())
2014-01-10 17:20:35 -07:00
{
if ($scope.model.config.fieldsets)
2014-01-10 17:20:35 -07:00
{
2014-01-16 13:42:08 -05:00
var newFieldset = getEmptyRenderFieldset($scope.getConfigFieldsetByAlias(fieldsetAlias));
2014-01-12 14:05:39 -05:00
if (typeof $index != 'undefined')
{
$scope.archetypeRenderModel.fieldsets.splice($index + 1, 0, newFieldset);
2014-01-12 14:05:39 -05:00
}
else
{
$scope.archetypeRenderModel.fieldsets.push(newFieldset);
2014-01-12 14:05:39 -05:00
}
2014-01-10 17:20:35 -07:00
}
newFieldset.collapse = $scope.model.config.enableCollapsing ? true : false;
$scope.focusFieldset(newFieldset);
2014-01-10 17:20:35 -07:00
}
}
//rather than splice the archetypeRenderModel, we're hiding this and cleaning onFormSubmitting
$scope.removeRow = function ($index) {
if ($scope.canRemove()) {
if (confirm('Are you sure you want to remove this?')) {
$scope.archetypeRenderModel.fieldsets[$index].remove = true;
2014-03-12 08:12:22 -04:00
/*
Touches the model. Not sure why this is needed but without it the Digest doesn't run on a remove row.
*/
$scope.archetypeRenderModel = JSON.parse(JSON.stringify($scope.archetypeRenderModel));
2014-01-10 17:20:35 -07:00
}
}
}
//helpers for determining if a user can do something
$scope.canAdd = function ()
{
if ($scope.model.config.maxFieldsets)
2014-01-10 17:20:35 -07:00
{
return countVisible() < $scope.model.config.maxFieldsets;
2014-01-10 17:20:35 -07:00
}
return true;
}
2014-01-12 14:05:39 -05:00
//helper that returns if an item can be removed
2014-01-10 17:20:35 -07:00
$scope.canRemove = function ()
{
return countVisible() > 1
|| $scope.model.config.startWithAddButton;
2014-01-10 17:20:35 -07:00
}
2014-01-12 14:05:39 -05:00
//helper that returns if an item can be sorted
2014-01-10 17:20:35 -07:00
$scope.canSort = function ()
{
return countVisible() > 1;
}
//helpers for determining if the add button should be shown
$scope.showAddButton = function () {
return $scope.model.config.startWithAddButton
&& countVisible() === 0;
}
2014-01-10 17:20:35 -07:00
//helper, ini the render model from the server (model.value)
function initArchetypeRenderModel() {
$scope.archetypeRenderModel = removeNulls($scope.model.value);
2014-01-16 12:37:01 -05:00
_.each($scope.archetypeRenderModel.fieldsets, function (fieldset)
{
2014-01-16 12:37:01 -05:00
fieldset.remove = false;
fieldset.collapse = false;
fieldset.isValid = true;
});
2014-01-10 17:20:35 -07:00
}
2014-01-12 14:05:39 -05:00
//helper to get the correct fieldset from config
$scope.getConfigFieldsetByAlias = function(alias) {
2014-01-16 12:37:01 -05:00
return _.find($scope.model.config.fieldsets, function(fieldset){
return fieldset.alias == alias;
});
2014-01-12 14:05:39 -05:00
}
//helper to get a property by alias from a fieldset
$scope.getPropertyValueByAlias = function(fieldset, propertyAlias) {
var property = _.find(fieldset.properties, function(p) {
return p.alias == propertyAlias;
});
return (typeof property !== 'undefined') ? property.value : '';
};
2014-01-19 08:14:27 -05:00
//helper for expanding/collapsing fieldsets
$scope.focusFieldset = function(fieldset){
fixDisableSelection();
if (!$scope.model.config.enableCollapsing) {
return;
}
var iniState;
if(fieldset)
{
iniState = fieldset.collapse;
}
2014-01-16 12:37:01 -05:00
_.each($scope.archetypeRenderModel.fieldsets, function(fieldset){
fieldset.collapse = true;
});
2014-03-14 22:28:27 -06:00
if(!fieldset && $scope.archetypeRenderModel.fieldsets.length == 1 && $scope.archetypeRenderModel.fieldsets[0].remove == false)
{
$scope.archetypeRenderModel.fieldsets[0].collapse = false;
return;
}
2014-03-14 22:28:27 -06:00
if(iniState && fieldset)
{
fieldset.collapse = !iniState;
}
}
2014-01-19 08:14:27 -05:00
//ini the fieldset expand/collapse
$scope.focusFieldset();
2014-01-12 14:05:39 -05:00
2014-01-10 17:20:35 -07:00
//developerMode helpers
$scope.archetypeRenderModel.toString = stringify;
2014-01-12 14:05:39 -05:00
//encapsulate stringify (should be built into browsers, not sure of IE support)
2014-01-10 17:20:35 -07:00
function stringify() {
return JSON.stringify(this);
}
//watch for changes
$scope.$watch('archetypeRenderModel', function (v) {
if ($scope.model.config.developerMode) {
2014-01-14 12:10:26 -05:00
console.log(v);
2014-01-10 17:20:35 -07:00
if (typeof v === 'string') {
$scope.archetypeRenderModel = JSON.parse(v);
$scope.archetypeRenderModel.toString = stringify;
}
}
});
//helper to count what is visible
function countVisible()
{
var count = 0;
2014-01-16 12:37:01 -05:00
_.each($scope.archetypeRenderModel.fieldsets, function(fieldset){
if (fieldset.remove == false) {
2014-01-10 17:20:35 -07:00
count++;
}
2014-01-16 12:37:01 -05:00
});
2014-01-10 17:20:35 -07:00
return count;
}
//helper to sync the model to the renderModel
function syncModelToRenderModel()
{
$scope.model.value = { fieldsets: [] };
2014-01-16 12:37:01 -05:00
_.each($scope.archetypeRenderModel.fieldsets, function(fieldset){
2014-02-02 22:58:04 -05:00
var cleanedFieldset = cleanFieldset(fieldset);
if(cleanedFieldset){
2014-02-02 22:58:04 -05:00
$scope.model.value.fieldsets.push(cleanedFieldset);
}
2014-02-01 20:20:09 -05:00
});
}
2014-01-16 12:37:01 -05:00
2014-02-01 20:20:09 -05:00
//helper to remove properties only used during editing that we don't want in the saved data
//also removes properties that are no longer in the config
function cleanFieldset(fieldset)
{
if (typeof fieldset != 'function' && !fieldset.remove){
2014-01-16 12:37:01 -05:00
2014-02-01 20:20:09 -05:00
var fieldsetConfig = $scope.getConfigFieldsetByAlias(fieldset.alias);
//clone and clean
var tempFieldset = JSON.parse(JSON.stringify(fieldset));
delete tempFieldset.remove;
delete tempFieldset.isValid;
delete tempFieldset.collapse;
_.each(tempFieldset.properties, function(property, index){
var propertyConfig = _.find(fieldsetConfig.properties, function(p){
return property.alias == p.alias;
2014-01-16 12:37:01 -05:00
});
2014-02-01 20:20:09 -05:00
//just prune the property
if(propertyConfig){
delete property.isValid;
}
else
2014-02-01 20:20:09 -05:00
{
2014-02-01 20:21:55 -05:00
//need to remove the whole property
2014-02-01 20:20:09 -05:00
tempFieldset.properties.splice(index, 1);
}
});
return tempFieldset;
}
2014-01-10 17:20:35 -07:00
}
// helper to get initial model if none was provided
function getDefaultModel(config) {
if (config.startWithAddButton)
return { fieldsets: [] };
return { fieldsets: [getEmptyRenderFieldset(config.fieldsets[0])] };
}
2014-01-19 08:14:27 -05:00
//helper to add an empty fieldset to the render model
function getEmptyRenderFieldset (fieldsetModel) {
return {alias: fieldsetModel.alias, remove: false, isValid: true, properties: []};
2014-01-12 14:05:39 -05:00
}
//helper to ensure no nulls make it into the model
function removeNulls(model){
if(model.fieldsets){
_.each(model.fieldsets, function(fieldset, index){
if(!fieldset){
model.fieldsets.splice(index, 1);
removeNulls(model);
}
});
return model;
}
}
// Hack for U4-4281 / #61
function fixDisableSelection() {
$timeout(function() {
$('.archetypeEditor .controls')
.bind('mousedown.ui-disableSelection selectstart.ui-disableSelection', function(e) {
e.stopImmediatePropagation();
});
}, 1000);
}
2014-01-19 08:14:27 -05:00
//helper to lookup validity when given a fieldsetIndex and property alias
2014-01-16 12:37:01 -05:00
$scope.getPropertyValidity = function(fieldsetIndex, alias)
{
if($scope.archetypeRenderModel.fieldsets[fieldsetIndex])
{
var property = _.find($scope.archetypeRenderModel.fieldsets[fieldsetIndex].properties, function(property){
return property.alias == alias;
});
2014-01-15 17:31:31 -05:00
}
2014-01-16 12:37:01 -05:00
return (typeof property == 'undefined') ? true : property.isValid;
2014-01-15 17:31:31 -05:00
}
2014-01-10 17:20:35 -07:00
//sync things up on save
$scope.$on("formSubmitting", function (ev, args) {
2014-02-01 20:20:09 -05:00
//test for form; may have to do this differently for nested archetypes
if(!form)
return;
2014-01-15 17:31:31 -05:00
2014-02-02 16:51:32 -05:00
if(form.$invalid)
2014-01-15 17:31:31 -05:00
{
notificationsService.warning("Cannot Save Document", "The document could not be saved because of missing required fields.")
}
else
2014-01-15 17:31:31 -05:00
{
syncModelToRenderModel();
}
2014-01-10 17:20:35 -07:00
});
//custom js
if ($scope.model.config.customJsPath) {
assetsService.loadJs($scope.model.config.customJsPath);
}
2014-01-10 17:20:35 -07:00
//archetype css
assetsService.loadCss("/App_Plugins/Archetype/css/archetype.css");
2014-01-10 17:20:35 -07:00
//custom css
if($scope.model.config.customCssPath)
{
assetsService.loadCss($scope.model.config.customCssPath);
}
});