Skip to content

Commit

Permalink
Editor / After adding an element with cardinality 0..1, add action is…
Browse files Browse the repository at this point in the history
… still available (see #1636)
  • Loading branch information
fxprunayre committed Jul 28, 2016
1 parent 015f784 commit 1ff18c7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
4 changes: 2 additions & 2 deletions schemas/iso19139/src/main/plugin/iso19139/layout/layout.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<xsl:param name="schema" select="$schema" required="no"/>
<xsl:param name="labels" select="$labels" required="no"/>
<xsl:param name="overrideLabel" select="''" required="no"/>
<xsl:param name="refToDelete" select="gn:element" required="no"/>
<xsl:param name="refToDelete" required="no"/>

<xsl:variable name="elementName" select="name()"/>
<xsl:variable name="exclusionMatchesParent">
Expand Down Expand Up @@ -325,7 +325,7 @@
<xsl:with-param name="name" select="$theElement/gn:element/@ref"/>
<xsl:with-param name="editInfo" select="$theElement/gn:element"/>
<xsl:with-param name="parentEditInfo"
select="$refToDelete"/>
select="if ($refToDelete) then $refToDelete else gn:element"/>
<!-- TODO: Handle conditional helper -->
<xsl:with-param name="listOfValues" select="$helper"/>
<xsl:with-param name="toggleLang" select="$isMultilingualElementExpanded"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@
import org.fao.geonet.kernel.metadata.StatusActions;
import org.fao.geonet.kernel.metadata.StatusActionsFactory;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.api.records.editing.AjaxEditUtils;
import org.fao.geonet.utils.Xml;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -155,7 +152,7 @@ public Element startEditing(
currTab, session, allRequestParams,
request, metadata.getId(), elMd, metadata.getDataInfo().getSchemaId(),
showValidationErrors,
context, applicationContext, false);
context, applicationContext, false, false);
}


Expand Down Expand Up @@ -313,7 +310,7 @@ public Element saveEdits(
tab, httpSession, forwardedParams,
request, metadata.getId(), elMd, metadata.getDataInfo().getSchemaId(),
withValidationErrors,
context, applicationContext, false);
context, applicationContext, false, false);
}


Expand Down Expand Up @@ -438,7 +435,7 @@ public Element addElement(
allRequestParams.get("currTab"), httpSession, allRequestParams,
request, metadata.getId(), md, metadata.getDataInfo().getSchemaId(),
false,
context, applicationContext, true);
context, applicationContext, true, true);
}


Expand Down Expand Up @@ -615,7 +612,7 @@ private Element buildEditorForm(
String schema,
boolean showValidationErrors,
ServiceContext context,
ApplicationContext applicationContext, boolean isEmbedded) throws Exception {
ApplicationContext applicationContext, boolean isEmbedded, boolean embedded) throws Exception {


UserSession userSession = ApiUtils.getUserSession(session);
Expand All @@ -626,7 +623,7 @@ private Element buildEditorForm(
new Element("currTab").setText(tab));
// This flag is used to generate top tool bar or not
gui.addContent(
new Element("reqService").setText("md.edit"));
new Element("reqService").setText(embedded ? "embedded" : "md.edit"));
String iso3langCode = languageUtils.getIso3langCode(request.getLocales());
gui.addContent(
new Element("language").setText(iso3langCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@
}
}
};
// When adding a new element and the cardinality is 0-1,
// then hide the add control.
// When an element is removed and the cardinality is 0-1,
// then display the add control
var checkAddControls = function(element, isRemoved) {
var addElement = $(element).next();
if (addElement !== undefined) {
var addBlock = addElement.get(0);
if ($(addBlock).hasClass('gn-add-field') &&
$(addBlock).attr('data-gn-cardinality') === '0-1') {
$(addBlock).toggleClass('hidden', isRemoved ? false : true);
}
}
};

// When adding a new element, the down control
// of the previous element must be enabled and
// the up control enabled only if the previous
Expand Down Expand Up @@ -114,6 +129,11 @@
gnCurrentEdit.savedTime = moment();
gnCurrentEdit.saving = status.saving;
};

