onfmready.js
Version:
A developer utility for working with the FileMaker web viewer JS object in FileMaker Pro/WebDirect.
148 lines (147 loc) • 5.36 kB
JavaScript
(function () {
var $wnd = window;
var iexplore;
if (!!window.document.documentMode) {
if (typeof Object.assign != 'function') {
Object.defineProperty(Object, 'assign', {
value: function assign(target, varArgs) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) {
for (var nextKey in nextSource) {
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
configurable: true
});
}
if (typeof window.CustomEvent != 'function') {
function CustomEvent(event, params) {
params = params || {
bubbles: false,
cancelable: false,
detail: undefined
};
var evt = document.createEvent('Event');
evt.initEvent(event, params.bubbles, params.cancelable);
Object.defineProperty(evt, 'detail', { value: params.detail });
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
window.Event = CustomEvent;
}
var FM_CONTEXT_TEST_1 = 'if (window.FileMaker != null)';
iexplore = {
resolver: function () {
try {
var caller = iexplore.resolver.caller.caller.toString();
iexplore.stash = caller.indexOf(FM_CONTEXT_TEST_1) >= 0;
}
catch (ex) {
iexplore.stash = false;
}
},
stash: false
};
}
var deferred = [];
var unfulfilled;
var events = function () {
if (!unfulfilled) {
var ready = new Event('filemaker-ready');
$wnd.dispatchEvent(ready);
document.dispatchEvent(ready);
}
var expected = Object.assign(new Event('filemaker-expected'), {
filemaker: !unfulfilled,
FileMaker: !unfulfilled
});
$wnd.dispatchEvent(expected);
document.dispatchEvent(expected);
};
if (typeof $wnd.FileMaker === 'object') {
setTimeout(events);
return;
}
$wnd.OnFMReady = Object.assign({ respondTo: {}, noLogging: false, unmount: false }, $wnd.OnFMReady);
var DEFAULT = {
PerformScript: function (script, param) {
return DEFAULT.PerformScriptWithOption(script, param);
},
PerformScriptWithOption: function (script, param, option) {
if (option === void 0) { option = 0; }
if (unfulfilled) {
utility(script, param, option);
}
else {
deferred.push([script, param, option]);
}
}
};
var utility = function (script, param, option) {
var responder = $wnd.OnFMReady.respondTo[script];
return responder
? responder(param, option)
: $wnd.OnFMReady.noLogging
? null
: console.log(Object.assign({ script: script, param: param }, !!option ? { option: option } : {}));
};
var STORE = DEFAULT;
var fallback;
var awaitingSet;
document.addEventListener('DOMContentLoaded', function () {
if (!iexplore || (!!iexplore && !iexplore.stash)) {
STORE = null;
awaitingSet = true;
}
setTimeout(function () {
setTimeout(function () {
$wnd.FileMaker;
});
});
});
Object.defineProperty(window, 'FileMaker', {
set: function (value) {
STORE = value;
awaitingSet = false;
clearTimeout(fallback);
if (value == undefined)
return;
setTimeout(function () {
var str = STORE;
var dispatcher = (str === null || str === void 0 ? void 0 : str.PerformScriptWithOption) || str.PerformScript;
deferred.forEach(function (d) {
dispatcher.apply(void 0, d);
});
deferred = [];
events();
});
},
get: function () {
if (iexplore && !iexplore.stash && !awaitingSet) {
iexplore.resolver();
if (iexplore.stash)
return null;
}
if (awaitingSet) {
iexplore = undefined;
fallback = setTimeout(function () {
unfulfilled = true;
$wnd.FileMaker = $wnd.OnFMReady.unmount ? undefined : DEFAULT;
});
}
return STORE;
}
});
})();