###** Fragment update API =================== This module exposes a low-level Javascript API to [change](/up.replace) or [destroy](/up.destroy) page fragments. Most of Unpoly's functionality (like [fragment links](/up.link) or [modals](/up.modal)) is built from these functions. You can use them to extend Unpoly from your [custom Javascript](/up.syntax). @class up.dom ### up.dom = (($) -> u = up.util ###** Configures defaults for fragment insertion. @property up.dom.config @param {string} [options.fallbacks=['body']] When a fragment updates cannot find the requested element, Unpoly will try this list of alternative selectors. The first selector that matches an element in the current page (or response) will be used. If the response contains none of the selectors, an error message will be shown. It is recommend to always keep `'body'` as the last selector in the last in the case your server or load balancer renders an error message that does not contain your application layout. @param {string} [options.fallbackTransition=null] The transition to use when using a [fallback target](/#options.fallbacks). By default this is not set and the original replacement's transition is used. @stable ### config = u.config fallbacks: ['body'] fallbackTransition: null reset = -> config.reset() setSource = (element, sourceUrl) -> unless sourceUrl is false $element = $(element) sourceUrl = u.normalizeUrl(sourceUrl) if u.isPresent(sourceUrl) $element.attr("up-source", sourceUrl) ###** Returns the URL the given element was retrieved from. @method up.dom.source @param {string|Element|jQuery} selectorOrElement @experimental ### source = (selectorOrElement) -> $element = $(selectorOrElement).closest('[up-source]') u.presence($element.attr("up-source")) || up.browser.url() ###** Resolves the given CSS selector (which might contain `&` references) to a full CSS selector without ampersands. If passed an `Element` or `jQuery` element, returns a CSS selector string for that element. @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)). @internal ### resolveSelector = (selectorOrElement, origin) -> if u.isString(selectorOrElement) selector = selectorOrElement if u.contains(selector, '&') if u.isPresent(origin) # isPresent returns false for empty jQuery collection originSelector = u.selectorForElement(origin) selector = selector.replace(/\&/, originSelector) else up.fail("Found origin reference (%s) in selector %s, but no origin was given", '&', selector) else selector = u.selectorForElement(selectorOrElement) selector ###** Replaces elements on the current page with corresponding elements from a new page fetched from the server. The current and new elements must both match the given CSS selector. The unobtrusive variant of this is the [`a[up-target]`](/a-up-target) selector. \#\#\# Example Let's say your curent HTML looks like this: