トースト通知

このサンプルでは検証コントロールにおける検証結果をトースト通知に設定する方法を説明します。トーストの表示位置やプログレスバーの表示などを調整できるほか、スタイルやテンプレートをレイアウトに適用することもできます。

検証コントロールのトースト通知機能について解説します。 概要 notify(またはdefaultNotify)プロパティのtoastプロパティを使用することで、検証結果を通知するトーストの表示可否(boolean)を設定できますが、このtoastプロパティには、通知のスタイル、通知の方向や配置位置などを調整する機能もあります。また、通知のレイアウトに適用するテンプレートもこのプロパティから設定可能です。 通知の表示位置 トースト通知の表示位置は、positionプロパティから設定可能です。設定可能な値(ToastPosition列挙体)は、以下の通りです。デフォルト値はTopRightです。 値 説明 TopRight トースト通知をコントロールの右上に表示します。(既定値) TopLeft トースト通知をコントロールの左上に表示します。 TopCenter トースト通知をコントロールの上中央に表示します。 BottomRight トースト通知をコントロールの右下に表示します。 BottomLeft トースト通知をコントロールの左下に表示します。 BottomCenter トースト通知をコントロールの下中央に表示します。 たとえば、トーストを画面の右下に表示したい場合のサンプルコードは、以下の通りです。(toastプロパティの設定部分のみ抜粋) 通知の表示時間 トーストの表示時間は、durationプロパティで秒単位で設定できます。例えば、表示時間を3秒に設定する場合は、次のように設定します。デフォルト値は4.5秒です。0に設定した場合はユーザーが手動で閉じるまで消えません。 通知のプログレスバー プログレスバーはトーストの有効期限が切れるまでの時間を視覚的に示します。showProgressプロパティをtrueに設定すると、プログレスバーの表示を有効にします。デフォルト値はtrueです。 また、showProgressプロパティをtrueに設定すると、トーストメッセージにマウスオーバーする際に一時停止を設定することもできます。 閉じるボタン showCloseプロパティをtrueまたはfalseに設定すると、トーストの閉じるボタンを表示/非表示に設定できます。デフォルト値はtrueです。 通知のテンプレート templateプロパティを使用することで、トーストの表示内容を定義するテンプレートを適用することが可能です。 以下は、トースト通知の背景色と文字色などをテンプレートで設定したサンプルコードです。
import '@mescius/inputman/CSS/gc.inputman-js.css'; import { InputMan } from '@mescius/inputman'; InputMan.appearanceStyle = InputMan.AppearanceStyle.Modern; const gcTextBox = new InputMan.GcTextBox( document.getElementById('gcTextBox'), { width: 200, } ); const gcNumber1 = new InputMan.GcNumber( document.getElementById('gcNumber1'), { minValue: 0, maxValue: 10, value: 5, } ); const toastPosition = [ InputMan.ToastPosition.TopLeft, InputMan.ToastPosition.TopCenter, InputMan.ToastPosition.TopRight, InputMan.ToastPosition.BottomLeft, InputMan.ToastPosition.BottomCenter, InputMan.ToastPosition.BottomRight, ]; const template = (context) => { const content = document.createElement('div'); content.innerHTML = ` <div class='rule' style="margin-left:3rem">テンプレート</div> <div style="color:red;min-width:20.5rem;margin-left:3rem">${context.message}</div> `; content.addEventListener('click', function () { context.close(); }); return content; }; document.getElementById('template').addEventListener('change', (e) => { update(); }); gcNumber1.onValueChanged((sender, eArgs) => { update(); }); document.getElementById('showProgress').addEventListener('change', (e) => { update(); }); document.getElementById('position').addEventListener('change', (e) => { update(); }); document.getElementById('showClose').addEventListener('change', (e) => { update(); }); document.getElementById('pauseOnHover').addEventListener('change', (e) => { update(); }); var validator; const update = () => { if (validator) { validator.destroy(); } validator = new InputMan.GcValidator({ items: [ { control: gcTextBox, ruleSet: [ { rule: InputMan.ValidateType.BetweenOrEqualToMinAndMaxLength, inputMaxLength: 8, inputMinLength: 4, successMessage: (control) => `検証が成功しました。`, failMessage: (control) => `検証が失敗しました。`, }, ], validateWhen: InputMan.ValidateWhen.Typing, }, ], defaultNotify: { fail: { toast: { title: '検証失敗', position: toastPosition[document.getElementById('position').selectedIndex], duration: gcNumber1.value, showProgress: document.getElementById('showProgress').checked, showClose: document.getElementById('showClose').checked, pauseOnHover: document.getElementById('pauseOnHover').checked, template: document.getElementById('template').checked ? template : '', }, }, success: { toast: { title: '検証成功', position: toastPosition[document.getElementById('position').selectedIndex], duration: gcNumber1.value, showProgress: document.getElementById('showProgress').checked, showClose: document.getElementById('showClose').checked, pauseOnHover: document.getElementById('pauseOnHover').checked, template: document.getElementById('template').checked ? template : '', }, }, }, }); validator.validate(); validator.refresh(); }; update(); gcTextBox.setText('トースト通知');
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>検証コントロール - 通知 - トースト通知</title> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> window.onload = function () { System.import('./src/app'); } </script> </head> <body> <input id="gcTextBox" style="margin: 8rem 15rem 2rem 15rem"> <table class="sample"> <tbody> <tr> <th>表示時間</th> <td> <input id="gcNumber1" /> </td> </tr> <tr> <th>表示位置</th> <td> <select id="position" class="update"> <option>左上</option> <option>上中央</option> <option>右上</option> <option>左下</option> <option>下中央</option> <option>右下</option> </select> </td> </tr> <tr> <th>プログレスバーを表示する</th> <td> <label><input type="checkbox" id="showProgress" class="update" checked />表示する</label><br /> </td> </tr> <tr> <th>閉じるボタン</th> <td> <label><input type="checkbox" id="showClose" class="update" checked />表示する</label><br /> </td> </tr> <tr> <th>ホバー時に一時停止</th> <td> <label><input type="checkbox" id="pauseOnHover" class="update" checked />停止する</label><br /> </td> </tr> <tr> <th>通知のテンプレート</th> <td> <label><input type="checkbox" id="template" class="update" />テンプレートを使って表示</label><br /> </td> </tr> </tbody> </table> </body> </html>
System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@mescius/inputman': 'npm:@mescius/inputman/index.js', '@mescius/inputman/CSS': 'npm:@mescius/inputman/CSS', '@mescius/inputman.richtexteditor': 'npm:@mescius/inputman.richtexteditor/index.js', "@mescius/inputman.richtexteditor/CSS": "npm:@mescius/inputman.richtexteditor/CSS", '@mescius/inputman.richtexteditor/JS/plugins/advlist': 'npm:@mescius/inputman.richtexteditor/JS/plugins/advlist/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/all': 'npm:@mescius/inputman.richtexteditor/JS/plugins/all/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/autosave': 'npm:@mescius/inputman.richtexteditor/JS/plugins/autosave/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/charmap': 'npm:@mescius/inputman.richtexteditor/JS/plugins/charmap/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/directionality': 'npm:@mescius/inputman.richtexteditor/JS/plugins/directionality/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/emoticons': 'npm:@mescius/inputman.richtexteditor/JS/plugins/emoticons/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/fullscreen': 'npm:@mescius/inputman.richtexteditor/JS/plugins/fullscreen/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/htmlcode': 'npm:@mescius/inputman.richtexteditor/JS/plugins/htmlcode/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/image': 'npm:@mescius/inputman.richtexteditor/JS/plugins/image/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/link': 'npm:@mescius/inputman.richtexteditor/JS/plugins/link/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/lists': 'npm:@mescius/inputman.richtexteditor/JS/plugins/lists/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/media': 'npm:@mescius/inputman.richtexteditor/JS/plugins/media/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/pagebreak': 'npm:@mescius/inputman.richtexteditor/JS/plugins/pagebreak/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/preview': 'npm:@mescius/inputman.richtexteditor/JS/plugins/preview/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/save': 'npm:@mescius/inputman.richtexteditor/JS/plugins/save/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/searchreplace': 'npm:@mescius/inputman.richtexteditor/JS/plugins/searchreplace/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/table': 'npm:@mescius/inputman.richtexteditor/JS/plugins/table/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/template': 'npm:@mescius/inputman.richtexteditor/JS/plugins/template/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/wordcount': 'npm:@mescius/inputman.richtexteditor/JS/plugins/wordcount/plugin.js', '@mescius/inputman.comment': 'npm:@mescius/inputman.comment/index.js', '@mescius/inputman.comment/CSS': 'npm:@mescius/inputman.comment/CSS', '@mescius/wijmo': 'npm:@mescius/wijmo/index.js', '@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles', '@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures', '@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js', '@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js', '@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets/styles': 'npm:@mescius/spread-sheets/styles', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, } });