1function dittyLoadGoogleFont(font, weight = 400) { 2 const fontId = font.replace(/\s+/g, "-").toLowerCase(); 3 4 let link = document.getElementById(`ditty-google-font--${fontId}`); 5 let weights = []; 6 let updateFont = false; 7 8 if (link) { 9 let dataWeights = link.dataset.weights; 10 if (dataWeights) { 11 weights = dataWeights.split(","); 12 } 13 } 14 15 if (!weights.includes(weight)) { 16 weights.push(weight); 17 updateFont = true; 18 } 19 20 dataWeights = weights.join(";"); 21 dataWeightsString = weights.join(";0,"); 22 23 if (updateFont) { 24 if (link) { 25 link.remove(); 26 } 27 link = jQuery( 28 `<link id="ditty-google-font--${fontId}" data-weights="${dataWeights}" href="https://fonts.googleapis.com/css?family=${font}:wght@0,${dataWeightsString}" rel="stylesheet">` 29 ); 30 31 jQuery("head").append(link); 32 } 33} 34 35/** 36 * Update item layout css 37 * 38 * @since 3.0 39 * @return null 40 */ 41function dittyLayoutCss(layoutCss, layoutId, updateCSS) { 42 var $styles = jQuery("style#ditty-layout--" + layoutId); 43 if (undefined === $styles[0]) { 44 $styles = jQuery('<style id="ditty-layout--' + layoutId + '"></style>'); 45 jQuery("head").append($styles); 46 updateCSS = "update"; 47 } 48 if ("update" === updateCSS) { 49 layoutCss = layoutCss.replace(">", ">"); 50 $styles.html(layoutCss); 51 } 52} 53 54/** 55 * Update item display css 56 * 57 * @since 3.0 58 * @return null 59 */ 60function dittyDisplayCss(displayCss, displayId) { 61 var $styles = jQuery("style#ditty-display--" + displayId); 62 if (undefined === $styles[0]) { 63 $styles = jQuery('<style id="ditty-display--' + displayId + '"></style>'); 64 jQuery("head").append($styles); 65 } 66 displayCss = displayCss.replace(">", ">"); 67 $styles.html(displayCss); 68} 69 70/** 71 * Update item display css 72 * 73 * @since 3.1.24 74 * @return null 75 */ 76function dittyTypographyCss(settings) { 77 let css = ""; 78 if (settings.fontFamily && "" !== settings.fontFamily) { 79 css += `font-family:${settings.fontFamily}`; 80 if (settings.fontTypeface && "" !== settings.fontTypeface) { 81 css += `, ${settings.fontTypeface}`; 82 } 83 css += `;`; 84 } 85 if (settings.fontWeight && "" !== settings.fontWeight) { 86 css += `font-weight:${settings.fontWeight};`; 87 } 88 if (settings.fontSize && "" !== settings.fontSize) { 89 css += `font-size:${settings.fontSize};`; 90 } 91 if (settings.lineHeight && "" !== settings.lineHeight) { 92 css += `line-height:${settings.lineHeight};`; 93 } 94 if (settings.letterSpacing && "" !== settings.letterSpacing) { 95 css += `letter-spacing:${settings.letterSpacing};`; 96 } 97 return css; 98} 99 100/** 101 * Update item display css 102 * 103 * @since 3.1.24 104 * @return null 105 */ 106function dittyRenderDisplayCss(settings, displayId) { 107 const cssPrefix = `.ditty[data-display="${settings.display}"]`; 108 109 let css = ""; 110 111 // Title CSS 112 const titleFont = settings.titleTypography ? settings.titleTypography : {}; 113 let titleFontCss = dittyTypographyCss(titleFont); 114 titleFontCss += `margin:0;padding:0;`; 115 if ("" !== settings.titleColor) { 116 titleFontCss += `color:${settings.titleColor};`; 117 } 118 if ("" !== titleFontCss) { 119 css += `${cssPrefix} .ditty__title__element{${titleFontCss};}`; 120 } 121 if ("" !== settings.titleColor) { 122 css += `${cssPrefix} .ditty__title__element *{color:${settings.titleColor};}`; 123 } 124 if ("" !== settings.itemLinkColor) { 125 css += `${cssPrefix} .ditty__title__element a{color:${settings.titleLinkColor};}`; 126 } 127 if ( 128 titleFont.fontType && 129 "google" === titleFont.fontType && 130 dittyVars.googleFonts 131 ) { 132 dittyLoadGoogleFont(titleFont.fontFamily, titleFont.fontWeight); 133 } 134 135 // Item CSS 136 const itemFont = settings.itemTypography ? settings.itemTypography : {}; 137 let itemFontCss = dittyTypographyCss(itemFont); 138 if ("" !== settings.itemTextColor) { 139 itemFontCss += `color:${settings.itemTextColor};`; 140 } 141 if ("" !== itemFontCss) { 142 css += `${cssPrefix} .ditty-item__elements{${itemFontCss};}`; 143 } 144 if ("" !== settings.itemLinkColor) { 145 css += `${cssPrefix} .ditty-item__elements a{color:${settings.itemLinkColor};}`; 146 } 147 if ( 148 itemFont.fontType && 149 "google" === itemFont.fontType && 150 dittyVars.googleFonts 151 ) { 152 dittyLoadGoogleFont(itemFont.fontFamily, itemFont.fontWeight); 153 } 154 dittyDisplayCss(css, displayId); 155} 156 157/** 158 * Configure the order of items 159 * 160 * @since 3.1.18 161 * @return null 162 */ 163function dittyOrderItemGroup(items, settings) { 164 const orderby = settings.orderby ? settings.orderby : "default"; 165 const order = settings.order ? settings.order : "desc"; 166 167 let sortedItems = items; 168 switch (orderby) { 169 case "timestamp": 170 sortedItems = items.sort( 171 (a, b) => new Date(b.timestamp_iso) - new Date(a.timestamp_iso) 172 ); 173 break; 174 case "random": 175 for (let i = sortedItems.length - 1; i > 0; i--) { 176 const j = Math.floor(Math.random() * (i + 1)); 177 const temp = sortedItems[i]; 178 sortedItems[i] = sortedItems[j]; 179 sortedItems[j] = temp; 180 } 181 break; 182 case "alphabetical": 183 sortedItems = [...items].sort((a, b) => { 184 const getSortText = (item) => { 185 const raw = 186 item.sort_text !== undefined 187 ? String(item.sort_text || "") 188 : (item.html || "").replace(/<[^>]*>/g, " "); 189 return raw.replace(/\s+/g, " ").trim().toLowerCase(); 190 }; 191 return getSortText(a).localeCompare(getSortText(b)); 192 }); 193 break; 194 default: 195 break; 196 } 197 198 if ("asc" === order) { 199 sortedItems.reverse(); 200 } 201 202 return sortedItems; 203} 204function dittyOrderItems(items, settings) { 205 const parentItems = []; 206 const childGroups = []; 207 items.map((item) => { 208 if (!item.parent_id || "0" === String(item.parent_id)) { 209 parentItems.push(item); 210 } else { 211 if (!childGroups[item.parent_id]) { 212 childGroups[item.parent_id] = []; 213 } 214 childGroups[item.parent_id].push(item); 215 } 216 }); 217 218 const sortedParentItems = dittyOrderItemGroup(parentItems, settings); 219 const sortedChildGroups = childGroups.map((group) => { 220 return dittyOrderItemGroup(group, settings); 221 }); 222 223 const sortedItems = sortedParentItems.reduce((itemsList, item) => { 224 itemsList.push(item); 225 if (sortedChildGroups[item.id]) { 226 itemsList = [...itemsList, ...sortedChildGroups[item.id]]; 227 } 228 return itemsList; 229 }, []); 230 231 return sortedItems; 232} 233 234/** 235 * Update items 236 * 237 * @since 3.1.19 238 * @return null 239 */ 240function dittyGetUpdatedItemData(prevItems, newItems) { 241 const updatedIndexes = []; 242 const updatedItems = newItems.map((item, index) => { 243 if (typeof prevItems[index] === "undefined") { 244 updatedIndexes.push(index); 245 } else if (String(prevItems[index].uniq_id) !== String(item.uniq_id)) { 246 updatedIndexes.push(index); 247 } else if (String(prevItems[index].html) !== String(item.html)) { 248 updatedIndexes.push(index); 249 } else if (String(prevItems[index].css) !== String(item.css)) { 250 updatedIndexes.push(index); 251 } 252 return item; 253 }); 254 255 return { 256 updatedItems: updatedItems, 257 updatedIndexes: updatedIndexes, 258 }; 259} 260// function dittyGetUpdatedItemData(prevItems, newItems) { 261// const newGroupedItems = newItems.reduce((items, item) => { 262// const index = items.findIndex((i) => { 263// return i.id === item.id; 264// }); 265// item.updated = "updated"; 266// if (index < 0) { 267// items.push({ 268// id: item.id, 269// items: [item], 270// }); 271// } else { 272// items[index].items.push(item); 273// } 274// return items; 275// }, []); 276 277// const flattenedItems = newGroupedItems.reduce((items, group) => { 278// return [...items, ...group.items]; 279// }, []); 280// const updatedIndexes = []; 281// const updatedItems = flattenedItems.map((item, index) => { 282// if (item.updated) { 283// updatedIndexes.push(index); 284// delete item.updated; 285// } else if (typeof prevItems[index] === "undefined") { 286// updatedIndexes.push(index); 287// } else if (String(prevItems[index].uniq_id) !== String(item.uniq_id)) { 288// updatedIndexes.push(index); 289// } 290// return item; 291// }); 292 293// return { 294// updatedItems: updatedItems, 295// updatedIndexes: updatedIndexes, 296// }; 297// } 298 299/** 300 * Update items 301 * 302 * @since 3.0.10 303 * @return null 304 */ 305function dittyUpdateItems(itemSwaps, swapType = "animate") { 306 var animationSpeed = 500; 307 308 jQuery.each(itemSwaps, function (index, data) { 309 var $current = data.currentItem, 310 $new = data.newItem; 311 312 var $updateWrapper = $current.parent(); 313 314 if ("static" === swapType) {
315 $current.after($new); 316 $current.remove(); 317 } else { 318 var newStyle = $new.attr("style"); 319 $current.wrap( 320 '<div class="ditty-update-wrapper" style="position: relative;overflow: hidden;"></div>' 321 ); 322 $updateWrapper.stop().css({ 323 height: $current.outerHeight(), 324 }); 325 $current.stop().css({ 326 position: "absolute", 327 top: 0, 328 left: 0, 329 width: "100%", 330 }); 331 $new.stop().css({ 332 position: "absolute", 333 top: 0, 334 left: 0, 335 width: "100%", 336 opacity: 0, 337 }); 338 $current.after($new); 339 340 $current.stop().animate( 341 { 342 opacity: 0, 343 }, 344 animationSpeed * 0.75, 345 "linear" 346 ); 347 348 $new.stop().animate( 349 { 350 opacity: 1, 351 }, 352 animationSpeed * 0.75, 353 "linear" 354 ); 355 356 $updateWrapper.stop().animate( 357 { 358 height: $new.outerHeight(), 359 }, 360 animationSpeed, 361 "easeOutQuint", 362 function () { 363 $updateWrapper.removeAttr("style"); 364 $current.unwrap(); 365 $current.remove(); 366 if (newStyle) { 367 $new.attr("style", newStyle); 368 } else { 369 $new.removeAttr("style"); 370 } 371 if ($new.hasClass("ditty-temp-item")) { 372 $new.remove(); 373 } 374 } 375 ); 376 } 377 }); 378}
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.