UNPKG

4.67 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(['exports', 'vue', './action-sheet'], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require('vue'), require('./action-sheet'));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.vue, global.actionSheet);
11 global.index = mod.exports;
12 }
13})(this, function (exports, _vue, _actionSheet) {
14 'use strict';
15
16 Object.defineProperty(exports, "__esModule", {
17 value: true
18 });
19
20 var _vue2 = _interopRequireDefault(_vue);
21
22 var _actionSheet2 = _interopRequireDefault(_actionSheet);
23
24 function _interopRequireDefault(obj) {
25 return obj && obj.__esModule ? obj : {
26 default: obj
27 };
28 }
29
30 var noop = function noop() {};
31
32 // all active instances
33 var instances = [];
34
35 /**
36 * Dynamically create a ActionSheet
37 *
38 * @param {Object} param
39 * @return {ActionSheet}
40 */
41 _actionSheet2.default.create = function (_ref) {
42 var _ref$value = _ref.value,
43 value = _ref$value === undefined ? true : _ref$value,
44 _ref$title = _ref.title,
45 title = _ref$title === undefined ? '' : _ref$title,
46 _ref$options = _ref.options,
47 options = _ref$options === undefined ? [] : _ref$options,
48 _ref$defaultIndex = _ref.defaultIndex,
49 defaultIndex = _ref$defaultIndex === undefined ? -1 : _ref$defaultIndex,
50 _ref$invalidIndex = _ref.invalidIndex,
51 invalidIndex = _ref$invalidIndex === undefined ? -1 : _ref$invalidIndex,
52 _ref$cancelText = _ref.cancelText,
53 cancelText = _ref$cancelText === undefined ? '取消' : _ref$cancelText,
54 _ref$maxHeight = _ref.maxHeight,
55 maxHeight = _ref$maxHeight === undefined ? 400 : _ref$maxHeight,
56 _ref$onShow = _ref.onShow,
57 onShow = _ref$onShow === undefined ? noop : _ref$onShow,
58 _ref$onHide = _ref.onHide,
59 onHide = _ref$onHide === undefined ? noop : _ref$onHide,
60 _ref$onSelected = _ref.onSelected,
61 onSelected = _ref$onSelected === undefined ? noop : _ref$onSelected,
62 _ref$onCancel = _ref.onCancel,
63 onCancel = _ref$onCancel === undefined ? noop : _ref$onCancel;
64
65 var ActionSheetCtor = _vue2.default.extend(_actionSheet2.default);
66 var vm = new ActionSheetCtor({
67 propsData: {
68 value: value,
69 title: title,
70 options: options,
71 defaultIndex: defaultIndex,
72 invalidIndex: invalidIndex,
73 cancelText: cancelText,
74 maxHeight: maxHeight
75 }
76 }).$mount();
77
78 instances.push(vm);
79
80 /* istanbul ignore else */
81 if (value) {
82 document.body.appendChild(vm.$el);
83 vm.value = true;
84 }
85 vm.$watch('value',
86 /* istanbul ignore next */function (val) {
87 if (val) {
88 document.body.appendChild(vm.$el);
89 }
90 });
91 vm.$on('input',
92 /* istanbul ignore next */function (val) {
93 if (val) {
94 vm.value = true;
95 } else {
96 vm.value = false;
97 }
98 });
99 vm.$on('show',
100 /* istanbul ignore next */function () {
101 if (typeof onShow === 'function') {
102 onShow.call(null);
103 }
104 });
105 vm.$on('hide',
106 /* istanbul ignore next */function () {
107 var parent = vm.$el.parentNode;
108 if (parent) {
109 parent.removeChild(vm.$el);
110 }
111 if (typeof onHide === 'function') {
112 onHide.call(null);
113 }
114 });
115 vm.$on('selected',
116 /* istanbul ignore next */function (item) {
117 if (typeof onSelected === 'function') {
118 onSelected.call(null, item);
119 }
120 });
121 vm.$on('cancel',
122 /* istanbul ignore next */function (item) {
123 if (typeof onCancel === 'function') {
124 onCancel.call(null, item);
125 }
126 });
127
128 /* istanbul ignore next */
129 vm.$on('hook:beforeDestroy', function () {
130 var parent = vm.$el.parentNode;
131 var index = instances.indexOf(vm);
132 if (index) {
133 instances.splice(index, 1);
134 }
135 if (parent) {
136 parent.removeChild(vm.$el);
137 }
138 });
139
140 return vm;
141 };
142
143 /**
144 * Close all actived global ActionSheets
145 *
146 * @static
147 * @return void
148 */
149 _actionSheet2.default.closeAll = function () {
150 instances.forEach(function (instance) {
151 instance.value = false;
152 });
153 };
154
155 /**
156 * Close and destroy all actived global ActionSheets
157 *
158 * @static
159 * @return void
160 */
161 _actionSheet2.default.destroyAll = function () {
162 instances.forEach(function (instance) {
163 instance.value = false;
164 instance.$on('hide', function () {
165 instance.$destroy();
166 });
167 });
168 };
169
170 exports.default = _actionSheet2.default;
171});
\No newline at end of file