Developer-Dashboard

 view release on metacpan or  search on metacpan

lib/Developer/Dashboard/Pax/StandaloneRuntime.pm  view on Meta::CPAN

        return _install_sub_impl($package, $name, $sub->{prototype}, $impl);
    }

    if (($sub->{op} // '') eq 'page_document_legacy_value') {
        $impl = sub {
            my ($value) = @_;
            return _runtime_legacy_value($value);
        };
        return _install_sub_impl($package, $name, $sub->{prototype}, $impl);
    }

    if (($sub->{op} // '') eq 'page_document_legacy_quote') {
        $impl = sub {
            my ($text) = @_;
            return _runtime_legacy_quote($text);
        };
        return _install_sub_impl($package, $name, $sub->{prototype}, $impl);
    }

    if (($sub->{op} // '') eq 'page_document_template_value') {
        my $trim_method = $sub->{trim_method} // die 'compiled sub trim method missing';
        $impl = sub {
            my ($path, $context) = @_;
            my @parts = grep { $_ ne '' } split /\./, _code_for($trim_method)->($path);
            my $value = $context;
            for my $part (@parts) {
                return '' if ref($value) ne 'HASH' || !exists $value->{$part};
                $value = $value->{$part};
            }
            return '' if !defined $value || ref($value);
            return $value;
        };
        return _install_sub_impl($package, $name, $sub->{prototype}, $impl);
    }

    if (($sub->{op} // '') eq 'page_document_legacy_bootstrap') {
        $impl = sub {
            return <<'JS';
<script>
function set_chain_value(obj, path, value) {
  let keys = (path || '').split('.');
  let current = obj;
  for (let i = 0; i < keys.length - 1; i++) {
    if (!current[keys[i]]) current[keys[i]] = {};
    current = current[keys[i]];
  }
  current[keys[keys.length - 1]] = value;
}
function dashboard_ajax_singleton_cleanup(name) {
  if (!name) return;
  if (!window.__dashboardAjaxSingletons) window.__dashboardAjaxSingletons = {};
  if (window.__dashboardAjaxSingletons[name]) return;
  window.__dashboardAjaxSingletons[name] = true;
  window.addEventListener('pagehide', function() {
    let url = '/ajax/singleton/stop?singleton=' + encodeURIComponent(name);
    if (navigator.sendBeacon) {
      navigator.sendBeacon(url, '');
      return;
    }
    if (window.fetch) {
      fetch(url, { method: 'POST', keepalive: true, credentials: 'same-origin' }).catch(function () {});
    }
  });
}
function dashboard_target_nodes(target) {
  if (!target) return [];
  if (typeof target === 'string') return Array.prototype.slice.call(document.querySelectorAll(target));
  if (target instanceof Element) return [target];
  if (target.length && typeof target !== 'string') return Array.prototype.slice.call(target);
  return [];
}
function dashboard_render_value(value, options, formatter) {
  let rendered = value;
  if (options && options.type === 'json' && typeof value === 'string' && value !== '') {
    try {
      rendered = JSON.parse(value);
    } catch (error) {
      rendered = null;
    }
  }
  if (typeof formatter === 'function') return formatter(rendered);
  if (rendered === null || typeof rendered === 'undefined') return '';
  if (typeof rendered === 'object') return JSON.stringify(rendered);
  return String(rendered);
}
function dashboard_write_target(target, value, options, formatter) {
  let nodes = dashboard_target_nodes(target);
  let rendered = dashboard_render_value(value, options || {}, formatter);
  nodes.forEach(function(node) {
    if (options && options.type === 'html') {
      node.innerHTML = rendered;
      return;
    }
    if (node.tagName === 'INPUT' || node.tagName === 'TEXTAREA') {
      node.value = rendered;
      return;
    }
    node.textContent = rendered;
  });
  return rendered;
}
function fetch_value(url, target, options, formatter) {
  if (!url || !window.fetch) return Promise.resolve('');
  let settings = Object.assign({ credentials: 'same-origin' }, (options && options.fetch) || {});
  return window.fetch(url, settings).then(function(response) {
    if (!response.ok) throw new Error('Request failed with status ' + response.status);
    if (options && options.type === 'json') return response.text();
    return response.text();
  }).then(function(value) {
    return dashboard_write_target(target, value, options || {}, formatter);
  });
}
function dashboard_stream_settings(options) {
  let fetchOptions = (options && options.fetch) || {};
  let method = fetchOptions.method || options.method || 'GET';
  let body = typeof fetchOptions.body !== 'undefined' ? fetchOptions.body : (typeof options.body !== 'undefined' ? options.body : null);
  let headers = fetchOptions.headers || options.headers || {};
  let credentials = fetchOptions.credentials || options.credentials || 'same-origin';
  return {
    method: method,
    body: body,
    headers: headers,
    credentials: credentials
  };
}
function stream_data(url, target, options, formatter) {
  if (!url) return Promise.resolve('');
  if (!window.XMLHttpRequest) return fetch_value(url, target, options, formatter);
  let settings = dashboard_stream_settings(options || {});
  return new Promise(function(resolve, reject) {
    let xhr = new XMLHttpRequest();
    xhr.open(settings.method, url, true);
    xhr.withCredentials = settings.credentials !== 'omit';
    Object.keys(settings.headers || {}).forEach(function(name) {
      xhr.setRequestHeader(name, settings.headers[name]);
    });
    xhr.onprogress = function () {
      dashboard_write_target(target, xhr.responseText, options || {}, formatter);
    };
    xhr.onload = function () {
      if (xhr.status < 200 || xhr.status >= 300) {
        reject(new Error('Request failed with status ' + xhr.status));
        return;
      }
      resolve(dashboard_write_target(target, xhr.responseText, options || {}, formatter));
    };
    xhr.onerror = function () {
      reject(new Error('Stream request failed'));
    };
    xhr.send(settings.body);
  });
}
function stream_value(url, target, options, formatter) {
  return stream_data(url, target, options, formatter);
}
var ready_status = {};
function ready(options) {
  let doit = options.doit || function() {};
  let is_ok = options.is_ok || function() { return true; };
  let next = options.next || function() {};
  let fail = options.fail;
  let retries = 0;
  let max_retry = options.num_of_retry;
  let interval = (options.retry_interval || 1) * 1000;
  doit();
  let handle = setInterval(function() {
    if (is_ok()) {
      clearInterval(handle);
      return next();
    }
    retries++;
    if (max_retry && retries >= max_retry) {
      clearInterval(handle);
      if (fail) fail();
    }
  }, interval);
}
if (!window.configs) window.configs = {};
</script>
JS
        };
        return _install_sub_impl($package, $name, $sub->{prototype}, $impl);
    }

    if (($sub->{op} // '') eq 'page_document_trim') {
        $impl = sub {
            my ($text) = @_;
            $text = '' if !defined $text;
            $text =~ s/\A\s+//;
            $text =~ s/\s+\z//;
            return $text;
        };
        return _install_sub_impl($package, $name, $sub->{prototype}, $impl);



( run in 0.775 second using v1.01-cache-2.11-cpan-007c89162af )