1/**
2 * Simplyfies AJAX requests for types
3 *
4 * @param {string} column A configured column in the form schema.name
5 * @param {function} fn Callback on success
6 * @param {object} data Additional data to pass
7 */
8function struct_ajax(column, fn, data) {
9    if (!data) data = {};
10
11    data['call'] = 'plugin_struct';
12    data['column'] = column;
13    data['id'] = JSINFO.id;
14    data['ns'] = JSINFO.namespace;
15
16    jQuery.post(DOKU_BASE + 'lib/exe/ajax.php', data, fn, 'json')
17        .fail(function (result) {
18            if (result.responseJSON) {
19                if (result.responseJSON.stacktrace) {
20                    console.error(result.responseJSON.error + "\n" + result.responseJSON.stacktrace);
21                }
22                alert(result.responseJSON.error);
23            } else {
24                // some fatal error occured, get a text only version of the response
25                alert(jQuery(result.responseText).text());
26            }
27        });
28}
29
30/**
31 * @param {string} val
32 * @return {Array}
33 */
34function split(val) {
35    return val.split(/,\s*/);
36}
37
38/**
39 * @param {string} term
40 * @returns {string}
41 */
42function extractLast(term) {
43    return split(term).pop();
44}
45
46
47/**
48 * Replace numbered placeholders in a string with the given arguments
49 *
50 * Example formatString('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET');
51 *
52 * adapted from http://stackoverflow.com/a/4673436/3293343
53 * @param format
54 * @returns {*}
55 */
56function formatString(format) {
57    var args = Array.prototype.slice.call(arguments, 1);
58    return format.replace(/{(\d+)}/g, function (match, number) {
59        return typeof args[number] != 'undefined'
60            ? args[number]
61            : match
62            ;
63    });
64}
65
66/**
67 * Custom onSelect handler for struct img button
68 */
69window.insertStructMedia = function (edid, mediaid, opts, align) {
70    jQuery(document.getElementById(edid)).val(mediaid).change();
71};
72