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();
2830var 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.
3337Sitemap . 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+
158192var 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} ) ;
0 commit comments