Переглянути джерело

Message: add closeOnHashChange, update docs

Leopoldthecoder 8 роки тому
батько
коміт
798bb8e75d

+ 2 - 1
build/cooking.demo.js

@@ -96,7 +96,8 @@ cooking.add('vueMarkdown', {
         return '</div></demo-block>\n';
       }
     }],
-    [require('markdown-it-container'), 'tip']
+    [require('markdown-it-container'), 'tip'],
+    [require('markdown-it-container'), 'warning']
   ],
   preprocess: function(MarkdownIt, source) {
     MarkdownIt.renderer.rules.table_open = function() {

+ 33 - 0
examples/app.vue

@@ -114,6 +114,39 @@
         color: #445368;
       }
     }
+
+    .warning {
+      border-bottom-right-radius: 4px;
+      border-left: 4px solid #f66;
+      border-top-right-radius: 4px;
+      padding: 12px 24px 12px 30px;
+      position: relative;
+      background-color: #f8f8f8;
+      margin-top: 20px;
+      
+      &::before {
+        background-color: #f66;
+        border-radius: 100%;
+        color: #fff;
+        content: "!";
+        font-family: Dosis,Source Sans Pro,Helvetica Neue,Arial,sans-serif;
+        font-size: 14px;
+        font-weight: 700;
+        left: -12px;
+        line-height: 20px;
+        position: absolute;
+        width: 20px;
+        height: 20px;
+        text-align: center;
+        top: 50%;
+        transform: translateY(-50%);
+      }
+  
+      code {
+        background-color: rgba(#fff, .7);
+        color: #445368;
+      }
+    }
   }
   .demo {
     margin: 20px 0;

+ 16 - 4
examples/docs/en-US/message-box.md

@@ -99,7 +99,10 @@
 
 ## MessageBox
 
-A set of modal boxes simulating system message box, mainly for message prompt, success tips, error messages and query information.
+A set of modal boxes simulating system message box, mainly for alerting information, confirm operations and prompting messages.
+:::tip
+By design MessageBox provides simulations of system's `alert`, `confirm` and `prompt`,so it's content should be simple. For more complicated contents, please use Dialog.
+:::
 
 ### Alert
 
@@ -262,17 +265,25 @@ Can be customized to show various content.
 
 ### Global method
 
-Element has added the following global methods for Vue.prototype: `$msgbox`, `$alert`, `$confirm` and `$prompt`. So in a vue instance you can call `MessageBox` like what we did in this page.
+If Element is fully imported, it will add the following global methods for Vue.prototype: `$msgbox`, `$alert`, `$confirm` and `$prompt`. So in a Vue instance you can call `MessageBox` like what we did in this page. The parameters are:
+- `$msgbox(options)`
+- `$alert(message, title, options)` or `$alert(message, options)`
+- `$confirm(message, title, options)` or `$confirm(message, options)`
+- `$prompt(message, title, options)` or `$prompt(message, options)`
 
 ### Local import
 
-Import `MessageBox`:
+If you prefer importing `MessageBox` on demand:
 
 ```javascript
 import { MessageBox } from 'element-ui';
 ```
 
-The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.confirm` and `MessageBox.prompt`.
+The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.confirm` and `MessageBox.prompt`. The parameters are the same as above.
+
+:::warning
+Although `message` property supports HTML strings, dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting). Please make sure the content of `message` is trusted, and **never** assign `message` to user-provided content.
+:::
 
 ### Options
 
@@ -293,6 +304,7 @@ The corresponding methods are: `MessageBox`, `MessageBox.alert`, `MessageBox.con
 | confirmButtonClass | custom class name of confirm button | string | — | — |
 | closeOnClickModal | whether MessageBox can be closed by clicking the mask | boolean | — | true (false when called with alert) |
 | closeOnPressEscape | whether MessageBox can be closed by pressing the ESC | boolean | — | true (false when called with alert) |
+| closeOnHashChange | whether to close MessageBox when hash changes | boolean | — | true |
 | showInput | whether to show an input | boolean | — | false (true when called with prompt) |
 | inputPlaceholder | placeholder of input | string | — | — |
 | inputValue | initial value of input | string | — | — |

+ 18 - 5
examples/docs/zh-CN/message-box.md

@@ -98,7 +98,11 @@
   };
 </script>
 ## MessageBox 弹框
-模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、成功提示、错误提示、询问信息。
+模拟系统的消息提示框而实现的一套模态对话框组件,用于消息提示、确认消息和提交内容。
+
+:::tip
+从场景上说,MessageBox 的作用是美化系统自带的 `alert`、`confirm` 和 `prompt`,因此适合展示较为简单的内容。如果需要弹出较为复杂的内容,请使用 Dialog。
+:::
 
 ### 消息提示
 
@@ -171,7 +175,7 @@
 
 当用户进行操作时会被触发,中断用户操作,提示用户进行输入的对话框。
 
-:::demo 调用`$prompt`方法即可打开消息提示,它模拟了系统的 `prompt`。可以用`inputPattern`字段自己规定匹配模式,或者用`inputValidator`规定校验函数,可以返回`Boolean`或`String`,`Boolean`为`false`或字符串时均表示校验未通过,`String`相当于定义了`inputErrorMessage`字段。此外,可以用`inputPlaceholder`字段来定义输入框的占位符。
+:::demo 调用`$prompt`方法即可打开消息提示,它模拟了系统的 `prompt`。可以用`inputPattern`字段自己规定匹配模式,或者用`inputValidator`规定校验函数,可以返回`Boolean`或`String`,返回`false`或字符串时均表示校验未通过,同时返回的字符串相当于定义了`inputErrorMessage`字段。此外,可以用`inputPlaceholder`字段来定义输入框的占位符。
 
 ```html
 <template>
