1// Clearly stolen from Proud 2 3var Proud = Proud || { 'settings': {}, 'behaviors': {}, 'locale': {} }; 4 5(function ($) { 6 7var cache = {}, uuid = 0; 8 9 /** 10 * Filters elements by whether they have not yet been processed. 11 * 12 * @param id 13 * (Optional) If this is a string, then it will be used as the CSS class 14 * name that is applied to the elements for determining whether it has 15 * already been processed. The elements will get a class in the form of 16 * "id-processed". 17 * 18 * If the id parameter is a function, it will be passed off to the fn 19 * parameter and the id will become a unique identifier, represented as a 20 * number. 21 * 22 * When the id is neither a string or a function, it becomes a unique 23 * identifier, depicted as a number. The element's class will then be 24 * represented in the form of "jquery-once-#-processed". 25 * 26 * Take note that the id must be valid for usage as an element's class name. 27 * @param fn 28 * (Optional) If given, this function will be called for each element that 29 * has not yet been processed. The function's return value follows the same 30 * logic as $.each(). Returning true will continue to the next matched 31 * element in the set, while returning false will entirely break the 32 * iteration. 33 */ 34 $.fn.once = function (id, fn) { 35 if (typeof id != 'string') { 36 // Generate a numeric ID if the id passed can't be used as a CSS class. 37 if (!(id in cache)) { 38 cache[id] = ++uuid; 39 } 40 // When the fn parameter is not passed, we interpret it from the id. 41 if (!fn) { 42 fn = id; 43 } 44 id = 'jquery-once-' + cache[id]; 45 } 46 // Remove elements from the set that have already been processed. 47 var name = id + '-processed'; 48 var elements = this.not('.' + name).addClass(name); 49 50 return $.isFunction(fn) ? elements.each(fn) : elements; 51 }; 52 53 /** 54 * Filters elements that have been processed once already. 55 * 56 * @param id 57 * A required string representing the name of the class which should be used 58 * when filtering the elements. This only filters elements that have already 59 * been processed by the once function. The id should be the same id that 60 * was originally passed to the once() function. 61 * @param fn 62 * (Optional) If given, this function will be called for each element that 63 * has not yet been processed. The function's return value follows the same 64 * logic as $.each(). Returning true will continue to the next matched 65 * element in the set, while returning false will entirely break the 66 * iteration. 67 */ 68 $.fn.removeOnce = function (id, fn) { 69 var name = id + '-processed'; 70 var elements = this.filter('.' + name).removeClass(name); 71 72 return $.isFunction(fn) ? elements.each(fn) : elements; 73 }; 74 75/** 76 * Attach all registered behaviors to a page element. 77 * 78 * Behaviors are event-triggered actions that attach to page elements, enhancing 79 * default non-JavaScript UIs. Behaviors are registered in the Proud.behaviors 80 * object using the method 'attach' and optionally also 'detach' as follows: 81 * @code 82 * Proud.behaviors.behaviorName = { 83 * attach: function (context, settings) { 84 * ... 85 * }, 86 * detach: function (context, settings, trigger) { 87 * ... 88 * } 89 * }; 90 * @endcode 91 * 92 * Proud.attachBehaviors is added below to the jQuery ready event and so 93 * runs on initial page load. Developers implementing AHAH/Ajax in their 94 * solutions should also call this function after new page content has been 95 * loaded, feeding in an element to be processed, in order to attach all 96 * behaviors to the new content. 97 * 98 * Behaviors should use 99 * @code 100 * $(selector).once('behavior-name', function () { 101 * ... 102 * }); 103 * @endcode 104 * to ensure the behavior is attached only once to a given element. (Doing so 105 * enables the reprocessing of given elements, which may be needed on occasion 106 * despite the ability to limit behavior attachment to a particular element.) 107 * 108 * @param context 109 * An element to attach behaviors to. If none is given, the document element 110 * is used. 111 * @param settings
112 * An object containing settings for the current context. If none given, the 113 * global Proud.settings object is used. 114 * @param reRunAjax 115 * Bool for if we're in ajax-load mode or not 116 */ 117Proud.attachBehaviors = function (context, settings, reRunAjax) { 118 context = context || document; 119 settings = settings || Proud.settings; 120 // Execute all of them. 121 $.each(Proud.behaviors, function () { 122 // We're a function AND ( we're on normal load OR behavior is flagged ) 123 if ($.isFunction(this.attach) && (!reRunAjax || this.reRunAjax) ) { 124 this.attach(context, settings); 125 } 126 }); 127}; 128 129/** 130 * Detach registered behaviors from a page element. 131 * 132 * Developers implementing AHAH/Ajax in their solutions should call this 133 * function before page content is about to be removed, feeding in an element 134 * to be processed, in order to allow special behaviors to detach from the 135 * content. 136 * 137 * Such implementations should look for the class name that was added in their 138 * corresponding Proud.behaviors.behaviorName.attach implementation, i.e. 139 * behaviorName-processed, to ensure the behavior is detached only from 140 * previously processed elements. 141 * 142 * @param context 143 * An element to detach behaviors from. If none is given, the document element 144 * is used. 145 * @param settings 146 * An object containing settings for the current context. If none given, the 147 * global Proud.settings object is used. 148 * @param trigger 149 * A string containing what's causing the behaviors to be detached. The 150 * possible triggers are: 151 * - unload: (default) The context element is being removed from the DOM. 152 * - move: The element is about to be moved within the DOM (for example, 153 * during a tabledrag row swap). After the move is completed, 154 * Proud.attachBehaviors() is called, so that the behavior can undo 155 * whatever it did in response to the move. Many behaviors won't need to 156 * do anything simply in response to the element being moved, but because 157 * IFRAME elements reload their "src" when being moved within the DOM, 158 * behaviors bound to IFRAME elements (like WYSIWYG editors) may need to 159 * take some action. 160 * - serialize: When an Ajax form is submitted, this is called with the 161 * form as the context. This provides every behavior within the form an 162 * opportunity to ensure that the field elements have correct content 163 * in them before the form is serialized. The canonical use-case is so 164 * that WYSIWYG editors can update the hidden textarea to which they are 165 * bound. 166 * 167 * @see Proud.attachBehaviors 168 */ 169Proud.detachBehaviors = function (context, settings, trigger) { 170 context = context || document; 171 settings = settings || Proud.settings; 172 trigger = trigger || 'unload'; 173 // Execute all of them. 174 $.each(Proud.behaviors, function () { 175 if ($.isFunction(this.detach)) { 176 this.detach(context, settings, trigger); 177 } 178 }); 179}; 180 181//Attach all behaviors. 182$(function () { 183 Proud.attachBehaviors(document, Proud.settings); 184 // Run on ajax for PageBuilder 185 $(document).ajaxComplete(function () { 186 Proud.attachBehaviors(document, Proud.settings, true); 187 }); 188}); 189 190})(jQuery);
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.