lib/assets/javascripts/unpoly/dom.coffee.erb in unpoly-rails-0.56.7 vs lib/assets/javascripts/unpoly/dom.coffee.erb in unpoly-rails-0.57.0

- old
+ new

@@ -68,10 +68,11 @@ @function up.dom.resolveSelector @param {string|Element|jQuery} selectorOrElement @param {string|Element|jQuery} origin The element that this selector resolution is relative to. That element's selector will be substituted for `&` ([like in Sass](https://sass-lang.com/documentation/file.SASS_REFERENCE.html#parent-selector)). + @return {string} @internal ### resolveSelector = (selectorOrElement, origin) -> if u.isString(selectorOrElement) selector = selectorOrElement @@ -177,17 +178,12 @@ If the call pushes an history entry and this option is missing, the title is extracted from the response's `<title>` tag. You can also pass `false` to explicitly prevent the title from being updated. @param {string} [options.method='get'] The HTTP method to use for the request. - @param {Object|Array|FormData} [options.data] - Parameters that should be sent as the request's payload. - - Parameters can either be passed as an object (where the property names become - the param names and the property values become the param values) or as - an array of `{ name: 'param-name', value: 'param-value' }` objects - + @param {Object|FormData|string|Array} [options.params] + [Parameters](/up.params) that should be sent as the request's payload. @param {string} [options.transition='none'] @param {string|boolean} [options.history=true] If a string is given, it is used as the URL the browser's location bar and history. If omitted or true, the `url` argument will be used. If set to `false`, the history will remain unchanged. @@ -209,22 +205,19 @@ Whether to use a [cached response](/up.proxy) if available. @param {string} [options.historyMethod='push'] @param {Object} [options.headers={}] An object of additional header key/value pairs to send along with the request. - @param {boolean} [options.requireMatch=true] - Whether to raise an error if the given selector is missing in - either the current page or in the response. @param {Element|jQuery} [options.origin] The element that triggered the replacement. The element's selector will be substituted for the `&` shorthand in the target selector ([like in Sass](https://sass-lang.com/documentation/file.SASS_REFERENCE.html#parent-selector)). @param {string} [options.layer='auto'] The name of the layer that ought to be updated. Valid values are - `auto`, `page`, `modal` and `popup`. + `'auto'`, `'page'`, `'modal'` and `'popup'`. - If set to `auto` (default), Unpoly will try to find a match in the + If set to `'auto'` (default), Unpoly will try to find a match in the same layer as the element that triggered the replacement (see `options.origin`). If that element is not known, or no match was found in that layer, Unpoly will search in other layers, starting from the topmost layer. @param {string} [options.failLayer='auto'] The name of the layer that ought to be updated if the server sends a non-200 status code. @@ -238,11 +231,11 @@ @stable ### replace = (selectorOrElement, url, options) -> options = u.options(options) - options.inspectResponse = fullLoad = -> up.browser.navigate(url, u.only(options, 'method', 'data')) + options.inspectResponse = fullLoad = -> up.browser.navigate(url, u.only(options, 'method', 'params')) if !up.browser.canPushState() && options.history != false fullLoad() unless options.preload return u.unresolvablePromise() @@ -264,30 +257,32 @@ catch e # Since we're an async function, we should not throw exceptions but return a rejected promise. # http://2ality.com/2016/03/promise-rejections-vs-exceptions.html return Promise.reject(e) - request = + request = new up.Request( url: url method: options.method - data: options.data + data: options.data # deprecated, use { params } + params: options.params target: improvedTarget failTarget: improvedFailTarget cache: options.cache preload: options.preload headers: options.headers timeout: options.timeout + ) onSuccess = (response) -> - processResponse(true, improvedTarget, response, successOptions) + processResponse(true, improvedTarget, request, response, successOptions) onFailure = (response) -> rejection = -> Promise.reject(response) if response.isFatalError() rejection() else - promise = processResponse(false, improvedFailTarget, response, failureOptions) + promise = processResponse(false, improvedFailTarget, request, response, failureOptions) # Although processResponse() we will perform a successful replacement of options.failTarget, # we still want to reject the promise that's returned to our API client. u.always(promise, rejection) promise = up.request(request) @@ -295,19 +290,16 @@ promise ###** @internal ### - processResponse = (isSuccess, selector, response, options) -> - request = response.request + processResponse = (isSuccess, selector, request, response, options) -> sourceUrl = response.url historyUrl = sourceUrl - hash = request.hash - if options.reveal == true && hash - # If the request URL had a #hash and options.reveal is not given, we reveal that #hash. - options.reveal = hash + if hash = request.hash + options.hash = hash historyUrl += hash isReloadable = (response.method == 'GET') if isSuccess @@ -374,11 +366,10 @@ ### extract = (selectorOrElement, html, options) -> up.log.group 'Extracting %s from %d bytes of HTML', selectorOrElement, html?.length, -> options = u.options options, historyMethod: 'push' - requireMatch: true keep: true layer: 'auto' up.layout.saveScroll() unless options.saveScroll == false @@ -450,11 +441,11 @@ hello($wrapper.children(), options) # Reveal element that was being prepended/appended. # Since we will animate (not morph) it's OK to allow animation of scrolling # if the user has configured up.layout.config.duration. - promise = up.layout.revealOrRestoreScroll($wrapper, options) + promise = up.layout.scrollAfterInsertFragment($wrapper, options) # Since we're adding content instead of replacing, we'll only # animate $new instead of morphing between $old and $new promise = promise.then -> up.animate($wrapper, transition, options) @@ -488,10 +479,11 @@ $old.remove() # clean up jQuery data emitFragmentDestroyed($old, $parent: $parent, log: false) return up.morph($old, $new, transition, morphOptions) + # This will find all [up-keep] descendants in $old an, overwrite their partner # element in $new and leave a visually identical clone in $old for a later transition. # Returns an array of keepPlans. transferKeepableElements = ($old, $new, options) -> keepPlans = [] @@ -510,10 +502,11 @@ # replace the matching element with $keepable so it will eventually return to the DOM. plan.$newElement.replaceWith($keepable) keepPlans.push(plan) keepPlans + findKeepPlan = ($element, $new, options) -> if options.keep $keepable = $element if partnerSelector = u.castedAttr($keepable, 'up-keep') u.isString(partnerSelector) or partnerSelector = '&' @@ -523,12 +516,12 @@ else $partner = u.selectInSubtree($new, partnerSelector) $partner = $partner.first() if $partner.length && $partner.is('[up-keep]') plan = - $element: $keepable # the element that should be kept - $newElement: $partner # the element that would have replaced it but now does not + $element: $keepable # the element that should be kept + $newElement: $partner # the element that would have replaced it but now does not newData: up.syntax.data($partner) # the parsed up-data attribute of the element we will discard keepEventArgs = u.merge(plan, message: ['Keeping element %o', $keepable.get(0)]) if up.bus.nobodyPrevents('up:fragment:keep', keepEventArgs) plan @@ -650,11 +643,11 @@ $element = $(selectorOrElement) options = u.options(options, keepPlans: []) keptElements = [] for plan in options.keepPlans emitFragmentKept(plan) - keptElements.push(plan.$element) + keptElements.push(plan.$element[0]) up.syntax.compile($element, skip: keptElements) emitFragmentInserted($element, options) $element ###** @@ -716,13 +709,14 @@ Also see the [`.up-destroying`](/up-destroying) class. @function up.first @param {string|Element|jQuery|Array<Element>} selectorOrElement - @param {string} options.layer - The name of the layer in which to find the element. Valid values are - `auto`, `page`, `modal` and `popup`. + @param {string} [options.layer='auto'] + The name of the layer in which to find the element. + + Valid values are `'auto'`, `'page'`, `'modal'` and `'popup'`. @param {string|Element|jQuery} [options.origin] An second element or selector that can be referenced as `&` in the first selector: $input = $('input.email'); up.first('.field:has(&)', $input); // returns the .field containing $input @@ -760,23 +754,54 @@ if isRealElement($element) && matchesLayer($element, layer) $match = $element break $match + ###** + @function up.dom.layerOf + @internal + ### layerOf = (selectorOrElement) -> $element = $(selectorOrElement) - if up.popup.contains($element) - 'popup' - else if up.modal.contains($element) - 'modal' - else - 'page' + if $element.length + if up.popup.contains($element) + 'popup' + else if up.modal.contains($element) + 'modal' + else + 'page' matchesLayer = (selectorOrElement, layer) -> - layerOf(selectorOrElement) == layer + !layer || layerOf(selectorOrElement) == layer ###** + Returns all elements matching the given selector, but + ignores elements that are being [destroyed](/up.destroy) or [transitioned](/up.morph). + + If the given argument is already a jQuery collection (or an array + of DOM elements), returns the subset of the given list that is matching these conditions. + + @function up.all + @param {string|jQuery|Array<Element>} selectorOrElements + @param {string|Element|jQuery} [options.origin] + An second element or selector that can be referenced as `&` in the first selector. + @param {string} [options.layer] + The name of the layer in which to find the element. Valid values are + `'page'`, `'modal'` and `'popup'`. + @return {jQuery} + A jQuery collection of matching elements. + @experimental + ### + all = (selectorOrElements, options) -> + options = u.options(options) + resolved = resolveSelector(selectorOrElements, options.origin) + $root = $(u.option(options.root, document)) + $root.find(resolved).filter (index, element) -> + $element = $(element) + isRealElement($element) && matchesLayer($element, options.layer) + + ###** Destroys the given element or selector. Takes care that all [`up.compiler()`](/up.compiler) destructors, if any, are called. The element is removed from the DOM. @@ -910,20 +935,23 @@ replace: replace reload: reload destroy: destroy extract: extract first: first + all: all source: source resolveSelector: resolveSelector hello: hello config: config + layerOf: layerOf )(jQuery) up.replace = up.dom.replace up.extract = up.dom.extract up.reload = up.dom.reload up.destroy = up.dom.destroy up.first = up.dom.first +up.all = up.dom.all up.hello = up.dom.hello -up.renamedModule 'flow', 'dom' +up.deprecateRenamedModule 'flow', 'dom'