Пример #1
0
		/// <summary>
		/// Creates an instance of FilterBoxVmCollection to be used as a collection of stoppage reports (cause)
		/// </summary>
		/// <param name="parent">Instance of <see cref="StoppageReportVm"/> that has this collection</param>
		/// <param name="selectedIds">default cause Ids (must be like: {level1Id, level2Id, level3Id})</param>
		/// <returns></returns>
		public static FilterBoxVmCollection CreateForStoppageReport(Report.StoppageReportVm parent, int[] selectedIds)
		{
			var vm = new FilterBoxVmCollection();

			//find causes and create FilterBoxVm instances for each level
			var causeDs = new DataServices.CauseDataService();
			var causeL1Models = causeDs.GetRoot().Children.Where(x => x.Status == (byte)Common.Status.Active).ToArray();
			var causeL3Box = FilterBoxVm.CreateForCause(null);
			var causeL2Box = FilterBoxVm.CreateForCause(causeL3Box);
			var causeL1Box = FilterBoxVm.CreateForCause(causeL2Box, causeL1Models);

			//set the event handlers
			causeL3Box.FilterableItemSelected += (s, old, v) =>
			{
				string code = string.Empty;
				if (causeL1Box.SelectedItem != null)
				{
					code = ((CauseVm)causeL1Box.SelectedItem.ViewModel).Code;
					if (causeL2Box.SelectedItem != null)
					{
						code += ((CauseVm)causeL2Box.SelectedItem.ViewModel).Code;
						if (v != null) 
							code += ((CauseVm)v.ViewModel).Code;
					}
				}
				parent.SelectedCode =  code;
				parent.SelectCause(v.Id);
			};
			causeL2Box.FilterableItemSelected += (s, old, v) =>
			{
				string code = string.Empty;
				if (causeL1Box.SelectedItem != null)
				{
					code = ((CauseVm)causeL1Box.SelectedItem.ViewModel).Code;
					if (v != null)
						code += ((CauseVm)v.ViewModel).Code;
				}
				parent.SelectedCode = code;

				causeL3Box.FilteredList.Clear();
				if (v == null) return;
				foreach (var item in ((CauseVm)v.ViewModel).ChildrenModels.Where(x => x.Status == (byte)Common.Status.Active))
				{
					causeL3Box.FilteredList.Add(FilterableItemVm.CreateForCause(causeL3Box, item));
				}
			};
			causeL1Box.FilterableItemSelected += (s, old, v) =>
			{
				if (v != null)
					parent.SelectedCode = ((CauseVm)v.ViewModel).Code;

				causeL2Box.FilteredList.Clear();
				if (v == null) return;
				foreach (var item in ((CauseVm)v.ViewModel).ChildrenModels.Where(x => x.Status == (byte)Common.Status.Active))
				{
					causeL2Box.FilteredList.Add(FilterableItemVm.CreateForCause(causeL2Box, item));
				}
			};

			//add filterboxes to the collection
			vm.FilterBoxes.Add(causeL1Box);
			vm.FilterBoxes.Add(causeL2Box);
			vm.FilterBoxes.Add(causeL3Box);

			//select the default cause
			try
			{
				if (selectedIds != null)
				{

					causeL1Box.SelectedItem = causeL1Box.FilteredList.FirstOrDefault(x => x.Id == selectedIds[0]);
					causeL2Box.SelectedItem = causeL2Box.FilteredList.FirstOrDefault(x => x.Id == selectedIds[1]);
					causeL3Box.SelectedItem = causeL3Box.FilteredList.FirstOrDefault(x => x.Id == selectedIds[2]);
				}
				else
				{
					causeL1Box.SelectedItem = causeL1Box.FilteredList.FirstOrDefault();
					causeL2Box.SelectedItem = causeL2Box.FilteredList.FirstOrDefault();
					causeL3Box.SelectedItem = causeL3Box.FilteredList.FirstOrDefault();
				}
			}
			catch { }

			vm.AddCommand = new Commands.Command(o => { });
			return vm;
		}