/// <summary> /// Appends a javascript property to the end of the collection that represents the specified propery name and value. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="value">A string that represents the value of the JSObjectItem to add to the end of the collection.</param> /// <param name="valueType">Type of the value.</param> /// <exception cref="System.ArgumentException">This will be thrown if the collection already contains the key.</exception> /// <exception cref="System.ArgumentException"> /// This is thrown if the <em>type</em> is Method or ObjectLiteral. /// </exception> public void AddProperty(string propertyName, string value, JSObjectItemType type) { if (ContainsName(propertyName)) { throw new ArgumentException("This collection already contains the name " + propertyName); } _items.Add(JSObjectItem.CreateProperty(propertyName, value, type)); }
/// <summary> /// Appends the specified javascript property or method to the JSObjectLiteral. /// </summary> /// <param name="item">The javascript property or method to append to the collection.</param> /// <exception cref="System.ArgumentException">This will be thrown if the collection already contains a JSObjectItem with the same name as <em>item</em>.</exception> public void Add(JSObjectItem item) { if (ContainsName(item.Name)) { throw new ArgumentException("This collection already contains the name " + item.Name); } _items.Add(item); }
/// <summary> /// Gets the value or method body of an JSObjectItem with the property or method name, <em>name</em>. /// </summary> /// <remarks> /// Note, a JSObjectItem of type ObjectLiteral will return the complete string of the object /// literal. This might not be what you want. /// </remarks> /// <param name="name">The name to find the property value or method body by.</param> /// <returns>The value of the JSObjectItem with the property or method name, <em>name</em>.</returns> private string GetValueByName(string name) { JSObjectItem item = GetJSObjectItemByName(name); if (item == null) { return(null); } return(item.StringValue); }
/// <summary> /// Checks to see if this JSObjectItem contains a JSObjectMethod with the method name, <em>methodName</em>. /// </summary> /// <param name="methodName">The method name to look for.</param> /// <returns>true if the collection contains a JSObjectMethod with the method name, <em>methodName</em>, false if it does not.</returns> public bool ContainsMethod(string methodName) { int index = GetIndexOfName(methodName); if (index == -1) { return(false); } JSObjectItem item = _items[index]; return(item is JSObjectMethod); }
/// <summary> /// Checks to see if this JSObjectItem contains a JSObjectItem with the property name, <em>propertyName</em> /// and is of JSObjectItemType, <em>type</em>. /// </summary> /// <param name="propertyName">The property name to look for.</param> /// <param name="type">The type of the property.</param> /// <returns>true if the collection contains a JSObjectProperty with the property name, <em>propertyName</em> /// and is of JSObjectItemType, <em>type</em>, false if it does not.</returns> public bool ContainsProperty(string propertyName, JSObjectItemType type) { int index = GetIndexOfName(propertyName); if (index == -1) { return(false); } JSObjectItem item = _items[index]; return(item.ValueType == type); }
/// <summary> /// Creates a new JSObjectItem of the type specified by <em>type</em>. /// </summary> /// <param name="propertyName">The name of the property to be created.</param> /// <param name="value">The value of the property.</param> /// <param name="type">The type of the property.</param> /// <returns> /// A JSObjectItem of the type specified by <em>type</em>. /// </returns> /// <remarks> /// This method cannot be used to create methods or ObjectLiteral properties. /// </remarks> /// <exception cref="System.ArgumentException"> /// This is thrown if the <em>type</em> is Method or ObjectLiteral. /// </exception> public static JSObjectItem CreateProperty(string propertyName, string value, JSObjectItemType type) { JSObjectItem item = null; switch (type) { case JSObjectItemType.Bool: item = (JSObjectItem) new JSObjectBoolProperty(propertyName); break; case JSObjectItemType.Float: item = (JSObjectItem) new JSObjectFloatProperty(propertyName); break; case JSObjectItemType.Reference: item = (JSObjectItem) new JSObjectReferenceProperty(propertyName); break; case JSObjectItemType.Int: item = (JSObjectItem) new JSObjectIntProperty(propertyName); break; case JSObjectItemType.String: item = (JSObjectItem) new JSObjectStringProperty(propertyName); break; case JSObjectItemType.ObjectLiteral: throw new ArgumentException("Property type ObjectLiteral cannot be created by this function."); case JSObjectItemType.Method: throw new ArgumentException("Methods cannot be created by this function."); } item.StringValue = value; return(item); }