@@ -259,17 +263,25 @@
 
 ### 全局方法
 
-Element 为 Vue.prototype 添加了如下全局方法:$msgbox, $alert, $confirm 和 $prompt。因此在 vue instance 中可以采用本页面中的方式调用 `MessageBox`。
+如果你完整引入了 Element,它会为 Vue.prototype 添加如下全局方法:$msgbox, $alert, $confirm 和 $prompt。因此在 Vue instance 中可以采用本页面中的方式调用 `MessageBox`。调用参数为:
+- `$msgbox(options)`
+- `$alert(message, title, options)` 或 `$alert(message, options)`
+- `$confirm(message, title, options)` 或 `$confirm(message, options)`
+- `$prompt(message, title, options)` 或 `$prompt(message, options)`
 
 ### 单独引用
 
-单独引入 `MessageBox`:
+如果单独引入 `MessageBox`:
 
 ```javascript
 import { MessageBox } from 'element-ui';
 ```
 
-对应于上述四个全局方法的调用方法依次为:MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt。
+那么对应于上述四个全局方法的调用方法依次为:MessageBox, MessageBox.alert, MessageBox.confirm 和 MessageBox.prompt,调用参数与全局方法相同。
+
+:::warning
+`message` 属性虽然支持传入 HTML 片段,但是在网站上动态渲染任意 HTML 是非常危险的,因为容易导致 [XSS 攻击](https://en.wikipedia.org/wiki/Cross-site_scripting)。请确保 `message` 的内容是可信的,**永远不要**将用户提交的内容赋值给 `message` 属性。
+:::
 
 ### Options
 
@@ -290,6 +302,7 @@ import { MessageBox } from 'element-ui';
 | confirmButtonClass | 确定按钮的自定义类名 | string | — | — |
 | closeOnClickModal | 是否可通过点击遮罩关闭 MessageBox | boolean | — | true(以 alert 方式调用时为 false) |
 | closeOnPressEscape | 是否可通过按下 ESC 键关闭 MessageBox | boolean | — | true(以 alert 方式调用时为 false) |
+| closeOnHashChange | 是否在 hashchange 时关闭 MessageBox | boolean | — | true |
 | showInput | 是否显示输入框 | boolean | — | false(以 prompt 方式调用时为 true)|
 | inputPlaceholder | 输入框的占位符 | string | — | — |
 | inputValue | 输入框的初始文本 | string | — | — |

+ 7 - 0
examples/pages/template/component.tpl

@@ -36,6 +36,13 @@
             padding-left: 10px;
           }
         }
+        ul {
+          margin: 10px 0;
+          padding: 0 0 0 20px;
+          font-size: 14px;
+          color: #5e6d82;
+          line-height: 2em;
+        }
       }
     }
     .page-component-up {

+ 4 - 6
packages/message-box/src/main.js

@@ -8,6 +8,7 @@ const defaults = {
   lockScroll: true,
   closeOnClickModal: true,
   closeOnPressEscape: true,
+  closeOnHashChange: true,
   inputValue: null,
   inputPlaceholder: '',
   inputPattern: null,
@@ -99,7 +100,7 @@ const showNextMsg = () => {
       } else {
         delete instance.$slots.default;
       }
-      ['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape'].forEach(prop => {
+      ['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape', 'closeOnHashChange'].forEach(prop => {
         if (instance[prop] === undefined) {
           instance[prop] = true;
         }
@@ -115,16 +116,13 @@ const showNextMsg = () => {
 
 const MessageBox = function(options, callback) {
   if (Vue.prototype.$isServer) return;
-  if (typeof options === 'string') {
+  if (typeof options === 'string' || isVNode(options)) {
     options = {
       message: options
     };
-    if (arguments[1]) {
+    if (typeof arguments[1] === 'string') {
       options.title = arguments[1];
     }
-    if (arguments[2]) {
-      options.type = arguments[2];
-    }
   } else if (options.callback && !callback) {
     callback = options.callback;
   }

+ 16 - 1
packages/message-box/src/main.vue

@@ -12,7 +12,7 @@
         <div class="el-message-box__content" v-if="message !== ''">
           <div class="el-message-box__status" :class="[ typeClass ]"></div>
           <div class="el-message-box__message" :style="{ 'margin-left': typeClass ? '50px' : '0' }">
-            <slot><p>{{ message }}</p></slot>
+            <slot><p v-html="message"></p></slot>
           </div>
           <div class="el-message-box__input" v-show="showInput">
             <el-input v-model="inputValue" @keyup.enter.native="handleAction('confirm')" :placeholder="inputPlaceholder" ref="input"></el-input>
@@ -75,6 +75,9 @@
       },
       closeOnPressEscape: {
         default: true
+      },
+      closeOnHashChange: {
+        default: true
       }
     },
 
@@ -210,6 +213,18 @@
       }
     },
 
+    mounted() {
+      if (this.closeOnHashChange) {
+        window.addEventListener('hashchange', this.close);
+      }
+    },
+
+    beforeDestroy() {
+      if (this.closeOnHashChange) {
+        window.removeEventListener('hashchange', this.close);
+      }
+    },
+
     data() {
       return {
         uid: 1,