Example #1
0
        private SingleChoiceListFinderModel BuildSingleChoiceModel(SocketEventContext model, string prefix, IUpdateModel updater)
        {
            var viewModel = new SingleChoiceListFinderModel();
            var item      = model.Query.Connectors.List().FirstOrDefault();
            var ids       = new int[] {};

            if (item != null && item.RightContent != null)
            {
                viewModel.RightItemId = item.RightContent.ContentItem.Id;
                ids = new[] { item.RightContent.ContentItem.Id };
            }
            PopulateChoiceListModel(model, viewModel, prefix, updater, ids);

            // Add "none" option for the drop-down
            if (model.Connector.Settings.AllowNone)
            {
                viewModel.AvailableRightItems = new[] { new SelectListItem()
                                                        {
                                                            Text = T("(none)").Text, Value = "", Selected = !viewModel.RightItemId.HasValue
                                                        } }
                .Concat(viewModel.AvailableRightItems);
            }

            return(viewModel);
        }
Example #2
0
 private void UpdatedSingleChoiceModel(SocketEventContext model, SingleChoiceListFinderModel viewModel, string prefix, IUpdateModel updater)
 {
     if (viewModel.RightItemId.HasValue)
     {
         // Remove existing ones if only allowed one end point
         // TODO: Sure we *might* want to show the dropdown instead but really, why?
         // Don't add if it's already there
         // TODO: (Perf) possibly not efficient, but it only happens on edits
         if (model.Connector.Settings.AllowDuplicates || !model.Query.Connectors.List().Any(c => c.RightContentItemId == viewModel.RightItemId.Value))
         {
             // Add new connector
             var addedId = viewModel.RightItemId.Value;
             model.Query.Connectors.Add(addedId);
         }
         if (!model.Connector.Settings.AllowMany)
         {
             model.Query.Connectors.Remove(c => c.RightContentItemId != viewModel.RightItemId.Value);
         }
     }
     else
     {
         if (!model.Connector.Settings.AllowMany)
         {
             // Removing all connectors
             model.Query.Connectors.Remove(c => true);
         }
     }
     // TODO: Logic shouldn't be here. Might be a problem if connector creation later fails due to permissions.
     if (!model.Connector.Settings.AllowNone && !model.Query.Connectors.Any())
     {
         updater.AddModelError(prefix + ".RightItemId", T("You must select an item for {0}", model.SocketMetadata.SocketTitle));
     }
 }