|
| 1 | +document.addEventListener 'DOMContentLoaded', -> |
| 2 | + |
| 3 | + window.$$ = (el) -> document.querySelectorAll(el) |
| 4 | + window.$ = (el) -> $$(el)[0] |
| 5 | + |
| 6 | + ### |
| 7 | + CRUD DOCUMENTATION SETTINGS |
| 8 | + ### |
| 9 | + |
| 10 | + # Default settings |
| 11 | + defaults = |
| 12 | + viewOptions: |
| 13 | + jquery: false |
| 14 | + semantic: true |
| 15 | + |
| 16 | + booleanViewOptions = ['jquery', 'semantic'] |
| 17 | + |
| 18 | + # Retrieve user's saved settings |
| 19 | + options = JSON.parse localStorage.getItem 'kickstartDocs' |
| 20 | + |
| 21 | + # Create shallow extend function |
| 22 | + # (http://andrewdupont.net/2009/08/28/deep-extending-objects-in-javascript/) |
| 23 | + extend = (destination, source) -> |
| 24 | + for property of source |
| 25 | + if source[property] and source[property].constructor and source[property].constructor is Object |
| 26 | + destination[property] = destination[property] or {} |
| 27 | + arguments.callee destination[property], source[property] |
| 28 | + else |
| 29 | + destination[property] = source[property] |
| 30 | + destination |
| 31 | + |
| 32 | + # Extend from defaults |
| 33 | + settings = if options then extend defaults, options else defaults |
| 34 | + |
| 35 | + # Create function to write to localStorage |
| 36 | + setSettings = (settings) -> |
| 37 | + localStorage.setItem 'kickstartDocs', JSON.stringify settings |
| 38 | + |
| 39 | + for option in booleanViewOptions |
| 40 | + if settings.viewOptions["#{option}"] |
| 41 | + document.body.classList.add "show-#{option}" |
| 42 | + else |
| 43 | + document.body.classList.remove "show-#{option}" |
| 44 | + |
| 45 | + # Write to localStorage |
| 46 | + setSettings(settings) |
| 47 | + |
| 48 | + els = [] |
| 49 | + for option in booleanViewOptions |
| 50 | + option = "#docs-#{option}" |
| 51 | + els.push option |
| 52 | + |
| 53 | + if $$(els).length |
| 54 | + # This page has checkboxes for view options. |
| 55 | + for option in booleanViewOptions |
| 56 | + # Closure needed for event listeners |
| 57 | + do (option) -> |
| 58 | + window["$opt#{option}"] = $ "#docs-#{option}" |
| 59 | + |
| 60 | + # Set state of buttons based on saved options in localStorage |
| 61 | + window["$opt#{option}"].checked = (if settings.viewOptions["#{option}"] then true else false) |
| 62 | + |
| 63 | + # Listen for checkbox changes |
| 64 | + window["$opt#{option}"].addEventListener 'click', -> |
| 65 | + settings.viewOptions["#{option}"] = this.checked |
| 66 | + setSettings settings |
| 67 | + |
| 68 | + # Show growls |
| 69 | + if k$.$('#example-showGrowl') |
| 70 | + k$.$('#example-showGrowl').addEventListener 'click', -> |
| 71 | + growls = [ |
| 72 | + { |
| 73 | + title: 'Document Saved.', |
| 74 | + text: 'Your document was successfully saved.' |
| 75 | + type: 'alert-green' |
| 76 | + }, |
| 77 | + { |
| 78 | + title: 'Library book not found' |
| 79 | + text: 'Sorry, we could find that library book.', |
| 80 | + type: 'alert-red' |
| 81 | + }, |
| 82 | + { |
| 83 | + title: 'Wide clearance selection', |
| 84 | + text: 'Remember to check out our clearance', |
| 85 | + type: 'alert-blue' |
| 86 | + }, |
| 87 | + { |
| 88 | + title: 'Deadline approaching', |
| 89 | + text: 'Friendly reminder that your deadline is quickly approaching.', |
| 90 | + type: 'alert-yellow' |
| 91 | + } |
| 92 | + ] |
| 93 | + |
| 94 | + k$.exampleCounter++ |
| 95 | + k$.exampleCounter = 0 if not k$.exampleCounter or k$.exampleCounter > 3 |
| 96 | + |
| 97 | + k$.growl growls[k$.exampleCounter] |
| 98 | + |
| 99 | + # Show status message |
| 100 | + if k$.$('#example-showStatus') |
| 101 | + k$.$('#example-showStatus').addEventListener 'click', -> |
| 102 | + statuses = [ |
| 103 | + { |
| 104 | + text: 'Document Saved.', |
| 105 | + type: 'status-green' |
| 106 | + }, |
| 107 | + { |
| 108 | + text: 'Sorry, we could find that library book.', |
| 109 | + type: 'status-red' |
| 110 | + }, |
| 111 | + { |
| 112 | + text: 'Remember to check out our clearance', |
| 113 | + type: 'status-blue' |
| 114 | + }, |
| 115 | + { |
| 116 | + text: 'Deadline is approaching!', |
| 117 | + type: 'status-yellow' |
| 118 | + } |
| 119 | + ] |
| 120 | + |
| 121 | + k$.exampleCounter++ |
| 122 | + k$.exampleCounter = 0 if not k$.exampleCounter or k$.exampleCounter > 3 |
| 123 | + |
| 124 | + k$.status(statuses[k$.exampleCounter]) |
| 125 | + |
| 126 | + k$.slugify = (str) -> |
| 127 | + `str.toLowerCase().replace(/ /g,'-').replace(/[^\w-]+/g,'')` |
| 128 | + |
| 129 | + # Create a table of contents |
| 130 | + |
| 131 | + if k$.$$('#toc').length |
| 132 | + k$.$('.creating-table').parentNode.removeChild k$.$('.creating-table') |
| 133 | + $toc = document.createElement 'ul' |
| 134 | + $toc.className = "list list-unstyled" |
| 135 | + $link = document.createElement('li') |
| 136 | + $link.innerHTML = '<a></a>' |
| 137 | + |
| 138 | + # Assuming proper html, start with h1. |
| 139 | + $headingLevel = 1 |
| 140 | + |
| 141 | + # The node we're currently appending to. Always a ul. |
| 142 | + $targetNode = $toc |
| 143 | + |
| 144 | + $documentContainer = k$.$('.document-container') |
| 145 | + for heading in $documentContainer.querySelectorAll('h1, h2, h3, h4, h5, h6') |
| 146 | + # Ignore headings that declare themselves as exempt from the TOC |
| 147 | + if not heading.classList.contains 'toc-exempt' |
| 148 | + # For extra unique names. |
| 149 | + # heading.id = "#{k$.slugify heading.innerHTML}-#{_k}" |
| 150 | + |
| 151 | + heading.id = k$.slugify heading.innerHTML |
| 152 | + |
| 153 | + # If this is a lower level. |
| 154 | + $thisHeadingLevel = parseInt(heading.tagName.substr(1, 2)) |
| 155 | + |
| 156 | + if $thisHeadingLevel > $headingLevel |
| 157 | + # Append a new submenu and make that the targetNode. |
| 158 | + $newSubmenu = document.createElement 'ul' |
| 159 | + $targetNode.children[$targetNode.children.length - 1].appendChild $newSubmenu |
| 160 | + $targetNode = $newSubmenu |
| 161 | + $headingLevel = $thisHeadingLevel |
| 162 | + |
| 163 | + # If this is a higher level |
| 164 | + if $thisHeadingLevel < $headingLevel |
| 165 | + $stepsUp = $headingLevel - $thisHeadingLevel |
| 166 | + |
| 167 | + while $stepsUp > -1 |
| 168 | + $targetNode = $targetNode.parentNode |
| 169 | + $stepsUp-- |
| 170 | + |
| 171 | + $headingLevel = $thisHeadingLevel |
| 172 | + |
| 173 | + # Make a new li and append it to the target ul node. |
| 174 | + $menuItem = $link.cloneNode true |
| 175 | + $menuItem.querySelector('a').href = "##{heading.id}" |
| 176 | + $menuItem.querySelector('a').innerHTML = heading.innerHTML |
| 177 | + $targetNode.appendChild $menuItem |
| 178 | + |
| 179 | + k$.$('#toc').appendChild $toc |
0 commit comments