| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 function createDescriptionList(items) { | |
| 27 var list = document.createElement('dl'); | |
| 28 items.forEach(function(pair) { | |
| 29 var dt = document.createElement('dt'); | |
| 30 dt.appendChild(pair[0]); | |
| 31 var dd = document.createElement('dd'); | |
| 32 dd.appendChild(pair[1]); | |
| 33 list.appendChild(dt); | |
| 34 list.appendChild(dd); | |
| 35 }); | |
| 36 return list; | |
| 37 } | |
| 38 | |
| 39 function fetchResource(url, method, queryParameters, callback, errorCallback) { | |
| 40 var xhr = new XMLHttpRequest(); | |
| 41 xhr.onreadystatechange = function() { | |
| 42 if (this.readyState !== 4) | |
| 43 return; | |
| 44 // Allow a status of 0 for easier testing with local files. | |
| 45 if (!this.status || this.status === 200) | |
| 46 callback(this); | |
| 47 else if (errorCallback) | |
| 48 errorCallback(this); | |
| 49 }; | |
| 50 | |
| 51 if (method === 'GET' && queryParameters) | |
| 52 url = addQueryParametersToURL(url, queryParameters); | |
| 53 | |
| 54 xhr.open(method, url); | |
| 55 | |
| 56 if (method === 'GET') { | |
| 57 xhr.send(); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 var data = urlEncodeQueryParameters(queryParameters); | |
| 62 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
| 63 xhr.send(data); | |
| 64 } | |
| 65 | |
| 66 function getResource(url, callback, errorCallback) { | |
| 67 fetchResource(url, 'GET', null, callback, errorCallback); | |
| 68 } | |
| 69 | |
| 70 function urlEncodeQueryParameters(queryParameters) { | |
| 71 var encodedParameters = Object.keys(queryParameters).map(function(key) { | |
| 72 return key + '=' + encodeURIComponent(queryParameters[key]); | |
| 73 }); | |
| 74 return encodedParameters.join('&'); | |
| 75 } | |
| 76 | |
| 77 function addQueryParametersToURL(url, queryParameters) { | |
| 78 if (url.indexOf('?') < 0) | |
| 79 url += '?'; | |
| 80 else | |
| 81 url += '&'; | |
| 82 | |
| 83 return url + urlEncodeQueryParameters(queryParameters); | |
| 84 } | |
| 85 | |
| 86 function longestCommonPathPrefix(paths) { | |
| 87 const separator = '/'; | |
| 88 | |
| 89 var splitPaths = paths.map(function(path) { return path.split(separator) }); | |
| 90 var firstSplitPath = splitPaths.shift(); | |
| 91 | |
| 92 var result = []; | |
| 93 for (var i = 0; i < firstSplitPath.length; ++i) { | |
| 94 if (!splitPaths.every(function(splitPath) { return splitPath[i] === firs
tSplitPath[i] })) | |
| 95 break; | |
| 96 result.push(firstSplitPath[i]); | |
| 97 } | |
| 98 | |
| 99 if (!result.length) | |
| 100 return null; | |
| 101 return result.join(separator); | |
| 102 } | |
| 103 | |
| 104 function removePathExtension(string) { | |
| 105 var dotIndex = string.lastIndexOf('.'); | |
| 106 if (dotIndex < 0) | |
| 107 return string; | |
| 108 return string.substring(0, dotIndex); | |
| 109 } | |
| 110 | |
| 111 function sorted(array, sortFunction) { | |
| 112 var newArray = array.slice(); | |
| 113 newArray.sort(sortFunction); | |
| 114 return newArray; | |
| 115 } | |
| 116 | |
| 117 Array.prototype.contains = function(value) { | |
| 118 return this.indexOf(value) >= 0; | |
| 119 } | |
| 120 | |
| 121 Array.prototype.findFirst = function(predicate) { | |
| 122 for (var i = 0; i < this.length; ++i) { | |
| 123 if (predicate(this[i])) | |
| 124 return this[i]; | |
| 125 } | |
| 126 return null; | |
| 127 } | |
| 128 | |
| 129 Array.prototype.findLast = function(predicate) { | |
| 130 for (var i = this.length - 1; i >= 0; --i) { | |
| 131 if (predicate(this[i])) | |
| 132 return this[i]; | |
| 133 } | |
| 134 return null; | |
| 135 } | |
| 136 | |
| 137 Array.prototype.last = function() { | |
| 138 if (!this.length) | |
| 139 return undefined; | |
| 140 return this[this.length - 1]; | |
| 141 } | |
| 142 | |
| 143 Element.prototype.hasStyleClass = function(klass) { | |
| 144 var regex = new RegExp('\\b' + klass + '\\b'); | |
| 145 return regex.test(this.className); | |
| 146 } | |
| 147 | |
| 148 Element.prototype.addStyleClass = function(klass) { | |
| 149 if (this.hasStyleClass(klass)) | |
| 150 return; | |
| 151 this.className += ' ' + klass; | |
| 152 } | |
| 153 | |
| 154 Element.prototype.removeStyleClass = function(klass) { | |
| 155 var regex = new RegExp('\\b' + klass + '\\b', 'g'); | |
| 156 this.className = this.className.replace(regex, ''); | |
| 157 } | |
| 158 | |
| 159 Element.prototype.toggleStyleClass = function(klass) { | |
| 160 if (this.hasStyleClass(klass)) | |
| 161 this.removeStyleClass(klass); | |
| 162 else | |
| 163 this.addStyleClass(klass); | |
| 164 } | |
| 165 | |
| 166 Node.prototype.appendChildren = function(children) { | |
| 167 for (var i = 0; i < children.length; ++i) | |
| 168 this.appendChild(children[i]); | |
| 169 } | |
| 170 | |
| 171 Node.prototype.removeAllChildren = function() { | |
| 172 while (this.firstChild) | |
| 173 this.removeChild(this.firstChild); | |
| 174 } | |
| 175 | |
| 176 String.prototype.contains = function(substring) { | |
| 177 return this.indexOf(substring) >= 0; | |
| 178 } | |
| OLD | NEW |