UNPKG

8.73 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define([], factory);
4 } else if (typeof exports !== "undefined") {
5 factory();
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory();
11 global.bootstrapTableEditable = mod.exports;
12 }
13})(this, function () {
14 'use strict';
15
16 function _classCallCheck(instance, Constructor) {
17 if (!(instance instanceof Constructor)) {
18 throw new TypeError("Cannot call a class as a function");
19 }
20 }
21
22 var _createClass = function () {
23 function defineProperties(target, props) {
24 for (var i = 0; i < props.length; i++) {
25 var descriptor = props[i];
26 descriptor.enumerable = descriptor.enumerable || false;
27 descriptor.configurable = true;
28 if ("value" in descriptor) descriptor.writable = true;
29 Object.defineProperty(target, descriptor.key, descriptor);
30 }
31 }
32
33 return function (Constructor, protoProps, staticProps) {
34 if (protoProps) defineProperties(Constructor.prototype, protoProps);
35 if (staticProps) defineProperties(Constructor, staticProps);
36 return Constructor;
37 };
38 }();
39
40 function _possibleConstructorReturn(self, call) {
41 if (!self) {
42 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
43 }
44
45 return call && (typeof call === "object" || typeof call === "function") ? call : self;
46 }
47
48 var _get = function get(object, property, receiver) {
49 if (object === null) object = Function.prototype;
50 var desc = Object.getOwnPropertyDescriptor(object, property);
51
52 if (desc === undefined) {
53 var parent = Object.getPrototypeOf(object);
54
55 if (parent === null) {
56 return undefined;
57 } else {
58 return get(parent, property, receiver);
59 }
60 } else if ("value" in desc) {
61 return desc.value;
62 } else {
63 var getter = desc.get;
64
65 if (getter === undefined) {
66 return undefined;
67 }
68
69 return getter.call(receiver);
70 }
71 };
72
73 function _inherits(subClass, superClass) {
74 if (typeof superClass !== "function" && superClass !== null) {
75 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
76 }
77
78 subClass.prototype = Object.create(superClass && superClass.prototype, {
79 constructor: {
80 value: subClass,
81 enumerable: false,
82 writable: true,
83 configurable: true
84 }
85 });
86 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
87 }
88
89 /**
90 * @author zhixin wen <wenzhixin2010@gmail.com>
91 * extensions: https://github.com/vitalets/x-editable
92 */
93
94 (function ($) {
95 var Utils = $.fn.bootstrapTable.utils;
96
97 $.extend($.fn.bootstrapTable.defaults, {
98 editable: true,
99 onEditableInit: function onEditableInit() {
100 return false;
101 },
102 onEditableSave: function onEditableSave(field, row, oldValue, $el) {
103 return false;
104 },
105 onEditableShown: function onEditableShown(field, row, $el, editable) {
106 return false;
107 },
108 onEditableHidden: function onEditableHidden(field, row, $el, reason) {
109 return false;
110 }
111 });
112
113 $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
114 'editable-init.bs.table': 'onEditableInit',
115 'editable-save.bs.table': 'onEditableSave',
116 'editable-shown.bs.table': 'onEditableShown',
117 'editable-hidden.bs.table': 'onEditableHidden'
118 });
119
120 $.BootstrapTable = function (_$$BootstrapTable) {
121 _inherits(_class, _$$BootstrapTable);
122
123 function _class() {
124 _classCallCheck(this, _class);
125
126 return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
127 }
128
129 _createClass(_class, [{
130 key: 'initTable',
131 value: function initTable() {
132 var _this2 = this;
133
134 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initTable', this).call(this);
135
136 if (!this.options.editable) {
137 return;
138 }
139
140 $.each(this.columns, function (i, column) {
141 if (!column.editable) {
142 return;
143 }
144
145 var editableOptions = {};
146 var editableDataMarkup = [];
147 var editableDataPrefix = 'editable-';
148 var processDataOptions = function processDataOptions(key, value) {
149 // Replace camel case with dashes.
150 var dashKey = key.replace(/([A-Z])/g, function ($1) {
151 return '-' + $1.toLowerCase();
152 });
153 if (dashKey.indexOf(editableDataPrefix) === 0) {
154 editableOptions[dashKey.replace(editableDataPrefix, 'data-')] = value;
155 }
156 };
157
158 $.each(_this2.options, processDataOptions);
159
160 column.formatter = column.formatter || function (value) {
161 return value;
162 };
163 column._formatter = column._formatter ? column._formatter : column.formatter;
164 column.formatter = function (value, row, index) {
165 var result = Utils.calculateObjectValue(column, column._formatter, [value, row, index], value);
166
167 $.each(column, processDataOptions);
168
169 $.each(editableOptions, function (key, value) {
170 editableDataMarkup.push(' ' + key + '="' + value + '"');
171 });
172
173 var _dont_edit_formatter = false;
174 if (column.editable.hasOwnProperty('noeditFormatter')) {
175 _dont_edit_formatter = column.editable.noeditFormatter(value, row, index);
176 }
177
178 if (_dont_edit_formatter === false) {
179 return '<a href="javascript:void(0)"\n data-name="' + column.field + '"\n data-pk="' + row[_this2.options.idField] + '"\n data-value="' + result + '"\n ' + editableDataMarkup.join('') + '></a>';
180 }
181 return _dont_edit_formatter;
182 };
183 });
184 }
185 }, {
186 key: 'initBody',
187 value: function initBody(fixedScroll) {
188 var _this3 = this;
189
190 _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'initBody', this).call(this, fixedScroll);
191
192 if (!this.options.editable) {
193 return;
194 }
195
196 $.each(this.columns, function (i, column) {
197 if (!column.editable) {
198 return;
199 }
200
201 var data = _this3.getData();
202 var $field = _this3.$body.find('a[data-name="' + column.field + '"]');
203
204 $field.each(function (i, element) {
205 var $element = $(element);
206 var $tr = $element.closest('tr');
207 var index = $tr.data('index');
208 var row = data[index];
209
210 var editableOpts = Utils.calculateObjectValue(column, column.editable, [index, row, $element], {});
211
212 $element.editable(editableOpts);
213 });
214
215 $field.off('save').on('save', function (_ref, _ref2) {
216 var currentTarget = _ref.currentTarget;
217 var submitValue = _ref2.submitValue;
218
219 var $this = $(currentTarget);
220 var data = _this3.getData();
221 var index = $this.parents('tr[data-index]').data('index');
222 var row = data[index];
223 var oldValue = row[column.field];
224
225 $this.data('value', submitValue);
226 row[column.field] = submitValue;
227 _this3.trigger('editable-save', column.field, row, oldValue, $this);
228 _this3.resetFooter();
229 });
230
231 $field.off('shown').on('shown', function (_ref3, editable) {
232 var currentTarget = _ref3.currentTarget;
233
234 var $this = $(currentTarget);
235 var data = _this3.getData();
236 var index = $this.parents('tr[data-index]').data('index');
237 var row = data[index];
238
239 _this3.trigger('editable-shown', column.field, row, $this, editable);
240 });
241
242 $field.off('hidden').on('hidden', function (_ref4, reason) {
243 var currentTarget = _ref4.currentTarget;
244
245 var $this = $(currentTarget);
246 var data = _this3.getData();
247 var index = $this.parents('tr[data-index]').data('index');
248 var row = data[index];
249
250 _this3.trigger('editable-hidden', column.field, row, $this, reason);
251 });
252 });
253 this.trigger('editable-init');
254 }
255 }]);
256
257 return _class;
258 }($.BootstrapTable);
259 })(jQuery);
260});
\No newline at end of file