summaryrefslogtreecommitdiff
path: root/public/javascripts/admin_interface.js
blob: 2beda982db8c06491b5f332b71d4bdc9c3ed8544 (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
$(document).ready(function () {
  admin_search.initialize();
  menu_items.initialize_search();
  meta_data.initialize();
  menu_item_sorter.initialize();
  
  jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
  })
  
  $(document).ajaxSend(function(event, request, settings) {
    if (typeof(AUTH_TOKEN) == "undefined") return;
    // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
    settings.data = settings.data || "";
    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
  });
  
});


meta_data = {
  initialize : function() {
    $("#metadata").hide();

    $("#button").click(function () {
      
      $("#metadata").slideToggle(1200);
      image_interface.initialize();

      if ($("#button").attr("class") == "unselected") {
        $("#button").attr("class", "selected");        
        
      }
      else {
        $("#button").attr("class", "unselected");
        $("#image_browser").hide();
        $("#image_browser_toggle").attr("class", "unselected");
      }
      
      return false;
    });
  }
};

cccms = {
  setup_autosave : function() {
    
    var elements = {
      title     : $('#page_title'),
      abstract  : $('#page_abstract'),
      body      : $('#page_body_ifr').contents().find('#tinymce'),
    }
  
    var page = {         
      cached_title    : elements.title.val(),
      cached_abstract : elements.abstract.val(),
      cached_body     : elements.body.html(),
  
      title_has_changed : function() {
         return (elements.title.val() != this.cached_title)
       },
  
       abstract_has_changed : function() {
         return (elements.abstract.val() != this.cached_abstract)
       },
  
       body_has_changed : function() {
         return elements.body.html() != this.cached_body
       }
    }
  
    jQuery.fn.submitWithAjax = function(options) {
      if (page.title_has_changed() || page.abstract_has_changed() || page.body_has_changed()) {
  
        page.cached_title      = elements.title.val();
        page.cached_abstract   = elements.abstract.val();
        page.cached_body       = elements.body.html();
  
        $("#flash").append("<img src='/images/ajax-loader.gif' alt='' />");
        $.post(this.attr("action"), $(this).serialize(), null, "script");
        
      }
    };
  
    setInterval('$("#page_editor > form").submitWithAjax()', 15000);
  }
}

menu_item_sorter = {
  
  initialize : function() {
    $("#menu_item_list").sortable({
      axis: 'y',
      items: 'tr',
      handle: 'td',
      placeholder: 'ui-state-highlight',
      start: function(e, ui) {
        menu_item_sorter.placeholder_helper(e,ui);
      },
      stop : function(){
        $.ajax({
          type: "POST",
          url: "/menu_items/0/sort",
          data: $(this).sortable("serialize"),
          dataType: "json",
          success : function(results) {
            alert(results);
          }
        });
      }
    });
  },
  
  placeholder_helper : function(e,ui) {
    $(".ui-state-highlight").html("<td colspan='100%'></td>");
  }
}

image_interface = {
    
  initialize : function() {
    
    $("#image_browser").hide();
    image_interface.initialize_sortable_image_box();
    image_interface.connect_browser_and_box();
    image_interface.set_droppable_behavior();
    image_interface.bind_image_browser_toggle();    
  },
  
  
  set_droppable_behavior : function() {
    $("ul#image_box").droppable({
      out : function(event, ui) {
        $(ui.draggable).fadeTo("fast", 0.4);

        $(ui.draggable).bind("mouseup", function() {
          $(this).remove();
        });
      },
      over : function(event, ui) {
        $(ui.draggable).fadeTo("fast", 1.0);
        $(ui.draggable).unbind("mouseup");
      }
    });
  },
  
  connect_browser_and_box : function() {
    $("#image_browser ul li").draggable({
      connectToSortable : 'ul#image_box',
      helper : 'clone',
      revert : 'invalid'
    });
  },
  
  initialize_sortable_image_box : function() {
    
    $("ul#image_box").sortable({
      revert  : true,
      update    : function(event, ui) {
        images = $("ul#image_box").sortable("serialize", {attribute : "rel"});

        $.ajax({
          type : "POST",
          url  : "/pages/" + $("ul#image_box").attr("rel") + "/sort_images",
          dataType : "json",
          data : images + "&_method=put",
          success : function() {
          }
        });
      }
    });    
  },
  
  bind_image_browser_toggle : function() {
    $("#image_browser_toggle").bind("click", function(){
      if ($("#image_browser_toggle").attr("class") == "unselected") {
        $("#image_browser_toggle").attr("class", "selected");
        $("#image_browser").show();
      }
      else {
        $("#image_browser_toggle").attr("class", "unselected");
        $("#image_browser").hide();
      }
      
      return false;
    });
  }
}