summaryrefslogtreecommitdiff
path: root/public/javascripts/admin_search.js
blob: f553334df46fbac3c48442f1dbe76899efa067d7 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
admin_search = {
  initialize : function() {
    $(document).bind("keydown", 'Alt+f', function(){
      admin_search.display_toggle();
      return false;
    });

    $(document).bind("keydown", function(e) {
      if (e.key === "Escape" && $('#search_widget').is(':visible')) {
        $('#search_widget').fadeOut();
      }
    });

    $(document).bind("click", function(e) {
      if ($('#search_widget').is(':visible') &&
          !$(e.target).closest('#search_widget').length &&
          !$(e.target).closest('a[onclick*="display_toggle"]').length) {
        $('#search_widget').fadeOut();
      }
    });

    initSearchPicker({
      inputSelector: "#search_term",
      resultsSelector: "#menu_search_results",
      url: ADMIN_SEARCH_URL,
      isActive: function() { return $('#search_widget').is(':visible'); },
      resultsHeaderHtml: "<p class='search_more'>Press Enter to see all results ⏎</p>"
    });
  },

  display_toggle : function() {
    if ($('#search_widget').is(':visible')) {
      $('#search_widget').fadeOut();
    } else {
      $('#search_widget').fadeIn();
      $('#search_term').focus();
    }
  }
};

function initSearchPicker(options) {
  var inputSelector = options.inputSelector;
  var resultsSelector = options.resultsSelector;
  var url = options.url || ADMIN_MENU_SEARCH_URL;
  var onSelect = options.onSelect;               // omit for a real-navigation search like admin_search
  var isActive = options.isActive;                 // optional guard, checked before every search
  var resultsHeaderHtml = options.resultsHeaderHtml; // optional, prepended only when results exist
  var requestId = 0;
  var timeout;

  $(inputSelector).bind("input", function() {
    if (isActive && !isActive()) return;

    var term = $(this).val();
    var results = $(resultsSelector);
    clearTimeout(timeout);

    if (!term) {
      results.slideUp();
      results.empty();
      return;
    }

    timeout = setTimeout(function() {
      var thisRequest = ++requestId;
      $.ajax({
        type: "GET",
        url: url,
        data: "search_term=" + term,
        dataType: "json",
        success: function(nodes) {
          if (thisRequest !== requestId) return;
          results.empty();
          if (nodes.length && resultsHeaderHtml) {
            results.append(resultsHeaderHtml);
          }
          var found = false;
          for (var i = 0; i < nodes.length; i++) {
            (function(node) {
              var link;
              if (onSelect) {
                link = $(
                  "<p><a href='#'>" + node.title +
                    "<span class='result_path'>" + node.unique_name + "</span>" +
                  "</a></p>"
                );
                link.find("a").bind("click", function() {
                  onSelect(node);
                  results.slideUp();
                  results.empty();
                  return false;
                });
              } else {
                link = $(
                  "<p><a href='" + node.node_path + "'>" + node.title +
                    "<span class='result_path'>" + node.unique_name + "</span>" +
                  "</a></p>"
                );
              }
              results.append(link);
              found = true;
            })(nodes[i]);
          }
          if (found) {
            results.slideDown();
          } else {
            results.slideUp();
          }
        },
        error: function(xhr, status, error) {
          console.log("Ajax error:", status, error, xhr.status, xhr.responseText);
        }
      });
    }, 250);
  });
}

menu_items = {
  initialize_search : function() {
    initSearchPicker({
      inputSelector: "#menu_search_term",
      resultsSelector: "#menu_item_search_results",
      onSelect: function(node) {
        $("#menu_item_node_id").val(node.node_id);
        $("#menu_item_path").val("/" + node.unique_name);
        $("#menu_item_title").val(node.title);
      }
    });
  }
};

parent_search = {
  initialize_search : function() {
    parent_search.initialize_radio_buttons();

    initSearchPicker({
      inputSelector: "#parent_search_term",
      resultsSelector: "#parent_search_results",
      onSelect: function(node) {
        $("#parent_search_term").val(node.title);
        $("#parent_id").val(node.node_id).attr("data-unique-name", node.unique_name);
        parent_search.update_resulting_path();
      }
    });

    $("#title").bind("input", function() {
      parent_search.update_resulting_path();
    });

    $("#copy_resulting_path").bind("click", function() {
      var path = $("#resulting_path").text();
      if (path === "—" || !navigator.clipboard) return;
      navigator.clipboard.writeText(path);
    });
  },

  update_resulting_path : function() {
    var kind  = $("input[name='kind']:checked");
    var title = $("#title").val();

    if (title === "") {
      $("#resulting_path").text("—");
      return;
    }

    var prefix = kind.val() === "generic"
      ? ($("#parent_id").attr("data-unique-name") || "")
      : (kind.attr("data-path-prefix") || "");

    clearTimeout(parent_search.path_timeout);
    parent_search.path_timeout = setTimeout(function() {
        $.get(PARAMETERIZE_PREVIEW_URL, { title: title }, function(slug) {
        $("#resulting_path").text(window.location.origin + "/" + (prefix ? prefix + "/" : "") + slug);
      });
    }, 300);
  },

  initialize_radio_buttons : function() {
    parent_search.sync_parent_field();
    $("input[name='kind']").bind("change", function() {
      parent_search.sync_parent_field();
      parent_search.update_resulting_path();
    });
  },

  sync_parent_field : function() {
    var kind = $("input[name='kind']:checked").val();
    if (kind === "generic") {
      $("#parent_search_field").show();
    } else {
      $("#parent_search_field").hide();
    }
  }
};

move_to_search = {
  initialize_search : function() {
    initSearchPicker({
      inputSelector: "#move_to_search_term",
      resultsSelector: "#move_to_search_results",
      onSelect: function(node) {
        $("#move_to_search_term").val(node.title);
        $("#node_staged_parent_id").val(node.node_id);
      }
    });
  }
};

event_search = {
  initialize_search : function() {
    initSearchPicker({
      inputSelector: "#event_node_search_term",
      resultsSelector: "#event_search_results",
      onSelect: function(node) {
        $("#event_node_search_term").val(node.title);
        $("#event_node_id").val(node.node_id);

        var title_field = $("#event_title");
        if (title_field.val() === "") {
          $("#event_title_hint").text("Using \"" + node.title + "\" from the associated node.");
        }
      }
    });
  }
};