public UserControl GetRender(ViewModel parent, Response r)
    {
      JObject resource = r.Decode<JObject>();

      BuilderContext context = new BuilderContext();
      MasonViewModel vm = new MasonViewModel(parent, resource, context);
      return new ApplicationMasonRender(vm);
    }
Beispiel #2
0
    public ResourceViewModel(ViewModel parent, JObject resource, BuilderContext context)
      : base(parent, resource)
    {
      Properties = new ObservableCollection<ViewModel>();

      JObject namespaces = resource.SelectToken(MasonProperties.Namespaces) as JObject;
      if (namespaces != null)
        BuildNamespaces(namespaces, context);

      foreach (var pair in resource)
      {
        if (pair.Key == MasonProperties.Namespaces && pair.Value is JObject)
        {
          // Ignore - it has been handled
        }
        else if (pair.Key == MasonProperties.Controls && pair.Value is JObject)
        {
          ControlsJsonValue = pair.Value;
          Controls = new ObservableCollection<ControlViewModel>(
            pair.Value.Children().OfType<JProperty>().Select(n => BuildControlElement(this, n.Name, n.Value as JObject, context)).Where(n => n != null));
        }
        else if (pair.Key == MasonProperties.Meta && pair.Value is JObject)
        {
          MetaJsonValue = pair.Value;
          Description = GetValue<string>(pair.Value, MasonProperties.MetaProperties.Description);
          JToken metaControlsProperty = pair.Value[MasonProperties.Controls];
          if (metaControlsProperty is JObject)
          {
            MetaControlsJsonValue = metaControlsProperty;
            MetaControls = new ObservableCollection<ControlViewModel>(
              metaControlsProperty.Children().OfType<JProperty>().Select(l => BuildControlElement(this, l.Name, l.Value as JObject, context)));
          }
        }
        else if (pair.Key == MasonProperties.Error && pair.Value is JObject)
        {
          ResourcePropertyViewModel error = new ResourcePropertyViewModel(this, pair.Value, pair.Key, new ResourceViewModel(this, (JObject)pair.Value, context));
          error.IsError = true;
          Properties.Add(error);
        }
        else
        {
          Properties.Add(CreatePropertiesRecursively(pair.Key, pair.Value, context));
        }
      }

      if (Controls == null)
        Controls = new ObservableCollection<ControlViewModel>();
    }
Beispiel #3
0
    public ControlViewModel(ViewModel parent, string name, JObject json, BuilderContext context, IControlBuilder cb)
      : base(parent, json)
    {
      if (json == null)
        throw new InvalidOperationException(string.Format("Expected JSON object for control {0}", name));
      
      string prefix;
      string reference;
      string nsname;

      Name = context.Namespaces.Expand(name, out prefix, out reference, out nsname);

      RegisterCommand(ActivateControlCommand = new DelegateCommand<object>(ActivateControl));

      if (reference != null && nsname != null)
      {
        NamePart1 = nsname;
        NamePart2 = reference;
      }
      else
      {
        NamePart1 = "";
        NamePart2 = Name;
      }

      JArray alt = json["alt"] as JArray;
      if (alt != null)
      {
        AlternateControls = new ObservableCollection<ControlViewModel>();
        for (int i = 0; i < alt.Count; ++i)
        {
          JObject l = alt[i] as JObject;
          if (l != null)
            AlternateControls.Add(cb.BuildControlElement(this, name, l, context));
              //LinkViewModel(this, l, string.Format("alt[{0}]", i), context));
        }
      }
    }
Beispiel #4
0
    public MasonViewModel(ViewModel parent, JObject resource, BuilderContext context)
      : base(parent)
    {
      MainProperty = new ResourcePropertyViewModel(this, resource, "ROOT", new ResourceViewModel(this, resource, context));

      Subscribe<SourceChangedEventArgs>(e => Source = e.Source);

      // Extract meta title for window title and top-level property name
      if (resource[MasonProperties.Meta] != null && resource[MasonProperties.Meta][MasonProperties.MetaProperties.Title] != null)
      {
        string title = resource[MasonProperties.Meta][MasonProperties.MetaProperties.Title].Value<string>();
        if (!string.IsNullOrEmpty(title))
        {
          MainProperty.Name = title;
          Publish(new TitleChangedEventArgs { Title = title });
        }
      }
      else
      {
        Publish(new TitleChangedEventArgs { Title = "Unnamed resource" });
      }

      Source = resource.ToString();
    }
 public JsonActionViewModel(ViewModel parent, string name, JObject json, BuilderContext context, IControlBuilder cb)
   : base(parent, name, json, context, cb)
 {
 }
Beispiel #6
0
    public ControlViewModel BuildControlElement(ViewModel parent, string name, JObject value, BuilderContext context)
    {
      string encoding = GetValue<string>(value, "encoding", "none").ToLower();
      string method = GetValue<string>(value, "method", "GET").ToUpper();
      bool isHrefTemplate = GetValue<bool?>(value, "isHrefTemplate") ?? false;

      if (method == "GET" && encoding == MasonProperties.EncodingTypes.None)
      {
        if (isHrefTemplate)
          return new VoidActionViewModel(parent, name, value, context, this);
        else
          return new LinkViewModel(parent, name, value, context, this);
      }
      else if (encoding == MasonProperties.EncodingTypes.None)
        return new VoidActionViewModel(parent, name, value, context, this);
      else if (encoding == MasonProperties.EncodingTypes.JSON)
        return new JsonActionViewModel(parent, name, value, context, this);
      else if (encoding == MasonProperties.EncodingTypes.JSONFiles)
        return new JsonFilesActionViewModel(parent, name, value, context, this);

      return null;
    }
Beispiel #7
0
 private void BuildNamespaces(JObject namespaces, BuilderContext context)
 {
   foreach (JProperty ns in namespaces.Properties())
   {
     JObject nsDef = ns.Value as JObject;
     if (nsDef != null)
     {
       JToken jsonName = nsDef[MasonProperties.NamespaceProperties.Name];
       string ns_name = (jsonName != null && jsonName.Type == JTokenType.String ? jsonName.Value<string>() : null);
       string ns_prefix = ns.Name;
       if (!string.IsNullOrWhiteSpace(ns_prefix) && !string.IsNullOrWhiteSpace(ns_name))
         context.Namespaces.Namespace(ns_prefix, ns_name);
     }
   }
 }
Beispiel #8
0
 private PropertyViewModel CreatePropertiesRecursively(string name, JToken json, BuilderContext context)
 {
   if (json is JArray)
   {
     int index=0;
     ObservableCollection<ViewModel> array = new ObservableCollection<ViewModel>(json.Select(i => CreatePropertiesRecursively(string.Format("[{0}]",index++), i, context)));
     return new ArrayPropertyViewModel(this, json, name, array);
   }
   else if (json is JObject)
   {
     return new ResourcePropertyViewModel(this, json, name, new ResourceViewModel(this, (JObject)json, context));
   }
   else
     return new PropertyViewModel(this, json, name, (json != null ? json.ToString() : ""));
 }
 public JsonActionViewModel(ViewModel parent, string name, JObject json, BuilderContext context, IControlBuilder cb)
     : base(parent, name, json, context, cb)
 {
 }