Skip to content

Commit

Permalink
Don't show add new when a new record
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdurow committed Jul 14, 2015
1 parent f4a4079 commit 4cf2b93
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 38 deletions.
26 changes: 21 additions & 5 deletions SparkleXrmSamples/ConnectionsUI/ClientUI/View/ConnectionsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SparkleXrm.GridEditor;
using System;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using Xrm;
using Xrm.Sdk;
Expand All @@ -37,9 +38,8 @@ public static void Init()
int lcid = (int)OrganizationServiceProxy.GetUserSettings().UILanguageId;

LocalisedContentLoader.FallBackLCID = 0; // Always get a resource file
LocalisedContentLoader.SupportedLCIDs.Add(1033); // English
LocalisedContentLoader.SupportedLCIDs.Add(1031); // German

LocalisedContentLoader.SupportedLCIDs.Add(0); // Allow all LCIDs

LocalisedContentLoader.LoadContent("con_/js/Res.metadata.js", lcid, delegate()
{
InitLocalisedContent();
Expand All @@ -65,7 +65,8 @@ private static void InitLocalisedContent()
#else
parameters = PageEx.GetWebResourceData(); // The allowed lookup types for the connections - e.g. account, contact, opportunity. This must be passed as a data parameter to the webresource 'account=name&contact=fullname&opportunity=name
id = ParentPage.Data.Entity.GetId();
logicalName = ParentPage.Data.Entity.GetEntityName();
logicalName = ParentPage.Data.Entity.GetEntityName();
ParentPage.Data.Entity.AddOnSave(CheckForSaved);
#endif
EntityReference parent = new EntityReference(new Guid(id), logicalName, null);
string entities = "account,contact,opportunity,systemuser";
Expand Down Expand Up @@ -93,7 +94,7 @@ private static void InitLocalisedContent()
// Get the columsn for the view
EntityQuery connectionViews = queryParser.EntityLookup["connection"];
FetchQuerySettings view = connectionViews.Views[connectionViews.Views.Keys[0]];
string fetchXml = queryParser.GetFetchXmlParentFilter(view, parent, "record1id");
string fetchXml = queryParser.GetFetchXmlParentFilter(view, "record1id");
vm = new ConnectionsViewModel(parent, entities.Split(","), pageSize, fetchXml);

// Bind Connections grid
Expand Down Expand Up @@ -144,6 +145,21 @@ private static void InitLocalisedContent()
});
}


private static void CheckForSaved()
{
// Check if we have the id yet
EntityReference parent = new EntityReference(new Guid(ParentPage.Data.Entity.GetId()), ParentPage.Data.Entity.GetEntityName(), null);
if (ParentPage.Ui.GetFormType() != FormTypes.Create && parent.Id != null)
{
vm.ParentRecordId.SetValue(parent);
vm.Search();
}
else
{
Window.SetTimeout(CheckForSaved, 1000);
}
}
private static void OverrideMetadata()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//

