Merge branch 'release-0.14' into debian-release-0.14
[snf-cloudcms] / cloudcms / static / cloudcms / js / resources_list.js
1 // Array Remove - By John Resig (MIT Licensed)\r
2 Array.prototype.remove = function(from, to) {\r
3   var rest = this.slice((to || from) + 1 || this.length);\r
4   this.length = from < 0 ? this.length + from : from;\r
5   return this.push.apply(this, rest);\r
6 };\r
7 \r
8 function ResourcesModule(el, conf) {\r
9     \r
10     var defaults = {'page_limit': 5};\r
11     this.conf = $.extend(defaults, conf);\r
12 \r
13     this.el = el;\r
14     \r
15     this.selected_category = window.location.hash.replace("#","");\r
16     if (!this.selected_category) { this.selected_category = undefined };\r
17     \r
18     this.categories = this.el.find(".categories ul li");\r
19     this.orig_categories = this.categories.clone();\r
20         \r
21     //this.update_page_objects();\r
22     \r
23     this.grid_height = 251;\r
24     this.grid_width = 251;\r
25     this.grid_gap = 22;\r
26 \r
27     var self = this;\r
28     $(window).bind('hashchange', function() {\r
29         self.selected_category = window.location.hash.substring(1);\r
30         self.update_page_objects();\r
31         self.update_selected_category();\r
32     })\r
33 \r
34     self.update_page_objects();\r
35     self.update_selected_category();\r
36 }\r
37 \r
38 ResourcesModule.prototype.switch = function(hide, show) {\r
39     var hide = $(hide), show = $(show);\r
40     var toparent = hide.parent();\r
41     var newshow = show.clone();\r
42     toparent.append(newshow);\r
43     hide.animate({top:"-249px"})\r
44     newshow.animate({\r
45                     top:"-249px"\r
46                 }, \r
47                 {\r
48                     complete:function(){\r
49                         hide.remove();\r
50                         newshow.css({top:0});\r
51                 }\r
52     })\r
53 }\r
54 \r
55 \r
56 ResourcesModule.prototype.resources = function() {\r
57     this._resources  = this.el.find(".resource-wrapper");\r
58     return this._resources;\r
59 }\r
60 \r
61 ResourcesModule.prototype.hide = function(q) {\r
62     q.fadeOut(300);\r
63 }\r
64 \r
65 ResourcesModule.prototype.animate_els = function(els) {\r
66     $(el).css({ position:'absolute' });\r
67     var left = i % 3 == 0 ? 0 : (i % 3) * (self.grid_width) + ((i % 3)-1) * self.grid_gap;\r
68 \r
69     var row = Math.floor(i/3);\r
70     var top = row * (self.grid_gap + self.grid_height);\r
71 \r
72     $(el).animate({left: left}, { complete: function(){\r
73         $(el).animate({top: top}, {complete: function(){\r
74         \r
75             self.el.height((self.grid_height + self.grid_gap) * (Math.floor(to_show.length/3) + 1));\r
76         \r
77         }});\r
78     }});\r
79     $(el).removeClass("first");\r
80 \r
81     if (i % 3 == 0) { \r
82         $(el).addClass("first");\r
83     }\r
84     $(el).removeClass("hidden");\r
85     $(el).show();\r
86 }\r
87 \r
88 ResourcesModule.prototype.update_selected_category = function() {\r
89     if (!this.selected_category) {\r
90         this.categories.removeClass("inactive").removeClass("active");\r
91         this.categories.show();\r
92         this.el.find(".categories a.clear").hide();\r
93         return;\r
94     }\r
95 \r
96     var to_hide = this.categories.filter("[data-id="+this.selected_category+"]");\r
97     var to_show = this.categories.filter("[data-id!="+this.selected_category+"]");\r
98 \r
99     to_show.removeClass("active").addClass("inactive");\r
100     to_hide.removeClass("inactive").addClass("active");\r
101     this.el.find(".categories a.clear").show();\r
102 }\r
103 \r
104 ResourcesModule.prototype.update_page_objects = function() {\r
105     var to_show = this.resources().filter("[data-category="+this.selected_category+"]");\r
106     var to_hide = this.resources().filter("[data-category!="+this.selected_category+"]");\r
107         \r
108     if (!this.selected_category) { to_show = this.resources(); }\r
109     _.each(to_hide, function(el){\r
110         $(el).hide();\r
111     });\r
112 \r
113     _.each(to_show, function(el, i){\r
114         if (i%3 == 0) {\r
115             $(el).addClass("first");\r
116         } else {\r
117             $(el).removeClass("first");\r
118         }\r
119         $(el).fadeIn(40);\r
120     });\r
121 }\r
122 \r
123 $(document).ready(function(){\r
124     var rm = new ResourcesModule($("#resources-list"), {}, []);\r
125     window.rm = rm;\r
126 })\r
127 \r
128 \r