| 1 | #region license
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (c) 2008, Kouji Yamaguchi
|
|---|
| 4 | All rights reserved.
|
|---|
| 5 |
|
|---|
| 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|---|
| 7 |
|
|---|
| 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|---|
| 9 |
|
|---|
| 10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|---|
| 11 |
|
|---|
| 12 | * Neither the name of coma2n nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|---|
| 13 |
|
|---|
| 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|---|
| 15 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 16 | IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|---|
| 17 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|---|
| 18 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|---|
| 19 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 20 | */
|
|---|
| 21 | #endregion
|
|---|
| 22 |
|
|---|
| 23 | #region namespaces
|
|---|
| 24 |
|
|---|
| 25 | using System;
|
|---|
| 26 | using System.Windows.Browser;
|
|---|
| 27 | using System.Diagnostics;
|
|---|
| 28 |
|
|---|
| 29 | using Silverlight.Commons;
|
|---|
| 30 |
|
|---|
| 31 | #endregion
|
|---|
| 32 |
|
|---|
| 33 | namespace Silverlight.JQuery {
|
|---|
| 34 |
|
|---|
| 35 | #region JQuery class
|
|---|
| 36 |
|
|---|
| 37 | /// <summary>
|
|---|
| 38 | /// JavaScriptライブラリ「jQuery」をラップするクラス
|
|---|
| 39 | /// </summary>
|
|---|
| 40 | public sealed class JQuery {
|
|---|
| 41 |
|
|---|
| 42 | #region const
|
|---|
| 43 |
|
|---|
| 44 | /// <summary>
|
|---|
| 45 | /// ブラウザウィンドウ
|
|---|
| 46 | /// </summary>
|
|---|
| 47 | private static readonly HtmlWindow window = HtmlPage.Window;
|
|---|
| 48 |
|
|---|
| 49 | #endregion
|
|---|
| 50 |
|
|---|
| 51 | #region fields
|
|---|
| 52 |
|
|---|
| 53 | /// <summary>
|
|---|
| 54 | /// エクスプレッションに該当した要素
|
|---|
| 55 | /// </summary>
|
|---|
| 56 | private ScriptObject element;
|
|---|
| 57 |
|
|---|
| 58 | #endregion
|
|---|
| 59 |
|
|---|
| 60 | #region constructors
|
|---|
| 61 |
|
|---|
| 62 | /// <summary>
|
|---|
| 63 | /// 指定したエクスプレッションからjQueryオブジェクトを生成します。
|
|---|
| 64 | /// </summary>
|
|---|
| 65 | /// <param name="expression">エクスプレッション</param>
|
|---|
| 66 | /// <exception cref="ArgumentException">引数が不正な時</exception>
|
|---|
| 67 | /// <exception cref="ArgumentNullException">引数がnullの時</exception>
|
|---|
| 68 | public JQuery(string expression) {
|
|---|
| 69 | #region ArgumentValidation
|
|---|
| 70 | ArgumentValidation.CheckForNullOrEmpty(expression, "expression");
|
|---|
| 71 | #endregion
|
|---|
| 72 |
|
|---|
| 73 | element = (ScriptObject)window.Invoke("$", expression);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | #endregion
|
|---|
| 77 |
|
|---|
| 78 | #region methods
|
|---|
| 79 |
|
|---|
| 80 | /// <summary>
|
|---|
| 81 | /// 要素のHTMLを取得します。
|
|---|
| 82 | /// </summary>
|
|---|
| 83 | /// <returns>HTML</returns>
|
|---|
| 84 | public string Html() { return Get<string>("html"); }
|
|---|
| 85 | /// <summary>
|
|---|
| 86 | /// 指定したHTML文字列を要素のHTMLに設定します。
|
|---|
| 87 | /// </summary>
|
|---|
| 88 | /// <param name="html">HTML文字列</param>
|
|---|
| 89 | /// <returns>自分自身</returns>
|
|---|
| 90 | public JQuery Html(string html) { return Set("html", html); }
|
|---|
| 91 |
|
|---|
| 92 | /// <summary>
|
|---|
| 93 | /// 要素のテキストを取得します。
|
|---|
| 94 | /// </summary>
|
|---|
| 95 | /// <returns>テキスト</returns>
|
|---|
| 96 | public string Text() { return Get<string>("text"); }
|
|---|
| 97 | /// <summary>
|
|---|
| 98 | /// 指定した文字列を要素のテキストに設定します。
|
|---|
| 99 | /// </summary>
|
|---|
| 100 | /// <param name="text">文字列</param>
|
|---|
| 101 | /// <returns>自分自身</returns>
|
|---|
| 102 | public JQuery Text(string text) { return Set("text", text); }
|
|---|
| 103 |
|
|---|
| 104 | /// <summary>
|
|---|
| 105 | /// 指定したコンテンツを現在の要素の子要素として後ろから追加します。
|
|---|
| 106 | /// </summary>
|
|---|
| 107 | /// <param name="contents">要素(JQueryオブジェクト or 文字列)</param>
|
|---|
| 108 | /// <returns>自分自身</returns>
|
|---|
| 109 | /// <exception cref="ArgumentNullException">引数がnullの時</exception>
|
|---|
| 110 | public JQuery Append(params object[] contents) { return AppendInternal("append", contents); }
|
|---|
| 111 |
|
|---|
| 112 | /// <summary>
|
|---|
| 113 | /// 指定したコンテンツを現在の要素の後ろに追加します。
|
|---|
| 114 | /// </summary>
|
|---|
| 115 | /// <param name="contents">要素(JQueryオブジェクト or 文字列)</param>
|
|---|
| 116 | /// <returns>自分自身</returns>
|
|---|
| 117 | /// <exception cref="ArgumentNullException">引数がnullの時</exception>
|
|---|
| 118 | public JQuery AppendTo(params object[] contents) { return AppendInternal("appendTo", contents); }
|
|---|
| 119 |
|
|---|
| 120 | /// <summary>
|
|---|
| 121 | /// 指定したコンテンツを現在の要素の子要素として前から追加します。
|
|---|
| 122 | /// </summary>
|
|---|
| 123 | /// <param name="contents">要素(JQueryオブジェクト or 文字列)</param>
|
|---|
| 124 | /// <returns>自分自身</returns>
|
|---|
| 125 | /// <exception cref="ArgumentNullException">引数がnullの時</exception>
|
|---|
| 126 | public JQuery Prepend(params object[] contents) { return AppendInternal("prepend", contents); }
|
|---|
| 127 |
|
|---|
| 128 | /// <summary>
|
|---|
| 129 | /// 指定したコンテンツを現在の要素の前に追加します。
|
|---|
| 130 | /// </summary>
|
|---|
| 131 | /// <param name="contents">要素(JQueryオブジェクト or 文字列)</param>
|
|---|
| 132 | /// <returns>自分自身</returns>
|
|---|
| 133 | /// <exception cref="ArgumentNullException">引数がnullの時</exception>
|
|---|
| 134 | public JQuery PrependTo(params object[] contents) { return AppendInternal("prependTo", contents); }
|
|---|
| 135 |
|
|---|
| 136 | /// <summary>
|
|---|
| 137 | ///
|
|---|
| 138 | /// </summary>
|
|---|
| 139 | /// <typeparam name="T"></typeparam>
|
|---|
| 140 | /// <param name="methodName"></param>
|
|---|
| 141 | /// <returns></returns>
|
|---|
| 142 | private T Get<T>(string methodName) {
|
|---|
| 143 | return (T)element.Invoke(methodName);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /// <summary>
|
|---|
| 147 | ///
|
|---|
| 148 | /// </summary>
|
|---|
| 149 | /// <param name="methodName"></param>
|
|---|
| 150 | /// <param name="value"></param>
|
|---|
| 151 | /// <returns></returns>
|
|---|
| 152 | private JQuery Set(string methodName, object value) {
|
|---|
| 153 | element.Invoke(methodName, value);
|
|---|
| 154 |
|
|---|
| 155 | return this;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /// <summary>
|
|---|
| 159 | ///
|
|---|
| 160 | /// </summary>
|
|---|
| 161 | /// <param name="methodName"></param>
|
|---|
| 162 | /// <param name="contents"></param>
|
|---|
| 163 | /// <returns></returns>
|
|---|
| 164 | private JQuery AppendInternal(string methodName, object[] contents) {
|
|---|
| 165 | #region ArgumentValidation
|
|---|
| 166 | ArgumentValidation.CheckForNullReference(contents, "contents");
|
|---|
| 167 | #endregion
|
|---|
| 168 |
|
|---|
| 169 | foreach(var c in contents) {
|
|---|
| 170 | element.Invoke(methodName,
|
|---|
| 171 | (c is JQuery) ? ((JQuery)c).element : c
|
|---|
| 172 | );
|
|---|
| 173 | }
|
|---|
| 174 | return this;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | #endregion
|
|---|
| 178 |
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | #endregion
|
|---|
| 182 |
|
|---|
| 183 | }
|
|---|