frappe-ui 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frappe-ui",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "A set of components and utilities for rapid UI development",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -2,8 +2,9 @@ import { call, debounce } from 'frappe-ui'
2
2
  import { reactive, watch } from 'vue'
3
3
 
4
4
  let cached = {}
5
+ let documentCache = {}
5
6
 
6
- function createResource(options, vm, getResource) {
7
+ export function createResource(options, vm, getResource) {
7
8
  let cacheKey = null
8
9
  if (options.cache) {
9
10
  cacheKey = getCacheKey(options.cache)
@@ -88,10 +89,10 @@ function createResource(options, vm, getResource) {
88
89
 
89
90
  function update({ method, params, auto }) {
90
91
  if (method && method !== options.method) {
91
- options.method = method
92
+ out.method = method
92
93
  }
93
94
  if (params && params !== options.params) {
94
- options.params = params
95
+ out.params = params
95
96
  }
96
97
  if (auto !== undefined && auto !== out.auto) {
97
98
  out.auto = auto
@@ -119,7 +120,13 @@ function createResource(options, vm, getResource) {
119
120
  }
120
121
  }
121
122
 
123
+ // usage:
124
+ // setData(newData) or
125
+ // setData(data => data.filter(d => !d.deleted))
122
126
  function setData(data) {
127
+ if (typeof data === 'function') {
128
+ data = data.call(vm, out.data)
129
+ }
123
130
  out.data = data
124
131
  }
125
132
 
@@ -130,6 +137,82 @@ function createResource(options, vm, getResource) {
130
137
  return out
131
138
  }
132
139
 
140
+ export function createDocumentResource(options, vm) {
141
+ let cacheKey = getCacheKey([options.doctype, options.name])
142
+ if (documentCache[cacheKey]) {
143
+ return documentCache[cacheKey]
144
+ }
145
+
146
+ let setValueOptions = {
147
+ method: 'frappe.client.set_value',
148
+ makeParams(values) {
149
+ return {
150
+ doctype: out.doctype,
151
+ name: out.name,
152
+ fieldname: values,
153
+ }
154
+ },
155
+ onSuccess(data) {
156
+ out.doc = data
157
+ },
158
+ }
159
+
160
+ let out = reactive({
161
+ doctype: options.doctype,
162
+ name: options.name,
163
+ doc: null,
164
+ get: createResource({
165
+ method: 'frappe.client.get',
166
+ makeParams() {
167
+ return {
168
+ doctype: out.doctype,
169
+ name: out.name,
170
+ }
171
+ },
172
+ onSuccess(data) {
173
+ out.doc = data
174
+ },
175
+ }),
176
+ setValue: createResource(setValueOptions),
177
+ setValueDebounced: createResource({
178
+ ...setValueOptions,
179
+ debounce: options.debounce || 500,
180
+ }),
181
+ delete: createResource({
182
+ method: 'frappe.client.delete',
183
+ makeParams() {
184
+ return {
185
+ doctype: out.doctype,
186
+ name: out.name,
187
+ }
188
+ },
189
+ onSuccess() {
190
+ out.doc = null
191
+ },
192
+ }),
193
+ update,
194
+ })
195
+
196
+ function update(updatedOptions) {
197
+ out.doctype = updatedOptions.doctype
198
+ out.name = updatedOptions.name
199
+ out.get.fetch()
200
+ }
201
+
202
+ // fetch the doc
203
+ out.get.fetch()
204
+ // cache
205
+ documentCache[cacheKey] = out
206
+ return out
207
+ }
208
+
209
+ function createResourceForOptions(options, vm, getResource) {
210
+ if (options.type === 'document') {
211
+ return createDocumentResource(options, vm, getResource)
212
+ }
213
+ return createResource(options, vm, getResource)
214
+ }
215
+
133
216
  function getCacheKey(cacheKey) {
134
217
  if (typeof cacheKey === 'string') {
135
218
  cacheKey = [cacheKey]
@@ -156,7 +239,7 @@ let createMixin = (mixinOptions) => ({
156
239
 
157
240
  let resource = this._resources[key]
158
241
  if (!resource) {
159
- resource = createResource(
242
+ resource = createResourceForOptions(
160
243
  updatedOptions,
161
244
  this,
162
245
  mixinOptions.getResource
@@ -174,7 +257,11 @@ let createMixin = (mixinOptions) => ({
174
257
  }
175
258
  )
176
259
  } else {
177
- let resource = createResource(options, this, mixinOptions.getResource)
260
+ let resource = createResourceForOptions(
261
+ options,
262
+ this,
263
+ mixinOptions.getResource
264
+ )
178
265
  this._resources[key] = resource
179
266
  if (resource.auto) {
180
267
  resource.fetch()
@@ -184,12 +271,13 @@ let createMixin = (mixinOptions) => ({
184
271
  }
185
272
  },
186
273
  methods: {
187
- $refetchResource(cache) {
274
+ $getResource(cache) {
188
275
  let cacheKey = getCacheKey(cache)
189
- if (cached[cacheKey]) {
190
- let resource = cached[cacheKey]
191
- resource.fetch()
192
- }
276
+ return cached[cacheKey] || null
277
+ },
278
+ $refetchResource(cache) {
279
+ let resource = this.$getResource(cache)
280
+ resource && resource.fetch()
193
281
  },
194
282
  },
195
283
  computed: {