1/* Copyright 2011 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16T5.extend(T5, { 17 tree : { 18 19 /** 20 * Approximate time per pixel for the hide and reveal animations. The 21 * idea is to have small (few children) and large (many childen) 22 * animations operate at the same visible rate, even though they will 23 * take different amounts of time. 24 */ 25 ANIMATION_RATE : .005, 26 27 /** 28 * Maximum animation time, in seconds. This is necessary for very large 29 * animations, otherwise its looks visually odd to see the child tree 30 * nodes whip down the screen. 31 */ 32 MAX_ANIMATION_DURATION : .5, 33 34 /** Type of Scriptaculous effect to hide/show child nodes. */ 35 TOGGLE_TYPE : 'blind', 36 37 /** 38 * Name of Scriptaculous effects queue to ensure that animations do not 39 * overwrite each other. 40 */ 41 QUEUE_NAME : 't-tree-updates' 42 } 43}); 44 45T5.extendInitializers(function() { 46 47 var cfg = T5.tree; 48 49 function doAnimate(element) { 50 var sublist = $(element).up('li').down("ul"); 51 52 var dim = sublist.getDimensions(); 53 54 var duration = Math.min(dim.height * cfg.ANIMATION_RATE, 55 cfg.MAX_ANIMATION_DURATION) 56 57 new Effect.toggle(sublist, cfg.TOGGLE_TYPE, { 58 duration : duration, 59 queue : { 60 position : 'end', 61 scope : cfg.QUEUE_NAME 62 } 63 }); 64 } 65 66 function animateRevealChildren(element) { 67 $(element).addClassName("t-tree-expanded"); 68 69 doAnimate(element); 70 } 71 72 function animateHideChildren(element) { 73 $(element).removeClassName("t-tree-expanded"); 74 75 doAnimate(element); 76 } 77 78 function initializer(spec) { 79 var loaded = spec.expanded || spec.leaf; 80 var expanded = spec.expanded; 81 var loading = false; 82 83 if (expanded) { 84 $(spec.clientId).addClassName("t-tree-expanded") 85 } 86 87 88 function successHandler(reply) { 89 // Remove the Ajax load indicator 90 $(spec.clientId).update(""); 91 $(spec.clientId).removeClassName("t-empty-node"); 92 93 var response = reply.responseJSON; 94 95 Tapestry.loadScriptsInReply(response, function() { 96 var element = $(spec.clientId).up("li"); 97 var label = element.down("span.t-tree-label"); 98 99 label.insert({ 100 after : response.content 101 }); 102 103 // Hide the new sublist so that we can animate revealing it. 104 element.down("ul").hide(); 105 106 animateRevealChildren(spec.clientId); 107 108 loading = false; 109 loaded = true; 110 expanded = true; 111 }); 112 113 } 114 115 function doLoad() { 116 if (loading) 117 return; 118 119 loading = true; 120 121 $(spec.clientId).addClassName("t-empty-node"); 122 123 $(spec.clientId).update("<span class='t-ajax-wait'/>"); 124 125 Tapestry.ajaxRequest(spec.expandChildrenURL, successHandler); 126 } 127 128 $(spec.clientId).observe("click", function(event) { 129 event.stop(); 130 131 if (!loaded && spec.expandChildrenURL) { 132 133 doLoad(); 134 135 return; 136 } 137 138 // Children have been loaded, just a matter of toggling 139 // between showing or hiding the children. 140 141 var f = expanded ? animateHideChildren : animateRevealChildren; 142 143 f.call(null, spec.clientId); 144 145 var url = expanded ? spec.markCollapsedURL : spec.markExpandedURL; 146 147 // Send request, ignore response. 148 149 Tapestry.ajaxRequest(url, {}); 150 151 expanded = !expanded; 152 }); 153 154 155 if (spec.selectURL) { 156 157 var selected = spec.selected; 158 159 var label = $(spec.clientId).next("span.t-tree-label"); 160 161 label.addClassName("t-selectable"); 162 163 if (selected) { 164 label.addClassName("t-selected-leaf-node-label"); 165 } 166 167 label.observe("click", function(event) { 168 event.stop(); 169 170 171 selected = ! selected; 172 173 174 if (selected) { 175 label.addClassName("t-selected-leaf-node-label"); 176 } 177 else { 178 label.removeClassName("t-selected-leaf-node-label"); 179 } 180 181 // TODO: In the future, we may want to select children when a parent is selected, 182 // or vice-versa. There's a lot of use cases. These will be directed from new methods 183 // on the TreeSelectionModel interface and encoded into the response. For now, 184 // the response is empty and ignored. 185 186 Tapestry.ajaxRequest(spec.selectURL, { parameters: { "t:selected": selected } }); 187 }); 188 } 189 } 190 191 return { 192 treeNode : initializer 193 }; 194});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.