using ClientUI.Model;
using ClientUI.ViewModels;
using jQueryApi;
using KnockoutApi;
using Slick;
Expand All @@ -18,6 +19,7 @@ namespace ClientUI.ViewModel
public class ConnectionsViewModel : ViewModelBase
{
#region Fields

[PreserveCase]
public EntityDataViewModel Connections;
[PreserveCase]
Expand All @@ -27,24 +29,25 @@ public class ConnectionsViewModel : ViewModelBase
[PreserveCase]
public Observable<String> ErrorMessage = Knockout.Observable<string>();
[PreserveCase]
public Observable<bool> AllowAddNew = Knockout.Observable<bool>(true);
private EntityReference ParentRecordId;
public DependentObservable<bool> AllowAddNew;
public Observable<EntityReference> ParentRecordId = Knockout.Observable<EntityReference>();
private string _viewFetchXml;
#endregion

#region Constructors
public ConnectionsViewModel(EntityReference parentRecordId, string[] connectToTypes, int pageSize, string viewFetchXml)
{
Connections = new EntityDataViewModel(pageSize, typeof(Connection), true);
ParentRecordId = parentRecordId;
ParentRecordId.SetValue(parentRecordId);
_viewFetchXml = viewFetchXml;
ObservableConnection connection = new ObservableConnection(connectToTypes);
connection.Record2Id.SetValue(ParentRecordId);
connection.Record2Id.SetValue(parentRecordId);
ConnectionEdit = (Observable<ObservableConnection>)ValidatedObservableFactory.ValidatedObservable(connection);

Connections.OnDataLoaded.Subscribe(Connections_OnDataLoaded);
ConnectionEdit.GetValue().OnSaveComplete += ConnectionsViewModel_OnSaveComplete;
ObservableConnection.RegisterValidation(Connections.ValidationBinder);
AllowAddNew = Knockout.DependentObservable<bool>(AllowAddNewComputed);
}
#endregion

Expand Down Expand Up @@ -125,6 +128,7 @@ private void ConnectionsViewModel_OnSaveComplete(string result)
#region Commands
public void Search()
{
string parentRecordId = ParentRecordId.GetValue().Id.ToString().Replace("{", "").Replace("}", "") ;
if (_viewFetchXml == null)
{
Connections.FetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' returntotalrecordcount='true' no-lock='true' distinct='false' count='{0}' paging-cookie='{1}' page='{2}'>
Expand All @@ -136,15 +140,15 @@ public void Search()
<attribute name='connectionid' />
<filter type='and'>
<condition attribute='record2id' operator='eq' value='" + ParentRecordId.Id.ToString().Replace("{", "").Replace("}", "") + @"' />
<condition attribute='record2id' operator='eq' value='" + parentRecordId + @"' />
</filter>
{3}
</entity>
</fetch>";
}
else
{
Connections.FetchXml = _viewFetchXml;
Connections.FetchXml = _viewFetchXml.Replace(QueryParser.ParentRecordPlaceholder, parentRecordId);

}
Connections.Refresh();
Expand All @@ -160,6 +164,7 @@ public void RoleSearchCommand(string term, Action<EntityCollection> callback)
[PreserveCase]
public void AddNewCommand()
{
ConnectionEdit.GetValue().Record2Id.SetValue(ParentRecordId.GetValue());
ErrorMessage.SetValue(null);
ConnectionEdit.GetValue().AddNewVisible.SetValue(true);

Expand Down Expand Up @@ -236,5 +241,13 @@ public void DeleteCommand(object data, jQueryEvent e)
}
#endregion

#region Computed Observables
public bool AllowAddNewComputed()
{
EntityReference parent = ParentRecordId.GetValue();
return parent != null && parent.Id != null && parent.Id.Value != null && parent.Id.Value.Length > 0;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace ClientUI.ViewModels

public class QueryParser
{
public const string ParentRecordPlaceholder = "#ParentRecordPlaceholder#";
public IEnumerable<string> Entities;
public Dictionary<string, EntityQuery> EntityLookup = new Dictionary<string, EntityQuery>();
Dictionary<string, EntityQuery> AliasEntityLookup = new Dictionary<string, EntityQuery>();
Expand Down Expand Up @@ -375,7 +376,7 @@ public string GetFetchXmlForQuery(string entityLogicalName, string queryName, st
fetchXml = fetchXml.Replace("#Query#", XmlHelper.Encode(searchTerm));
return fetchXml;
}
public string GetFetchXmlParentFilter(FetchQuerySettings query, EntityReference parentId, string parentAttribute)
public string GetFetchXmlParentFilter(FetchQuerySettings query, string parentAttribute)
{

jQueryObject fetchElement = query.FetchXml.Find("fetch");
Expand Down Expand Up @@ -411,7 +412,7 @@ public string GetFetchXmlParentFilter(FetchQuerySettings query, EntityReference
}

// Add in the parent query filter
jQueryObject parentFilter = jQuery.FromHtml("<condition attribute='" + parentAttribute + "' operator='eq' value='" + parentId.Id.Value.Replace("{","").Replace("}","") + "'/>");
jQueryObject parentFilter = jQuery.FromHtml("<condition attribute='" + parentAttribute + "' operator='eq' value='" + ParentRecordPlaceholder + "'/>");
filter.Append(parentFilter);

// Add the order by placeholder for the EntityDataViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<div class="toolbar-buttons">
<button type="button" class="gridtoolbar-button delete16" id="Delete" data-bind="visible: $root.AllowAddNew, click: $root.DeleteSelectedCommand"></button>
<button type="button" class="gridtoolbar-button addnew16" id="AddNew" data-bind="visible: $root.AllowAddNew, click: $root.AddNewCommand"></button>
<button type="button" class="gridtoolbar-button opensubgrid16" id="openAssociatedSubGrid" data-bind="click: $root.OpenAssociatedSubGridCommand"></button>
<button type="button" class="gridtoolbar-button opensubgrid16" id="openAssociatedSubGrid" data-bind="visible: $root.AllowAddNew, click: $root.OpenAssociatedSubGridCommand"></button>
</div>
<div id="contactsEditorGrid">
<!-- Connections Editor Form-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ ClientUI.ViewModels.QueryParser.prototype = {
return fetchXml;
},

getFetchXmlParentFilter: function ClientUI_ViewModels_QueryParser$getFetchXmlParentFilter(query, parentId, parentAttribute) {
getFetchXmlParentFilter: function ClientUI_ViewModels_QueryParser$getFetchXmlParentFilter(query, parentAttribute) {
var fetchElement = query.fetchXml.find('fetch');
fetchElement.attr('count', '{0}');
fetchElement.attr('paging-cookie', '{1}');
Expand All @@ -286,7 +286,7 @@ ClientUI.ViewModels.QueryParser.prototype = {
fetchElement.find('entity').append(andFilter);
}
}
var parentFilter = $("<condition attribute='" + parentAttribute + "' operator='eq' value='" + parentId.id.value.replaceAll('{', '').replaceAll('}', '') + "'/>");
var parentFilter = $("<condition attribute='" + parentAttribute + "' operator='eq' value='" + '#ParentRecordPlaceholder#' + "'/>");
filter.append(parentFilter);
return query.fetchXml.html().replaceAll('</entity>', '{3}</entity>');
}
Expand Down Expand Up @@ -386,22 +386,23 @@ Type.registerNamespace('ClientUI.ViewModel');
ClientUI.ViewModel.ConnectionsViewModel = function ClientUI_ViewModel_ConnectionsViewModel(parentRecordId, connectToTypes, pageSize, viewFetchXml) {
this.SelectedConnection = ko.observable();
this.ErrorMessage = ko.observable();
this.AllowAddNew = ko.observable(true);
this.parentRecordId = ko.observable();
ClientUI.ViewModel.ConnectionsViewModel.initializeBase(this);
this.Connections = new SparkleXrm.GridEditor.EntityDataViewModel(pageSize, ClientUI.Model.Connection, true);
this._parentRecordId$1 = parentRecordId;
this.parentRecordId(parentRecordId);
this._viewFetchXml$1 = viewFetchXml;
var connection = new ClientUI.ViewModel.ObservableConnection(connectToTypes);
connection.record2id(this._parentRecordId$1);
connection.record2id(parentRecordId);
this.ConnectionEdit = ko.validatedObservable(connection);
this.Connections.onDataLoaded.subscribe(ss.Delegate.create(this, this._connections_OnDataLoaded$1));
this.ConnectionEdit().add_onSaveComplete(ss.Delegate.create(this, this._connectionsViewModel_OnSaveComplete$1));
ClientUI.ViewModel.ObservableConnection.registerValidation(this.Connections.validationBinder);
this.AllowAddNew = ko.dependentObservable(ss.Delegate.create(this, this.allowAddNewComputed));
}
ClientUI.ViewModel.ConnectionsViewModel.prototype = {
Connections: null,
ConnectionEdit: null,
_parentRecordId$1: null,
AllowAddNew: null,
_viewFetchXml$1: null,

_connections_OnDataLoaded$1: function ClientUI_ViewModel_ConnectionsViewModel$_connections_OnDataLoaded$1(e, data) {
Expand Down Expand Up @@ -460,11 +461,12 @@ ClientUI.ViewModel.ConnectionsViewModel.prototype = {
},

search: function ClientUI_ViewModel_ConnectionsViewModel$search() {
var parentRecordId = this.parentRecordId().id.toString().replaceAll('{', '').replaceAll('}', '');
if (this._viewFetchXml$1 == null) {
this.Connections.set_fetchXml("<fetch version='1.0' output-format='xml-platform' mapping='logical' returntotalrecordcount='true' no-lock='true' distinct='false' count='{0}' paging-cookie='{1}' page='{2}'>\r\n <entity name='connection'>\r\n <attribute name='record2id' />\r\n <attribute name='record2roleid' />\r\n <attribute name='record1id' />\r\n <attribute name='record1roleid' />\r\n <attribute name='connectionid' />\r\n <filter type='and'>\r\n \r\n <condition attribute='record2id' operator='eq' value='" + this._parentRecordId$1.id.toString().replaceAll('{', '').replaceAll('}', '') + "' />\r\n </filter>\r\n {3}\r\n </entity>\r\n </fetch>");
this.Connections.set_fetchXml("<fetch version='1.0' output-format='xml-platform' mapping='logical' returntotalrecordcount='true' no-lock='true' distinct='false' count='{0}' paging-cookie='{1}' page='{2}'>\r\n <entity name='connection'>\r\n <attribute name='record2id' />\r\n <attribute name='record2roleid' />\r\n <attribute name='record1id' />\r\n <attribute name='record1roleid' />\r\n <attribute name='connectionid' />\r\n <filter type='and'>\r\n \r\n <condition attribute='record2id' operator='eq' value='" + parentRecordId + "' />\r\n </filter>\r\n {3}\r\n </entity>\r\n </fetch>");
}
else {
this.Connections.set_fetchXml(this._viewFetchXml$1);
this.Connections.set_fetchXml(this._viewFetchXml$1.replaceAll('#ParentRecordPlaceholder#', parentRecordId));
}
this.Connections.refresh();
},
Expand All @@ -474,6 +476,7 @@ ClientUI.ViewModel.ConnectionsViewModel.prototype = {
},

AddNewCommand: function ClientUI_ViewModel_ConnectionsViewModel$AddNewCommand() {
this.ConnectionEdit().record2id(this.parentRecordId());
this.ErrorMessage(null);
this.ConnectionEdit().AddNewVisible(true);
},
Expand Down Expand Up @@ -532,6 +535,11 @@ ClientUI.ViewModel.ConnectionsViewModel.prototype = {
}
}));
}), null);
},

