Home Reference Source

src/senko/Dialog.js

  1. /**
  2. * The script is part of SenkoWSH.
  3. *
  4. * AUTHOR:
  5. * natade (http://twitter.com/natadea)
  6. *
  7. * LICENSE:
  8. * The MIT license https://opensource.org/licenses/MIT
  9. */
  10.  
  11. import System from "./System";
  12. import SFile from "./SFile";
  13.  
  14.  
  15. /**
  16. * ポップアップ用のオプション
  17. * @typedef {Object} PopupOption
  18. * @property {number} [secondstowait=0] タイムアウト時間(`0`で無効)
  19. * @property {string} [caption=""] タイトルバー
  20. * @property {number} [type=0] `Dialog.POPUP_OPTION_TYPE` を組み合わせて使用する
  21. */
  22.  
  23. /**
  24. * 「ファイルを開く」ダイアログ用のオプション
  25. * @typedef {Object} OpenFileOption
  26. * @property {string} [initial_directory] 初期ディレクトリ(`"C:\"`など)
  27. * @property {string} [filter="All files(*.*)|*.*"] ファイル形式(`"画像ファイル(*.png;*.bmp)|*.png;*.bmp"`など)
  28. * @property {string} [title] タイトル(「`ファイルを選択してください`」など)
  29. */
  30.  
  31. /**
  32. * 「フォルダを開く」ダイアログ用のオプション
  33. * @typedef {Object} OpenDirectoryOption
  34. * @property {string} [initial_directory] 初期ディレクトリ(`"C:\"`など)
  35. * @property {string} [title] タイトル(「`フォルダを選択してください`」など)
  36. */
  37.  
  38. /**
  39. * 「名前を付けて保存する」ダイアログ用のオプション
  40. * @typedef {Object} SaveAsOption
  41. * @property {string} [initial_directory] 初期ディレクトリ(`"C:\"`など)
  42. * @property {string} [default_ext] 拡張子を省略した場合の値(`".txt"`など)
  43. * @property {string} [file_name] ファイル名の初期値(`"新しいファイル.txt"`など)
  44. * @property {string} [filter="All files(*.*)|*.*"] ファイル形式(`"画像ファイル(*.png;*.bmp)|*.png;*.bmp"`など)
  45. * @property {string} [title] タイトル(「`保存するファイル名を設定してください`」など)
  46. */
  47.  
  48. /**
  49. * ダイアログを扱うクラス
  50. */
  51. export default class Dialog {
  52. /**
  53. * ダイアログを表示する
  54. * - 引数の `PopupOption` の `type` には `Dialog.POPUP_OPTION_TYPE` を組み合わせて使用できます
  55. * - 戻り値は `Dialog.POPUP_RETURN` のどれかの値が返ります
  56. *
  57. * @param {string} text
  58. * @param {PopupOption} [option]
  59. * @returns {number} `Dialog.POPUP_RETURN`
  60. */
  61. static popupMessage(text, option) {
  62. const secondstowait = option && option.secondstowait ? option.secondstowait : 0;
  63. const caption = option && option.caption ? option.caption : "";
  64. const type = option && option.type ? option.type : 0;
  65. return WScript.CreateObject("WScript.Shell").Popup( text, secondstowait, caption, type );
  66. }
  67.  
  68. /**
  69. * ファイルを開くダイアログを表示する
  70. * @param {OpenFileOption} [option]
  71. * @returns {SFile|null}
  72. */
  73. static popupOpenFile(option) {
  74. let command = "Add-Type -AssemblyName System.Windows.Forms;$dialog = New-Object System.Windows.Forms.OpenFileDialog;";
  75. command += "$dialog.Filter = \"" + ((option && option.filter) ? option.filter.replace(/"/g, "\\\"") : "All files(*.*)|*.*") + "\";";
  76. command += (option && option.initial_directory) ? ("$dialog.InitialDirectory = \"" + option.initial_directory.toString().replace(/"/g, "\\\"") + "\";"): "";
  77. command += (option && option.title) ? ("$dialog.Title = \"" + option.title.replace(/"/g, "\\\"") + "\";"): "";
  78. command += "if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){$dialog.FileName;}";
  79. /*
  80. Add-Type -AssemblyName System.Windows.Forms;
  81. $dialog = New-Object System.Windows.Forms.OpenFileDialog;
  82. ...
  83. if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){
  84. $dialog.FileName;
  85. }
  86. */
  87. const select_text = System.execPowerShell(command).trim();
  88. if(select_text !== "") {
  89. return new SFile(select_text);
  90. }
  91. else {
  92. return null;
  93. }
  94. }
  95.  
  96. /**
  97. * フォルダを開くダイアログを表示する
  98. * @param {OpenDirectoryOption} [option]
  99. * @returns {SFile|null}
  100. */
  101. static popupOpenDirectory(option) {
  102. const shell = new ActiveXObject("Shell.Application");
  103. const caption = option && option.title ? option.title : "";
  104. let folder;
  105. if(option && option.initial_directory) {
  106. folder = shell.BrowseForFolder(0, caption, 0, option.initial_directory);
  107. }
  108. else {
  109. folder = shell.BrowseForFolder(0, caption, 0);
  110. }
  111. if(folder !== null) {
  112. return new SFile(folder.Self.Path);
  113. }
  114. else {
  115. return null;
  116. }
  117. }
  118.  
  119. /**
  120. * 名前を付けて保存ダイアログを表示する
  121. * @param {SaveAsOption} [option]
  122. * @returns {SFile|null}
  123. */
  124. static popupSaveAs(option) {
  125. let command = "Add-Type -AssemblyName System.Windows.Forms;$dialog = New-Object System.Windows.Forms.SaveFileDialog;";
  126. command += (option && option.default_ext) ? ("$dialog.DefaultExt = \"" + option.default_ext.replace(/"/g, "\\\"") + "\";"): "";
  127. command += (option && option.file_name) ? ("$dialog.FileName = \"" + option.file_name.replace(/"/g, "\\\"") + "\";"): "";
  128. command += (option && option.filter) ? ("$dialog.Filter = \"" + option.file_name.replace(/"/g, "\\\"") + "\";"): "";
  129. command += (option && option.initial_directory) ? ("$dialog.InitialDirectory = \"" + option.initial_directory.toString().replace(/"/g, "\\\"") + "\";"): "";
  130. command += (option && option.title) ? ("$dialog.Title = \"" + option.title.replace(/"/g, "\\\"") + "\";"): "";
  131. command += "$dialog.ShowHelp = $FALSE;";
  132. command += "$dialog.OverwritePrompt = $TRUE;";
  133. command += "if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){$dialog.FileName;}";
  134. /*
  135. Add-Type -AssemblyName System.Windows.Forms;
  136. $dialog = New-Object System.Windows.Forms.SaveFileDialog;
  137. $dialog.Title = "ファイルを指定して下さい。";
  138. $dialog.ShowHelp = $FALSE;
  139. $dialog.OverwritePrompt = $TRUE;
  140. if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){
  141. $dialog.FileName;
  142. }
  143. */
  144. const select_text = System.execPowerShell(command).trim();
  145. if(select_text !== "") {
  146. return new SFile(select_text);
  147. }
  148. else {
  149. return null;
  150. }
  151. }
  152.  
  153. }
  154.  
  155. /**
  156. * メッセージボックスのボタン配置
  157. * @typedef {Object} typePopupMessageButton
  158. * @property {number} MB_OK 「OK」のボタン配置
  159. * @property {number} MB_OKCANCEL 「OK」、「キャンセル」のボタン配置
  160. * @property {number} MB_ABORTRETRYIGNORE 「中止」、「再試行」、「無視」のボタン配置
  161. * @property {number} MB_YESNOCANCEL 「はい」、「いいえ」、「キャンセル」のボタン配置
  162. * @property {number} MB_YESNO 「はい」、「いいえ」のボタン配置
  163. * @property {number} MB_RETRYCANCEL 「再試行」、「キャンセル」のボタン配置
  164. */
  165.  
  166. /**
  167. * メッセージボックスのアイコン
  168. * @typedef {Object} typePopupMessageIcon
  169. * @property {number} MB_ICONSTOP 中止「Stop」のアイコンのダイアログ
  170. * @property {number} MB_ICONQUESTION 質問「?」のアイコンのダイアログ
  171. * @property {number} MB_ICONWARNING 警告「!」のアイコンのダイアログ
  172. * @property {number} MB_ICONINFORMATION 情報「i」のアイコンのダイアログ
  173. */
  174.  
  175. /**
  176. * メッセージボックスのボタンのデフォルト
  177. * @typedef {Object} typePopupMessageDefaultButton
  178. * @property {number} MB_DEFBUTTON1 「ボタン1」を選択
  179. * @property {number} MB_DEFBUTTON2 「ボタン2」を選択
  180. * @property {number} MB_DEFBUTTON3 「ボタン3」を選択
  181. * @property {number} MB_DEFBUTTON4 「ボタン4」を選択
  182. */
  183.  
  184. /**
  185. * メッセージボックスのボタンのデフォルト
  186. * @typedef {Object} typePopupMessageOption
  187. * @property {typePopupMessageButton} BUTTON ボタン配置
  188. * @property {typePopupMessageIcon} ICON アイコン
  189. * @property {typePopupMessageDefaultButton} DEFAULT_BUTTON ボタンのデフォルト
  190. */
  191.  
  192. /**
  193. * `Dialog.popupMessage` 引数用の定数
  194. * @type {typePopupMessageOption}
  195. */
  196. Dialog.POPUP_OPTION_TYPE = {
  197. BUTTON : {
  198. MB_OK : 0,
  199. MB_OKCANCEL : 1,
  200. MB_ABORTRETRYIGNORE : 2,
  201. MB_YESNOCANCEL : 3,
  202. MB_YESNO : 4,
  203. MB_RETRYCANCEL : 5
  204. },
  205. ICON : {
  206. MB_ICONSTOP : 16,
  207. MB_ICONQUESTION : 32,
  208. MB_ICONWARNING : 48,
  209. MB_ICONINFORMATION : 64
  210. },
  211. DEFAULT_BUTTON : {
  212. MB_DEFBUTTON1 : 0x0000,
  213. MB_DEFBUTTON2 : 0x0100,
  214. MB_DEFBUTTON3 : 0x0200,
  215. MB_DEFBUTTON4 : 0x0300
  216. }
  217. };
  218.  
  219. /**
  220. * メッセージボックスの戻り値
  221. * @typedef {Object} typePopupMessageReturn
  222. * @property {number} IDTIMEOUT タイムアウトが発生
  223. * @property {number} IDOK 「OK」を選択
  224. * @property {number} IDCANCEL 「キャンセル」を選択
  225. * @property {number} IDABORT 「中止」を選択
  226. * @property {number} IDRETRY 「再試行」を選択
  227. * @property {number} IDIGNORE 「無視」を選択
  228. * @property {number} IDYES 「はい」を選択
  229. * @property {number} IDNO 「いいえ」を選択
  230. */
  231.  
  232. /**
  233. * `Dialog.popupMessage` の戻り値用の定数
  234. * @type {typePopupMessageReturn}
  235. */
  236. Dialog.POPUP_RETURN = {
  237. IDTIMEOUT : -1,
  238. IDOK : 1,
  239. IDCANCEL : 2,
  240. IDABORT : 3,
  241. IDRETRY : 4,
  242. IDIGNORE : 5,
  243. IDYES : 6,
  244. IDNO : 7
  245. };
  246.