1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | 1× 23× 23× 23× 23× 23× 23× 8× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 23× 21× 6× 15× 2× 2× 2× 2× 2× 21× 13× 13× 13× 13× 13× 13× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× | import { assert } from '@ember/debug'; import { isArray } from '@ember/array'; import { camelize } from '@ember/string'; import ArrayProxy from '@ember/array/proxy'; import ObjectProxy from '@ember/object/proxy'; import { getOwner } from '@ember/application'; import { readOnly } from '@ember/object/computed'; import { typeOf as emberTypeOf } from '@ember/utils'; import EmberObject, { computed } from '@ember/object'; import PromiseProxyMixin from '@ember/object/promise-proxy-mixin'; import { reject } from 'rsvp'; import deepMerge from 'lodash/merge'; import normalizePayload from 'ember-custom-actions/utils/normalize-payload'; import urlBuilder from 'ember-custom-actions/utils/url-builder'; const promiseProxies = { array: ArrayProxy.extend(PromiseProxyMixin), object: ObjectProxy.extend(PromiseProxyMixin) }; export default EmberObject.extend({ id: '', model: null, options: {}, instance: false, integrated: false, init() { this._super(...arguments); assert('Custom actions require model property to be passed!', this.get('model')); assert('Custom action model has to be persisted!', !(this.get('instance') && !this.get('model.id'))); }, /** @return {DS.Store} */ store: readOnly('model.store'), /** @return {String} */ modelName: computed('model', function() { let { constructor } = this.get('model'); return constructor.modelName || constructor.typeKey; }).readOnly(), /** @return {DS.Adapter} */ adapter: computed('modelName', 'store', function() { return this.get('store').adapterFor(this.get('modelName')); }).readOnly(), /** @return {DS.Serializer} */ serializer: computed('modelName', 'store', function() { return this.get('store').serializerFor(this.get('modelName')); }).readOnly(), /** @return {Ember.Object} */ config: computed('options', 'model', function() { let model = this.get('model'); let appConfig = model ? (getOwner(model).resolveRegistration('config:environment').emberCustomActions || {}) : {}; let mergedConfig = deepMerge({}, appConfig, this.get('options')); return EmberObject.create(mergedConfig); }).readOnly(), /** @public @method callAction @return {Promise} */ callAction() { let promise = this._promise(); let responseType = camelize(this.get('config.responseType') || ''); let promiseProxy = promiseProxies[responseType]; return promiseProxy ? promiseProxy.create({ promise }) : promise; }, /** @private @method queryParams @return {Object} */ queryParams() { let queryParams = this.get('config.queryParams'); assert('Custom action queryParams option has to be an object', emberTypeOf(queryParams) === 'object'); return this.get('adapter').sortQueryParams(queryParams); }, /** @private @method requestMethod @return {String} */ requestMethod() { let integrated = this.get('integrated') && this.get('adapter').methodForCustomAction; let method = this.get('config.method').toUpperCase(); return integrated ? this._methodForCustomAction(method) : method; }, /** @private @method requestUrl @return {String} */ requestUrl() { let integrated = this.get('integrated') && this.get('adapter').urlForCustomAction; return integrated ? this._urlForCustomAction() : this._urlFromBuilder(); }, /** @private @method requestHeaders @return {String} */ requestHeaders() { let integrated = this.get('integrated') && this.get('adapter').headersForCustomAction; let configHeaders = this.get('config.headers'); let headers = integrated ? this._headersForCustomAction(configHeaders) : configHeaders; assert('Custom action headers option has to be an object', emberTypeOf(headers) === 'object'); return headers; }, /** @private @method requestData @return {Object} */ requestData() { let integrated = this.get('integrated') && this.get('adapter').dataForCustomAction; let payload = this.get('config.data'); let data = (integrated ? this._dataForCustomAction(payload) : payload) || {}; assert('Custom action payload has to be an object', emberTypeOf(data) === 'object'); return normalizePayload(data, this.get('config.normalizeOperation')); }, /** @private @method ajaxOptions @return {Object} */ ajaxOptions() { return deepMerge({}, this.get('config.ajaxOptions'), { data: this.requestData(), headers: this.requestHeaders() }); }, // Internals _promise() { return this.get('adapter') .ajax(this.requestUrl(), this.requestMethod(), this.ajaxOptions()) .then(this._onSuccess.bind(this), this._onError.bind(this)); }, _onSuccess(response) { if (this.get('config.pushToStore') && this._validResponse(response)) { return this.get('serializer').pushPayload(this.get('store'), response); } return response; }, _onError(error) { Eif (this.get('config.pushToStore') && isArray(error.errors)) { let id = this.get('model.id'); let typeClass = this.get('model').constructor; error.serializedErrors = this.get('serializer').extractErrors(this.get('store'), typeClass, error, id); } return reject(error); }, _validResponse(object) { return emberTypeOf(object) === 'object' && Object.keys(object).length > 0; }, _urlFromBuilder() { let path = this.get('id'); let queryParams = this.queryParams(); let modelName = this.get('modelName'); let id = this.get('instance') ? this.get('model.id') : null; let url = this.get('adapter')._buildURL(modelName, id); return urlBuilder(url, path, queryParams); }, // Adapter integration API _urlForCustomAction() { let id = this.get('model.id'); let actionId = this.get('id'); let queryParams = this.queryParams(); let modelName = this.get('modelName'); let adapterOptions = this.get('config.adapterOptions'); let snapshot = this.get('model')._internalModel.createSnapshot({ adapterOptions }); return this.get('adapter').urlForCustomAction(modelName, id, snapshot, actionId, queryParams); }, _methodForCustomAction(method) { let actionId = this.get('id'); let modelId = this.get('model.id'); return this.get('adapter').methodForCustomAction({ method, actionId, modelId }); }, _headersForCustomAction(headers) { let actionId = this.get('id'); let modelId = this.get('model.id'); return this.get('adapter').headersForCustomAction({ headers, actionId, modelId }); }, _dataForCustomAction(data) { let actionId = this.get('id'); let modelId = this.get('model.id'); return this.get('adapter').dataForCustomAction({ data, actionId, modelId }); } }); |