move stuff out of test and into xosHelper.js for better code reuse
[plstackapi.git] / planetstack / core / xoslib / static / js / xoslib / xosHelper.js
1 XOSApplication = Marionette.Application.extend({
2     errorBoxId: "#errorBox",
3     errorCloseButtonId: "#close-error-box",
4     successBoxId: "#successBox",
5     successCloseButtonId: "#close-success-box",
6     errorTemplate: "#xos-error-template",
7     successTemplate: "#xos-success-template",
8
9     hideError: function(result) {
10         $(this.errorBoxId).hide();
11         $(this.successBoxId).hide();
12     },
13
14     showSuccess: function(result) {
15          $(this.successBoxId).show();
16          $(this.successBoxId).html(_.template($(this.successTemplate).html())(result));
17          $(this.successCloseButtonId).unbind().bind('click', function() {
18              $(this.successBoxId).hide();
19          });
20     },
21
22     showError: function(result) {
23          $(this.errorBoxId).show();
24          $(this.errorBoxId).html(_.template($(this.errorTemplate).html())(result));
25          $(this.errorCloseButtonId).unbind().bind('click', function() {
26              $(this.errorBoxId).hide();
27          });
28     },
29 });
30
31 /* Give an id, the name of a collection, and the name of a field for models
32    within that collection, lookup the id and return the value of the field.
33 */
34
35 idToName = function(id, collectionName, fieldName) {
36     linkedObject = xos[collectionName].get(id);
37     if (linkedObject == undefined) {
38         return "#" + id;
39     } else {
40         return linkedObject.attributes[fieldName];
41     }
42 };
43
44 /* Constructs lists of <option> html blocks for items in a collection.
45
46    selectedId = the id of an object that should be selected, if any
47    collectionName = name of collection
48    fieldName = name of field within models of collection that will be displayed
49 */
50
51 idToOptions = function(selectedId, collectionName, fieldName) {
52     result=""
53     for (index in xos[collectionName].models) {
54         linkedObject = xos[collectionName].models[index];
55         linkedId = linkedObject["id"];
56         linkedName = linkedObject.attributes[fieldName];
57         if (linkedId == selectedId) {
58             selected = " selected";
59         } else {
60             selected = "";
61         }
62         result = result + '<option value="' + linkedId + '"' + selected + '>' + linkedName + '</option>';
63     }
64     return result;
65 };
66
67 /* Constructs an html <select> and the <option>s to go with it.
68
69    variable = variable name to return to form
70    selectedId = the id of an object that should be selected, if any
71    collectionName = name of collection
72    fieldName = name of field within models of collection that will be displayed
73 */
74
75 idToSelect = function(variable, selectedId, collectionName, fieldName) {
76     result = '<select name="' + variable + '">' +
77              idToOptions(selectedId, collectionName, fieldName) +
78              '</select>';
79     return result;
80 }
81