Skip to content

Commit d75e3b1

Browse files
committed
Keep open/closed/last selected state of sitemap
* Stores the ids of the open sections in a cookie. * When page loads previously opened sections are reopened.
1 parent 4fcd29d commit d75e3b1

File tree

7 files changed

+55
-9
lines changed

7 files changed

+55
-9
lines changed

Gemfile.lock

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ PATH
1010
rails (>= 3.2.5, < 3.3.0)
1111
sass-rails
1212
term-ansicolor
13+
underscore-rails (~> 1.4)
1314

1415
GEM
1516
remote: http://rubygems.org/
@@ -95,7 +96,7 @@ GEM
9596
hike (1.2.1)
9697
i18n (0.6.1)
9798
journey (1.0.4)
98-
jquery-rails (2.2.1)
99+
jquery-rails (2.3.0)
99100
railties (>= 3.0, < 5.0)
100101
thor (>= 0.14, < 2.0)
101102
json (1.7.7)
@@ -176,20 +177,23 @@ GEM
176177
sqlite3 (1.3.6)
177178
sqlite3-ruby (1.3.3)
178179
sqlite3 (>= 1.3.3)
179-
term-ansicolor (1.0.7)
180+
term-ansicolor (1.2.2)
181+
tins (~> 0.8)
180182
thin (1.5.0)
181183
daemons (>= 1.0.9)
182184
eventmachine (>= 0.12.6)
183185
rack (>= 1.0.0)
184186
thor (0.17.0)
185187
tilt (1.3.3)
188+
tins (0.8.0)
186189
treetop (1.4.12)
187190
polyglot
188191
polyglot (>= 0.3.1)
189192
tzinfo (0.3.35)
190193
uglifier (1.3.0)
191194
execjs (>= 0.3.0)
192195
multi_json (~> 1.0, >= 1.0.2)
196+
underscore-rails (1.4.4)
193197
websocket (1.0.4)
194198
xpath (0.1.4)
195199
nokogiri (~> 1.3)

app/assets/javascripts/cms/core_library.js.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ $(function() {
154154
}
155155
});
156156

157-
//CookieSet allows us to treat one cookie value as a set of values
157+
//CookieSet allows us to treat one cookie value as a Set of values (Each value can appear only once)
158158
jQuery(function($) {
159159
var sep = '|'
160160

app/assets/javascripts/cms/new-sitemap.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//= require 'jquery'
22
//= require 'jquery-ui'
3+
//= require 'jquery.cookie'
34
//= require 'bootstrap'
45
//= require 'cms/ajax'
6+
//= require 'underscore'
57

68
// Code for working with the new sitemap structure.
79

@@ -28,6 +30,8 @@ var globalMenu = new GlobalMenu();
2830
var Sitemap = function() {
2931
};
3032

33+
Sitemap.STATE = 'cms.sitemap.opened';
34+
3135
// @return [Selector] The currently selected section in the sitemap. If a page or other child is selected, this will be
3236
// that element's parent.
3337
Sitemap.prototype.currentSection = function() {
@@ -155,6 +159,36 @@ Sitemap.prototype.move_to = function(node_id, target_node_id, position) {
155159
console.log("Dropping node", node_id, "into", target_node_id, "at position", position);
156160

157161
};
162+
163+
// @param [Selector] A selected link (<a>)
164+
Sitemap.prototype.isOpen = function(link){
165+
return link.siblings('ul').hasClass('in') == true
166+
};
167+
168+
// @param [Selector] link A selected link (<a>)
169+
// @param [String] icon The full name of the icon (icon-folder-open)
170+
Sitemap.prototype.changeIcon = function(link, icon){
171+
link.find('i:first').attr('class', icon);
172+
};
173+
174+
// @param [Number] id
175+
Sitemap.prototype.openedSection = function(id){
176+
$.cookieSet.add(Sitemap.STATE, id);
177+
};
178+
Sitemap.prototype.closedSection = function(id){
179+
$.cookieSet.remove(Sitemap.STATE, id);
180+
};
181+
182+
// Reopen all sections that the user was last working with.
183+
Sitemap.prototype.restoreOpenState = function(){
184+
var section_ids = $.cookieSet.get(Sitemap.STATE);
185+
_.each(section_ids, function(id){
186+
var link = $('.selectable[data-type="section"][data-id=' + id + ']');
187+
sitemap.changeIcon(link, 'icon-folder-open');
188+
$(link.data('target')).addClass('in');
189+
});
190+
};
191+
158192
var sitemap = new Sitemap();
159193

160194
$(function() {
@@ -182,11 +216,17 @@ $(function() {
182216

183217
// Change the folder icon when they are opened/closed.
184218
$(function() {
219+
sitemap.restoreOpenState();
220+
console.log("On page load, these sections should be open", $.cookie('sitemap.opened'));
185221
$('a[data-toggle="collapse"]').click(function() {
186-
if ($(this).siblings('ul').hasClass('in') == true) {
187-
$(this).find('i:first').attr('class', 'icon-folder-close');
222+
if (sitemap.isOpen($(this))) {
223+
// console.log("Section", $(this).data('id'), "was closed.");
224+
sitemap.closedSection($(this).data('id'));
225+
sitemap.changeIcon($(this), 'icon-folder-close');
188226
} else {
189-
$(this).find('i:first').attr('class', 'icon-folder-open');
227+
// console.log("Section", $(this).data('id'), "was opened.");
228+
sitemap.openedSection($(this).data('id'));
229+
sitemap.changeIcon($(this), 'icon-folder-open');
190230
}
191231
});
192232
});

app/views/cms/section_nodes/_section.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
data = {
55
target: "#section#{section.id}",
66
type: 'section',
7+
id: section.id,
78
node_id: key.id,
89
configure_path: edit_section_path(section),
910
delete_path: section_path(section),

browsercms.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
3535
s.add_dependency("bootstrap-sass")
3636
s.add_dependency("ancestry", "~> 1.2.4")
3737
s.add_dependency("ckeditor_rails", "~> 4.0.1.1")
38+
s.add_dependency("underscore-rails", "~> 1.4")
3839
s.add_dependency("jquery-rails", "~> 2.0")
3940
s.add_dependency("paperclip", "~> 3.0.3")
4041

lib/cms/engine.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
require 'browsercms'
66

77
require 'bootstrap-sass'
8-
98
# Gem name is different than file name
109
# Must be required FIRST, so that our assets paths appear before its do.
1110
# This allows app/assets/ckeditor/config.js to set CMS specific defaults.
@@ -15,6 +14,8 @@
1514
# especially while upgrading
1615
require 'jquery-rails'
1716

17+
require 'underscore-rails'
18+
1819
module Cms
1920

2021
class Engine < Rails::Engine

todo_ui_Revamp.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ Tasks:
99
## UI Merge
1010

1111
* Move/drag/drop sections
12-
* Keep open/closed/last selected state of sitemap
1312
* Audit/Delete/merge js/cms/sitemap.js.erb
1413
* No visual indicator of an empty section
15-
* page_editor.css/page_content_editing.css shouldn't have been deleted.
14+
* page_editor.css/page_content_editing.css shouldn't have been deleted during the bootstrap UI merge.
1615
* Can't edit the root section
1716
* Delete old sitemap pages (_section.old.erb, etc)
1817

0 commit comments

Comments
 (0)