Changeset 6893
- Timestamp:
- 03/21/2018 03:09:34 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/style/trac/common/js/trac.js
r730 r6893 10 10 .attr("title", title).appendTo(this); 11 11 }); 12 } 13 12 }; 13 14 14 $.fn.checked = function(checked) { 15 15 if (checked == undefined) { // getter … … 21 21 }); 22 22 } 23 } 24 23 }; 24 25 // Add a Select All checkbox to each thead in the table. 26 $.fn.addSelectAllCheckboxes = function() { 27 var $table = this; 28 if ($("tr td.sel", $table).length > 0) { 29 $("tr th.sel", $table).append( 30 $('<input type="checkbox" name="toggle_group" />').attr({ 31 title: _("Toggle group") 32 }).click(function() { 33 $("tr td.sel input", 34 $(this).closest("thead, tbody").next()) 35 .prop("checked", this.checked).change(); 36 }) 37 ); 38 $("tr td.sel", $table).click(function() { 39 var $tbody = $(this).closest("tbody"); 40 var $checkboxes = $("tr td.sel input", $tbody); 41 var num_selected = $checkboxes.filter(":checked").length; 42 var none_selected = num_selected === 0; 43 var all_selected = num_selected === $checkboxes.length; 44 $("tr th.sel input", $tbody.prev()) 45 .prop({"checked": all_selected, 46 "indeterminate": !(none_selected || all_selected)}); 47 }); 48 } 49 }; 50 51 $.fn.exclusiveOnClick = function(selector) { 52 var $container = $(this); 53 $container.on("click", selector, 54 function(event) { 55 if (!event.metaKey && !event.altKey) 56 return; 57 var clicked; 58 if (this.tagName === "LABEL") { 59 if (this.htmlFor) { 60 clicked = document.getElementById(this.htmlFor); 61 } else { 62 clicked = this.children[0]; 63 } 64 } else { 65 clicked = this; 66 } 67 var $clicked = $(clicked); 68 $container.find(":checkbox").not(clicked).prop("checked", false); 69 $clicked.prop("checked", true); 70 }).mousedown(function(event) { 71 if (event.metaKey || event.altKey) { 72 event.preventDefault(); // Prevent border on Firefox. 73 } 74 }); 75 }; 76 77 // Conditionally disable the submit button. Returns a jQuery object. 78 $.fn.disableSubmit = function(determinant) { 79 determinant = $(determinant); 80 var subject = $(this); 81 var isDisabled; 82 if (determinant.is("input:checkbox")) { 83 isDisabled = function () { 84 return determinant.filter(":checked").length === 0; 85 } 86 } else if (determinant.is("input:file")) { 87 isDisabled = function () { 88 return !determinant.val(); 89 } 90 } else { 91 return subject; 92 } 93 function toggleDisabled() { 94 subject.prop("disabled", isDisabled); 95 if (subject.prop("disabled")) { 96 subject.attr("title", _("At least one item must be selected")) 97 } else { 98 subject.removeAttr("title"); 99 } 100 } 101 determinant.change(toggleDisabled); 102 toggleDisabled(); 103 return subject; 104 }; 105 25 106 $.fn.enable = function(enabled) { 26 107 if (enabled == undefined) enabled = true; … … 37 118 } 38 119 }); 39 } 40 120 }; 121 41 122 $.fn.getAbsolutePos = function() { 42 123 return this.map(function() { … … 51 132 return {left: left, top: top}; 52 133 }); 53 } 54 134 }; 135 55 136 $.fn.scrollToTop = function() { 56 137 return this.each(function() { … … 58 139 return false; 59 140 }); 60 } 61 141 }; 142 143 // Disable the form's submit action after the submit button is pressed by 144 // replacing it with a handler that cancels the action. The handler is 145 // removed when navigating away from the page so that the action will 146 // be enabled when using the back button to return to the page. 147 $.fn.disableOnSubmit = function() { 148 this.click(function() { 149 var form = $(this).closest("form"); 150 if (form.hasClass("trac-submit-is-disabled")) { 151 form.on("submit.prevent-submit", function() { 152 return false; 153 }); 154 $(window).on("unload", function() { 155 form.off("submit.prevent-submit"); 156 }); 157 } else { 158 form.addClass("trac-submit-is-disabled"); 159 $(window).on("unload", function() { 160 form.removeClass("trac-submit-is-disabled"); 161 }) 162 } 163 }); 164 }; 165 62 166 $.loadStyleSheet = function(href, type) { 63 167 type = type || "text/css"; 64 $(document).ready(function() { 168 $(function() { 169 var link; 170 $("link[rel=stylesheet]").each(function() { 171 if (this.getAttribute("href") === href) { 172 if (this.disabled) 173 this.disabled = false; 174 link = this; 175 return false; 176 } 177 }); 178 if (link !== undefined) 179 return; 65 180 if (document.createStyleSheet) { // MSIE 66 181 document.createStyleSheet(href); … … 70 185 } 71 186 }); 72 } 73 187 }; 188 189 // {script.src: [listener1, listener2, ...]} 190 var readyListeners = {}; 191 192 $.documentReady = function(listener) { 193 var script = document.currentScript; 194 if (script === undefined) { 195 script = $("head script"); 196 script = script[script.length - 1]; 197 } 198 if (script) { 199 var href = script.getAttribute("src"); 200 if (!(href in readyListeners)) 201 readyListeners[href] = []; 202 var listeners = readyListeners[href]; 203 listeners.push(listener); 204 } 205 $(listener); 206 }; 207 208 $.loadScript = function(href, type, charset) { 209 var script; 210 $("head script").each(function() { 211 if (this.getAttribute("src") === href) { 212 script = this; 213 return false; 214 } 215 }); 216 if (script !== undefined) { 217 // Call registered ready listeners 218 $.each(readyListeners[href] || [], function(idx, listener) { 219 listener.call(document, $); 220 }); 221 } else { 222 // Don't use $("<script>").appendTo("head") to avoid adding 223 // "_=<timestamp>" parameter to url. 224 script = document.createElement("script"); 225 script.src = href; 226 script.async = false; 227 script.type = type || "text/javascript"; 228 script.charset = charset || "utf-8"; 229 $("head")[0].appendChild(script); 230 } 231 }; 232 233 var warn_unsaved_changes; 234 235 // Prompt a warning if leaving the page with unsaved changes 236 $.setWarningUnsavedChanges = function(enabled, message) { 237 if (enabled) { 238 if (!warn_unsaved_changes) { 239 $(window).on("beforeunload", function() { 240 return warn_unsaved_changes; 241 }); 242 } 243 warn_unsaved_changes = message || _("You have unsaved changes. Your " + 244 "changes will be lost if you leave this page before saving your " + 245 "changes."); 246 } else { 247 $(window).off("beforeunload"); 248 warn_unsaved_changes = null; 249 } 250 }; 251 74 252 // Escape special HTML characters (&<>") 75 253 var quote = {"&": "&", "<": "<", ">": ">", '"': """}; … … 79 257 return value; 80 258 return value.replace(/[&<>"]/g, function(c) { return quote[c]; }); 81 } 82 259 }; 260 83 261 function format(str, args, escape) { 84 262 var kwargs = args[args.length - 1]; … … 90 268 result = kwargs[k]; 91 269 return escape ? escape(result) : result; 92 }); 270 }); 93 271 } 94 272 … … 97 275 $.format = function(str) { 98 276 return format(str, arguments); 99 } 277 }; 100 278 101 279 $.htmlFormat = function(str) { 102 280 return format(str, arguments, $.htmlEscape); 103 } 281 }; 104 282 105 283 $.template = $.format; // For backward compatibility … … 113 291 } 114 292 115 // The following are defined for backwards compatibility with releases prior116 // to Trac 0.11117 118 window.addEvent = function(elem, type, func) {119 $(elem).bind(type, func);120 }121 window.addHeadingLinks = function(container, title) {122 $.each(["h1", "h2", "h3", "h4", "h5", "h6"], function() {123 $(this, container).addAnchor(title);124 });125 }126 window.enableControl = function(id, enabled) {127 $("#" + id).enable(enabled);128 }129 window.getAncestorByTagName = function(elem, tagName) {130 return $(elem).parents(tagName).get(0);131 }132 133 293 })(jQuery);
Note: See TracChangeset
for help on using the changeset viewer.