allowAddNewComputed: function ClientUI_ViewModel_ConnectionsViewModel$allowAddNewComputed() {
var parent = this.parentRecordId();
return parent != null && parent.id != null && parent.id.value != null && parent.id.value.length > 0;
}
}

Expand Down Expand Up @@ -689,8 +697,7 @@ ClientUI.View.ConnectionsView.Init = function ClientUI_View_ConnectionsView$Init
Xrm.PageEx.majorVersion = 2013;
var lcid = Xrm.Sdk.OrganizationServiceProxy.getUserSettings().uilanguageid;
SparkleXrm.LocalisedContentLoader.fallBackLCID = 0;
SparkleXrm.LocalisedContentLoader.supportedLCIDs.add(1033);
SparkleXrm.LocalisedContentLoader.supportedLCIDs.add(1031);
SparkleXrm.LocalisedContentLoader.supportedLCIDs.add(0);
SparkleXrm.LocalisedContentLoader.loadContent('con_/js/Res.metadata.js', lcid, function() {
ClientUI.View.ConnectionsView._initLocalisedContent();
});
Expand All @@ -704,6 +711,7 @@ ClientUI.View.ConnectionsView._initLocalisedContent = function ClientUI_View_Con
parameters = Xrm.PageEx.getWebResourceData();
id = window.parent.Xrm.Page.data.entity.getId();
logicalName = window.parent.Xrm.Page.data.entity.getEntityName();
window.parent.Xrm.Page.data.entity.addOnSave(ClientUI.View.ConnectionsView._checkForSaved);
var parent = new Xrm.Sdk.EntityReference(new Xrm.Sdk.Guid(id), logicalName, null);
var entities = 'account,contact,opportunity,systemuser';
var $enum1 = ss.IEnumerator.getEnumerator(Object.keys(parameters));
Expand All @@ -726,7 +734,7 @@ ClientUI.View.ConnectionsView._initLocalisedContent = function ClientUI_View_Con
queryParser.queryMetadata();
var connectionViews = queryParser.entityLookup['connection'];
var view = connectionViews.views[Object.keys(connectionViews.views)[0]];
var fetchXml = queryParser.getFetchXmlParentFilter(view, parent, 'record1id');
var fetchXml = queryParser.getFetchXmlParentFilter(view, 'record1id');
ClientUI.View.ConnectionsView._vm = new ClientUI.ViewModel.ConnectionsViewModel(parent, entities.split(','), pageSize, fetchXml);
var connectionsGridDataBinder = new SparkleXrm.GridEditor.GridDataViewBinder();
var columns = view.columns;
Expand Down Expand Up @@ -760,6 +768,16 @@ ClientUI.View.ConnectionsView._initLocalisedContent = function ClientUI_View_Con
ClientUI.View.ConnectionsView._vm.search();
});
}
ClientUI.View.ConnectionsView._checkForSaved = function ClientUI_View_ConnectionsView$_checkForSaved() {
var parent = new Xrm.Sdk.EntityReference(new Xrm.Sdk.Guid(window.parent.Xrm.Page.data.entity.getId()), window.parent.Xrm.Page.data.entity.getEntityName(), null);
if (window.parent.Xrm.Page.ui.getFormType() !== 10*.1 && parent.id != null) {
ClientUI.View.ConnectionsView._vm.parentRecordId(parent);
ClientUI.View.ConnectionsView._vm.search();
}
else {
window.setTimeout(ClientUI.View.ConnectionsView._checkForSaved, 1000);
}
}
ClientUI.View.ConnectionsView._overrideMetadata = function ClientUI_View_ConnectionsView$_overrideMetadata() {
var getSmallIconUrl = Xrm.Sdk.Metadata.MetadataCache.getSmallIconUrl;
var overrideMethod = function(typeName) {
Expand Down Expand Up @@ -795,6 +813,7 @@ ResourceStrings.CancelButton = null;
ResourceStrings.Connection_CollectionName = null;
ResourceStrings.ConnectTo = null;
ResourceStrings.Role = null;
ClientUI.ViewModels.QueryParser.parentRecordPlaceholder = '#ParentRecordPlaceholder#';
ClientUI.Model.Connection.logicalName = 'connection';
ClientUI.View.ConnectionsView._vm = null;
ClientUI.View.ConnectionsView._connectionsGrid = null;
Expand Down
Loading

0 comments on commit 4cf2b93

Please sign in to comment.