// Remove XML header
var cleanData = function(data) {
return data.replace(/<\?xml version="1.0".*\?>\n/, '');
};
return {
buildEditUrlPrefix: function(service) {
var params = ['../api/records/',
Expand Down Expand Up @@ -154,7 +174,7 @@
'application/x-www-form-urlencoded'}
}).success(function(data) {

var snippet = $(data);
var snippet = $(cleanData(data));
if (refreshForm) {
scope.refreshEditorForm(snippet);
}
Expand Down Expand Up @@ -315,7 +335,7 @@
.success(function(data) {
// Append HTML snippet after current element - compile Angular
var target = $('#gn-el-' + insertRef);
var snippet = $(data);
var snippet = $(cleanData(data));

if (attribute) {
target.replaceWith(snippet);
Expand All @@ -331,7 +351,8 @@
target[position || 'after'](snippet); // Insert
snippet.slideDown(duration, function() {}); // Slide

// Adapt the move element
// Adapt the add & move element
checkAddControls(snippet);
checkMoveControls(snippet);
}
$compile(snippet)(gnCurrentEdit.formScope);
Expand All @@ -352,7 +373,7 @@
'&child=' + name).success(function(data) {
// Append HTML snippet after current element - compile Angular
var target = $('#gn-el-' + insertRef);
var snippet = $(data);
var snippet = $(cleanData(data));

if (target.hasClass('gn-add-field')) {
target.addClass('gn-extra-field');
Expand All @@ -361,7 +382,7 @@
target[position || 'before'](snippet); // Insert
snippet.slideDown(duration, function() {}); // Slide

// Adapt the move element
checkAddControls(snippet);
checkMoveControls(snippet);

$compile(snippet)(gnCurrentEdit.formScope);
Expand Down Expand Up @@ -424,7 +445,7 @@
}
};

// Adapt the move element
checkAddControls(target.get(0), true);
checkMoveControls(target.get(0));

target.slideUp(duration, function() { $(this).remove();});
Expand Down
24 changes: 22 additions & 2 deletions web/src/main/webapp/xslt/ui-metadata/form-builder.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,23 @@
</xsl:otherwise>
</xsl:choose>

<!-- When building the form with an element having cardinality 0..1,
add a hidden add action in case the element is removed. If removed,
the client app take care of displaying this control. -->
<xsl:if test="$service = 'md.edit' and $parentEditInfo and $parentEditInfo/@min = 0 and $parentEditInfo/@max = 1">
<xsl:variable name="directive" select="gn-fn-metadata:getFieldAddDirective($editorConfig, name())"/>

<xsl:call-template name="render-element-to-add">
<xsl:with-param name="label"
select="gn-fn-metadata:getLabel($schema, name(.), $labels, name(..), '', '')/label"/>
<xsl:with-param name="directive" select="$directive"/>
<xsl:with-param name="childEditInfo" select="$parentEditInfo"/>
<xsl:with-param name="parentEditInfo" select="../gn:element"/>
<xsl:with-param name="isFirst" select="false()"/>
<xsl:with-param name="isHidden" select="true()"/>
<xsl:with-param name="name" select="name()"/>
</xsl:call-template>
</xsl:if>
</xsl:template>


Expand Down Expand Up @@ -668,6 +685,8 @@
<!-- Hide add element if child of an XLink section. -->
<xsl:param name="isDisabled" select="ancestor::node()[@xlink:href]"/>
<xsl:param name="isFirst" required="no" as="xs:boolean" select="true()"/>
<xsl:param name="isHidden" required="no" as="xs:boolean" select="false()"/>
<xsl:param name="name" required="no" as="xs:string" select="''"/>
<xsl:param name="btnLabel" required="no" as="xs:string?" select="''"/>
<xsl:param name="btnClass" required="no" as="xs:string?" select="''"/>

Expand All @@ -681,8 +700,9 @@

<!-- This element is replaced by the content received when clicking add -->
<div
class="form-group gn-field {if ($isRequired) then 'gn-required' else ''} {if ($isFirst) then '' else 'gn-extra-field gn-add-field'} "
class="form-group gn-field {if ($isRequired) then 'gn-required' else ''} {if ($isFirst) then '' else 'gn-extra-field'} gn-add-field {if ($isHidden) then 'hidden' else ''}"
id="gn-el-{$id}"
data-gn-cardinality="{$childEditInfo/@min}-{$childEditInfo/@max}"
data-gn-field-highlight="">
<label class="col-sm-2 control-label"
data-gn-field-tooltip="{$schema}|{$qualifiedName}|{$parentName}|">
Expand Down Expand Up @@ -760,7 +780,7 @@
-->
<a class="btn btn-default"
title="{$i18n/addA} {$label}"
data-gn-click-and-spin="add({$parentEditInfo/@ref}, '{concat(@prefix, ':', @name)}', '{$id}', 'before');">
data-gn-click-and-spin="add({$parentEditInfo/@ref}, '{if ($name != '') then $name else concat(@prefix, ':', @name)}', '{$id}', 'before');">
<i class="{if ($btnClass != '') then $btnClass else 'fa fa-plus'} gn-add"/>
<xsl:if test="$btnLabel != ''">&#160;
<span>
Expand Down

0 comments on commit 1ff18c7

Please sign in to comment.