aboutsummaryrefslogtreecommitdiff
path: root/data/assets/js/util.js
blob: a812f1f94cfd30682406c012526436c859bdaddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
jQuery(function($) {
  "use strict";

  window.send = function(fn, args) {
    return $.ajax({
      url: "/guff/api/" + fn,
      method: 'POST',
      dataType: 'json',
      data: args,
    });
  };

  window.gripe = function(r, action) {
    // get error
    var error = r.responseText;

    // handle json response
    try {
      var data = $.parseJSON(r.responseText);
      if (data.error)
        error = data.error;
    } catch (e) {}

    // build prefix
    var prefix = 'Error: ';
    if  (action)
      prefix += " Couldn't " + action + ": ";

    // display error message
    alert(prefix + error);
  };

  // slugify string
  window.slugify = function(s) {
    return s.toLowerCase()
      .replace(/[^a-z0-9_-]+/g, '-')
      .replace(/^-+|-+$/g, '');
  };

  // convert textarea to jsonified array of strings
  window.listify = function(el) {
    var s = $(el).val().replace(/^\s+|\s+$/m, '');
    return JSON.stringify((s.length > 0) ? s.split(/\s+/m) : []);
  }


  LuigiTemplate.FILTERS.json = function(val) {
    return JSON.stringify(val);
  };
});