ce6e7f991bbb9cd6015c7370bec8cb0c33493058
[atutor.git] / mods / photo_album / fluid / component-templates / js / fluid / Fluid.js
1 /*
2 Copyright 2007 University of Cambridge
3 Copyright 2007 University of Toronto
4
5 Licensed under the Educational Community License (ECL), Version 2.0 or the New
6 BSD license. You may not use this file except in compliance with one these
7 Licenses.
8
9 You may obtain a copy of the ECL 2.0 License and BSD License at
10 https://source.fluidproject.org/svn/LICENSE.txt
11 */
12
13 var fluid = fluid || {};
14
15 fluid.keys = {
16     UP: 38,
17     DOWN: 40,
18     LEFT: 37,
19     RIGHT: 39,
20     SPACE: 32,
21     ENTER: 13,
22     TAB: 9,
23     CTRL: 17
24 };
25
26 fluid.orientation = {
27         HORIZONTAL: 0,
28         VERTICAL: 1
29 };
30
31 fluid.position = {
32         BEFORE: 0,
33         AFTER: 1
34 };
35
36 fluid.mixin = function (target, args) {
37     for (var arg in args) {
38         if (args.hasOwnProperty (arg)) {
39             target[arg] = args[arg];
40         }
41     }
42 };
43   
44 fluid.deriveLightboxCellBase = function (namebase, index) {
45     return namebase + "lightbox-cell:" + index + ":";
46 };
47     
48 // Client-level initialisation for the lightbox, allowing parameterisation for
49 // different templates.
50 fluid.initLightbox = function (namebase, messageNamebase) {
51     var reorderform = fluid.Utilities.findForm (document.getElementById (namebase));
52         
53     // Remove the anchors from the taborder - camel case 'tabIndex' needed for IE7 support
54     jQuery ("a", reorderform).attr ("tabIndex", "-1");
55     
56     // An <input> tag nested within our root namebase tag, which has an id which 
57     // begins with the  namebase:lightbox-cell:: prefix, and ends with "reorder-index" trail.
58     // Very hard to imagine any perversity which may lead to this picking any stray stuff :P
59     
60     // An approach based on the "sourceIndex" DOM property would be much more efficient,
61     // but this is only supported in IE. 
62     var orderChangedCallback = function() {
63         var inputs = fluid.Utilities.seekNodesById(
64             reorderform, 
65             "input", 
66             "^" + fluid.deriveLightboxCellBase (namebase, "[^:]*") + "reorder-index$");
67         
68         for (var i = 0; i < inputs.length; ++ i) {
69             inputs[i].value = i;
70         }
71
72         if (reorderform && reorderform.action) {
73             jQuery.post(reorderform.action, 
74             jQuery(reorderform).serialize(),
75             function (type, data, evt) { /* No-op response */ });
76         }
77     };
78     
79     // This orderable finder knows that the lightbox thumbnails are 'div' elements
80     var lightboxCellNamePattern = "^" + fluid.deriveLightboxCellBase (namebase, "[0-9]+") +"$";
81     var lightboxOrderableFinder = function (containerEl) {
82         return fluid.Utilities.seekNodesById (containerEl, "div", lightboxCellNamePattern);
83     };
84     
85     var lightboxContainer = jQuery ("[id=" + namebase + "]");
86     var layoutHandlerParams = {
87         orderableFinder: lightboxOrderableFinder,
88         container: lightboxContainer,
89         orderChangedCallback: orderChangedCallback
90     };
91     var lightbox = new fluid.Reorderer (lightboxContainer, {
92             messageNamebase : messageNamebase,
93             layoutHandler: new fluid.GridLayoutHandler (layoutHandlerParams),
94             orderableFinder: lightboxOrderableFinder
95         }
96     );
97     
98     fluid.Lightbox.addThumbnailActivateHandler (lightbox);
99 };
100   
101
102
103 /*
104  * Utilities object for providing various general convenience functions
105  */
106 fluid.Utilities = {};
107
108 // Custom query method seeks all tags descended from a given root with a 
109 // particular tag name, whose id matches a regex. The Dojo query parser
110 // is broken http://trac.dojotoolkit.org/ticket/3520#preview, this is all
111 // it might do anyway, and this will be plenty fast.
112 fluid.Utilities.seekNodesById = function (rootnode, tagname, idmatch) {
113     var inputs = rootnode.getElementsByTagName (tagname);
114     var togo = [];
115     for (var i = 0; i < inputs.length; ++ i) {
116         var input = inputs[i];
117         var id = input.id;
118         if (id && id.match (idmatch)) {
119             togo.push (input);
120         }
121     }
122     return togo;
123 };
124       
125 fluid.Utilities.escapeSelector = function(id) {
126     return id.replace (/\:/g,"\\:");
127 };
128   
129 fluid.Utilities.findForm = function (element) {
130     while(element) {
131         if (element.nodeName.toLowerCase() === "form") {
132             return element;
133         }
134         element = element.parentNode;
135     }
136 };