move code up one directory
[atutor.git] / mods / _standard / photos / include / imageReorderer.js
1 /*
2 Copyright 2007-2009 University of Cambridge
3 Copyright 2007-2009 University of Toronto
4 Copyright 2007-2009 University of California, Berkeley
5
6 Licensed under the Educational Community License (ECL), Version 2.0 or the New
7 BSD license. You may not use this file except in compliance with one these
8 Licenses.
9
10 You may obtain a copy of the ECL 2.0 License and BSD License at
11 https://source.fluidproject.org/svn/LICENSE.txt
12 */
13
14 /*global jQuery*/
15 /*global fluid*/
16 /*global atutor*/
17
18 var atutor = atutor || {};
19 (function (jQuery, fluid) {
20         var afterMoveListener = function (thePhotoThatMoved, position, allPhotos) {
21                 // Loop through each item in the ordered list and update its hidden form field.
22                 allPhotos.each(function (idx, photo) {
23                         jQuery(photo).children("a").children("input").val(idx+1);
24                 }); 
25
26                 //POST it back to the server
27                 postOrder();
28         };
29
30         // Serialize the form and post it back to the server.
31         var postOrder = function () {
32                 var form = jQuery("#reorder-images-form"); // Get the form out of the DOM
33                 var photoRequest = jQuery(form).serialize(); // Use jQuery to serialize it into a standard form request.
34
35                 // Send it back to the server via an AJAX POST request.
36                 jQuery.ajax({
37                         type: "post",
38                         url: form.attr('action'), 
39                         data: photoRequest, 
40                         complete: function (data, ajaxStatus) {
41                                 // Handle success or failure by being nice to the user.
42                         }
43                 });
44         };
45
46     
47     /**
48      * Which actually uses the Grid reorderer
49      */
50     atutor.formBasedImageReorderer = function () {
51         var reorderer = fluid.reorderGrid("#reorder-images-form", {
52             selectors: {
53                 movables: ".photo_wrapper"
54             },
55             listeners: {
56                 afterMove: afterMoveListener
57             }
58         });
59     };
60 })(jQuery, fluid);