fl-builder-preview.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. (function($){
  2. /**
  3. * Helper class for dealing with live previews.
  4. *
  5. * @class FLBuilderPreview
  6. * @since 1.3.3
  7. * @param {Object} config
  8. */
  9. FLBuilderPreview = function( config )
  10. {
  11. // Set the type.
  12. this.type = config.type;
  13. // Save the current state.
  14. this._saveState();
  15. // Initialize the preview.
  16. if ( config.layout ) {
  17. FLBuilder._renderLayout( config.layout, function() {
  18. this._init();
  19. if ( config.callback ) {
  20. config.callback();
  21. }
  22. }.bind( this ) );
  23. } else {
  24. this._init();
  25. }
  26. };
  27. /**
  28. * Stores all the fonts and weights of all font fields.
  29. * This is used to render the stylesheet with Google Fonts.
  30. *
  31. * @since 1.6.3
  32. * @access private
  33. * @property {Array} _fontsList
  34. */
  35. FLBuilderPreview._fontsList = {};
  36. /**
  37. * Returns a formatted selector string for a preview.
  38. *
  39. * @since 2.1
  40. * @method getFormattedSelector
  41. * @param {String} selector A CSS selector string.
  42. * @return {String}
  43. */
  44. FLBuilderPreview.getFormattedSelector = function( prefix, selector )
  45. {
  46. var formatted = '',
  47. parts = selector.split( ',' ),
  48. i = 0;
  49. for ( ; i < parts.length; i++ ) {
  50. if ( parts[ i ].indexOf( '{node}' ) > -1 ) {
  51. formatted = parts[ i ].replace( '{node}', prefix );
  52. }
  53. else {
  54. formatted += prefix + ' ' + parts[ i ];
  55. }
  56. if ( i != parts.length - 1 ) {
  57. formatted += ', ';
  58. }
  59. }
  60. return formatted;
  61. };
  62. /**
  63. * Prototype for new instances.
  64. *
  65. * @since 1.3.3
  66. * @property {Object} prototype
  67. */
  68. FLBuilderPreview.prototype = {
  69. /**
  70. * The type of node that we are previewing.
  71. *
  72. * @since 1.3.3
  73. * @property {String} type
  74. */
  75. type : '',
  76. /**
  77. * The ID of node that we are previewing.
  78. *
  79. * @since 1.3.3
  80. * @property {String} nodeId
  81. */
  82. nodeId : null,
  83. /**
  84. * An object with data for each CSS class
  85. * in the preview.
  86. *
  87. * @since 1.3.3
  88. * @property {Object} classes
  89. */
  90. classes : {},
  91. /**
  92. * An object with references to each element
  93. * in the preview.
  94. *
  95. * @since 1.3.3
  96. * @property {Object} elements
  97. */
  98. elements : {},
  99. /**
  100. * An object that contains data for the current
  101. * state of a layout before changes are made.
  102. *
  103. * @since 1.3.3
  104. * @property {Object} state
  105. */
  106. state : null,
  107. /**
  108. * Node settings saved when the preview was initalized.
  109. *
  110. * @since 1.7
  111. * @access private
  112. * @property {Object} _savedSettings
  113. */
  114. _savedSettings : null,
  115. /**
  116. * An instance of FLStyleSheet for the current preview.
  117. *
  118. * @since 1.3.3
  119. * @access private
  120. * @property {FLStyleSheet} _styleSheet
  121. */
  122. _styleSheet : null,
  123. /**
  124. * An instance of FLStyleSheet for the medium device preview.
  125. *
  126. * @since 1.9
  127. * @access private
  128. * @property {FLStyleSheet} _styleSheetMedium
  129. */
  130. _styleSheetMedium : null,
  131. /**
  132. * An instance of FLStyleSheet for the responsive device preview.
  133. *
  134. * @since 1.9
  135. * @access private
  136. * @property {FLStyleSheet} _styleSheet
  137. */
  138. _styleSheetResponsive : null,
  139. /**
  140. * A timeout object for delaying the current preview refresh.
  141. *
  142. * @since 1.3.3
  143. * @access private
  144. * @property {Object} _timeout
  145. */
  146. _timeout : null,
  147. /**
  148. * A timeout object for delaying when we show the loading
  149. * graphic for refresh previews.
  150. *
  151. * @since 1.10
  152. * @access private
  153. * @property {Object} _loaderTimeout
  154. */
  155. _loaderTimeout : null,
  156. /**
  157. * Stores the last classname for a classname preview.
  158. *
  159. * @since 1.3.3
  160. * @access private
  161. * @property {String} _lastClassName
  162. */
  163. _lastClassName : null,
  164. /**
  165. * A reference to the AJAX object for a preview refresh.
  166. *
  167. * @since 1.3.3
  168. * @access private
  169. * @property {Object} _xhr
  170. */
  171. _xhr : null,
  172. /**
  173. * Initializes a builder preview.
  174. *
  175. * @since 1.3.3
  176. * @access private
  177. * @method _init
  178. */
  179. _init: function()
  180. {
  181. // Node Id
  182. this.nodeId = $('.fl-builder-settings').data('node');
  183. // Save settings
  184. this._saveSettings();
  185. // Elements and Class Names
  186. this._initElementsAndClasses();
  187. // Create the preview stylesheets
  188. this._createSheets();
  189. // Responsive previews
  190. this._initResponsivePreviews();
  191. // Default field previews
  192. this._initDefaultFieldPreviews();
  193. // Init
  194. switch(this.type) {
  195. case 'row':
  196. this._initRow();
  197. break;
  198. case 'col':
  199. this._initColumn();
  200. break;
  201. case 'module':
  202. this._initModule();
  203. break;
  204. }
  205. },
  206. /**
  207. * Saves the current settings to be checked to see if
  208. * anything has changed when a preview is canceled.
  209. *
  210. * @since 1.7
  211. * @access private
  212. * @method _saveSettings
  213. */
  214. _saveSettings: function()
  215. {
  216. var form = $('.fl-builder-settings-lightbox .fl-builder-settings');
  217. this._savedSettings = FLBuilder._getSettingsForChangedCheck( this.nodeId, form );
  218. },
  219. /**
  220. * Checks to see if the settings have changed.
  221. *
  222. * @since 1.7
  223. * @access private
  224. * @method _settingsHaveChanged
  225. * @return bool
  226. */
  227. _settingsHaveChanged: function()
  228. {
  229. var form = $('.fl-builder-settings-lightbox .fl-builder-settings'),
  230. settings = FLBuilder._getSettings( form );
  231. return JSON.stringify( this._savedSettings ) != JSON.stringify( settings );
  232. },
  233. /**
  234. * Initializes the classname and element references
  235. * for this preview.
  236. *
  237. * @since 1.3.3
  238. * @access private
  239. * @method _initElementsAndClasses
  240. */
  241. _initElementsAndClasses: function()
  242. {
  243. var contentClass;
  244. // Content Class
  245. if(this.type == 'row') {
  246. contentClass = '.fl-row-content-wrap';
  247. }
  248. else {
  249. contentClass = '.fl-' + this.type + '-content';
  250. }
  251. // Class Names
  252. $.extend(this.classes, {
  253. settings : '.fl-builder-' + this.type + '-settings',
  254. settingsHeader : '.fl-builder-' + this.type + '-settings .fl-lightbox-header',
  255. node : FLBuilder._contentClass + ' .fl-node-' + this.nodeId,
  256. content : FLBuilder._contentClass + ' .fl-node-' + this.nodeId + ' > ' + contentClass
  257. });
  258. // Elements
  259. $.extend(this.elements, {
  260. settings : $(this.classes.settings),
  261. settingsHeader : $(this.classes.settingsHeader),
  262. node : $(this.classes.node),
  263. content : $(this.classes.content)
  264. });
  265. },
  266. /**
  267. * Creates the stylesheets for default, medium
  268. * and responsive previews.
  269. *
  270. * @since 1.9
  271. * @method _createSheets
  272. */
  273. _createSheets: function()
  274. {
  275. this._destroySheets();
  276. if ( ! this._styleSheet ) {
  277. this._styleSheet = new FLStyleSheet( {
  278. id : 'fl-builder-preview',
  279. className : 'fl-builder-preview-style'
  280. } );
  281. }
  282. if ( ! this._styleSheetMedium ) {
  283. this._styleSheetMedium = new FLStyleSheet( {
  284. id : 'fl-builder-preview-medium',
  285. className : 'fl-builder-preview-style'
  286. } );
  287. }
  288. if ( ! this._styleSheetResponsive ) {
  289. this._styleSheetResponsive = new FLStyleSheet( {
  290. id : 'fl-builder-preview-responsive',
  291. className : 'fl-builder-preview-style'
  292. } );
  293. }
  294. },
  295. /**
  296. * Destroys all preview sheets.
  297. *
  298. * @since 1.9
  299. * @method _destroySheets
  300. */
  301. _destroySheets: function()
  302. {
  303. if ( this._styleSheet ) {
  304. this._styleSheet.destroy();
  305. this._styleSheet = null;
  306. }
  307. if ( this._styleSheetMedium ) {
  308. this._styleSheetMedium.destroy();
  309. this._styleSheetMedium = null;
  310. }
  311. if ( this._styleSheetResponsive ) {
  312. this._styleSheetResponsive.destroy();
  313. this._styleSheetResponsive = null;
  314. }
  315. },
  316. /**
  317. * Updates a CSS rule for this preview.
  318. *
  319. * @since 1.3.3
  320. * @method updateCSSRule
  321. * @param {String} selector The CSS selector to update.
  322. * @param {String} property The CSS property to update.
  323. * @param {String} value The CSS value to update.
  324. */
  325. updateCSSRule: function( selector, property, value )
  326. {
  327. this._styleSheet.updateRule( selector, property, value );
  328. },
  329. /**
  330. * Runs a delay with a callback.
  331. *
  332. * @since 1.3.3
  333. * @method delay
  334. * @param {Number} length How long to wait before running the callback.
  335. * @param {Function} callback A function to call when the delay is complete.
  336. */
  337. delay: function(length, callback)
  338. {
  339. this._cancelDelay();
  340. this._timeout = setTimeout(callback, length);
  341. },
  342. /**
  343. * Cancels a preview refresh delay.
  344. *
  345. * @since 1.3.3
  346. * @access private
  347. * @method _cancelDelay
  348. */
  349. _cancelDelay: function()
  350. {
  351. if(this._timeout !== null) {
  352. clearTimeout(this._timeout);
  353. }
  354. },
  355. /**
  356. * Converts a hex value to an array of RGB values.
  357. *
  358. * @since 1.3.3
  359. * @method hexToRgb
  360. * @param {String} hex
  361. * @return {Array}
  362. */
  363. hexToRgb: function(hex)
  364. {
  365. var bigInt = parseInt(hex, 16),
  366. r = (bigInt >> 16) & 255,
  367. g = (bigInt >> 8) & 255,
  368. b = bigInt & 255;
  369. return [r, g, b];
  370. },
  371. /**
  372. * Parses a float or returns 0 if we don't have a number.
  373. *
  374. * @since 1.3.3
  375. * @method parseFloat
  376. * @param {Number} value
  377. * @return {Number}
  378. */
  379. parseFloat: function(value)
  380. {
  381. return isNaN(parseFloat(value)) ? 0 : parseFloat(value);
  382. },
  383. /* Responsive Previews
  384. ----------------------------------------------------------*/
  385. /**
  386. * Initializes logic for responsive previews.
  387. *
  388. * @since 1.9
  389. * @method _initResponsivePreviews
  390. */
  391. _initResponsivePreviews: function()
  392. {
  393. FLBuilder.addHook( 'responsive-editing-switched.preview', $.proxy( this._responsiveEditingSwitched, this ) );
  394. },
  395. /**
  396. * Destroys responsive preview events.
  397. *
  398. * @since 1.9
  399. * @method _destroyResponsivePreviews
  400. */
  401. _destroyResponsivePreviews: function()
  402. {
  403. FLBuilder.removeHook( 'responsive-editing-switched.preview' );
  404. },
  405. /**
  406. * Initializes logic for responsive previews.
  407. *
  408. * @since 1.9
  409. * @method _responsiveEditingSwitched
  410. */
  411. _responsiveEditingSwitched: function( e, mode )
  412. {
  413. if ( 'default' == mode ) {
  414. this._styleSheetMedium.disable();
  415. this._styleSheetResponsive.disable();
  416. }
  417. else if ( 'medium' == mode ) {
  418. this._styleSheetMedium.enable();
  419. this._styleSheetResponsive.disable();
  420. }
  421. else if ( 'responsive' == mode ) {
  422. this._styleSheetMedium.disable();
  423. this._styleSheetResponsive.enable();
  424. }
  425. },
  426. /**
  427. * Updates a CSS rule for responsive preview.
  428. *
  429. * @since 1.9
  430. * @method updateResponsiveCSSRule
  431. * @param {String} selector The CSS selector to update.
  432. * @param {String} property The CSS property to update.
  433. * @param {String} value The CSS value to update.
  434. */
  435. updateResponsiveCSSRule: function( selector, property, value )
  436. {
  437. var mode = FLBuilderResponsiveEditing._mode,
  438. sheetKey = 'default' == mode ? '' : mode.charAt(0).toUpperCase() + mode.slice(1);
  439. this[ '_styleSheet' + sheetKey ].updateRule( selector, property, value );
  440. },
  441. /* States
  442. ----------------------------------------------------------*/
  443. /**
  444. * Saves the current state of a layout.
  445. *
  446. * @since 1.3.3
  447. * @access private
  448. * @method _saveState
  449. */
  450. _saveState: function()
  451. {
  452. var post = FLBuilderConfig.postId,
  453. css = $('link[href*="/cache/' + post + '"]').attr('href'),
  454. js = $('script[src*="/cache/' + post + '"]').attr('src'),
  455. html = $(FLBuilder._contentClass).html();
  456. this.state = {
  457. css : css,
  458. js : js,
  459. html : html
  460. };
  461. },
  462. /**
  463. * Runs a preview refresh for the current settings lightbox.
  464. *
  465. * @since 1.3.3
  466. * @method preview
  467. */
  468. preview: function()
  469. {
  470. var form = $('.fl-builder-settings-lightbox .fl-builder-settings'),
  471. nodeId = form.attr('data-node'),
  472. settings = FLBuilder._getSettings(form);
  473. // Show the node as loading.
  474. FLBuilder._showNodeLoading( nodeId );
  475. // Abort an existing preview request.
  476. this._cancelPreview();
  477. // Make a new preview request.
  478. this._xhr = FLBuilder.ajax({
  479. action : 'render_layout',
  480. node_id : nodeId,
  481. node_preview : settings
  482. }, $.proxy(this._renderPreview, this));
  483. },
  484. /**
  485. * Runs a preview refresh with a delay.
  486. *
  487. * @since 1.3.3
  488. * @method delayPreview
  489. */
  490. delayPreview: function(e)
  491. {
  492. var heading = typeof e == 'undefined' ? [] : $(e.target).closest('tr').find('th'),
  493. widgetHeading = $('.fl-builder-widget-settings .fl-builder-settings-title'),
  494. lightboxHeading = $('.fl-builder-settings .fl-lightbox-header'),
  495. loaderSrc = FLBuilderLayoutConfig.paths.pluginUrl + 'img/ajax-loader-small.svg',
  496. loader = $('<img class="fl-builder-preview-loader" src="' + loaderSrc + '" />');
  497. this.delay(1000, $.proxy(this.preview, this));
  498. this._loaderTimeout = setTimeout( function() {
  499. $('.fl-builder-preview-loader').remove();
  500. if(heading.length > 0) {
  501. heading.append(loader);
  502. }
  503. else if(widgetHeading.length > 0) {
  504. widgetHeading.append(loader);
  505. }
  506. else if(lightboxHeading.length > 0) {
  507. lightboxHeading.append(loader);
  508. }
  509. }, 1500 );
  510. },
  511. /**
  512. * Cancels a preview refresh.
  513. *
  514. * @since 1.3.3
  515. * @access private
  516. * @method _cancelPreview
  517. */
  518. _cancelPreview: function()
  519. {
  520. if(this._xhr) {
  521. this._xhr.abort();
  522. this._xhr = null;
  523. }
  524. },
  525. /**
  526. * Renders the response of a preview refresh.
  527. *
  528. * @since 1.3.3
  529. * @access private
  530. * @method _renderPreview
  531. * @param {String} response The JSON encoded response.
  532. */
  533. _renderPreview: function(response)
  534. {
  535. this._xhr = null;
  536. FLBuilder._renderLayout(response, $.proxy(this._renderPreviewComplete, this));
  537. },
  538. /**
  539. * Fires when a preview refresh has finished rendering.
  540. *
  541. * @since 1.3.3
  542. * @access private
  543. * @method _renderPreviewComplete
  544. */
  545. _renderPreviewComplete: function()
  546. {
  547. // Refresh the preview styles.
  548. this._createSheets();
  549. // Refresh the elements.
  550. this._initElementsAndClasses();
  551. // Clear the loader timeout.
  552. if(this._loaderTimeout !== null) {
  553. clearTimeout(this._loaderTimeout);
  554. }
  555. // Remove the loading graphic.
  556. $('.fl-builder-preview-loader').remove();
  557. // Fire the preview rendered event.
  558. $( FLBuilder._contentClass ).trigger( 'fl-builder.preview-rendered' );
  559. },
  560. /**
  561. * Reverts a preview to the state that was saved
  562. * before the preview was initialized.
  563. *
  564. * @since 1.3.3
  565. * @method revert
  566. */
  567. revert: function()
  568. {
  569. if ( ! this._settingsHaveChanged() ) {
  570. this.clear();
  571. return;
  572. }
  573. FLBuilder._updateNode( this.nodeId, function() {
  574. this.clear();
  575. }.bind( this ) );
  576. },
  577. /**
  578. * Cancels a preview refresh.
  579. *
  580. * @since 1.3.3
  581. * @method clear
  582. */
  583. cancel: function()
  584. {
  585. this._cancelDelay();
  586. this._cancelPreview();
  587. },
  588. /**
  589. * Cancels a preview refresh and removes
  590. * any stylesheet changes.
  591. *
  592. * @since 1.3.3
  593. * @method clear
  594. */
  595. clear: function()
  596. {
  597. // Canel any preview delays or requests.
  598. this.cancel();
  599. // Destroy the preview stylesheet.
  600. this._destroySheets();
  601. // Destroy responsive editing previews.
  602. this._destroyResponsivePreviews();
  603. },
  604. /* Node Text Color Settings
  605. ----------------------------------------------------------*/
  606. /**
  607. * Initializes node text color previews.
  608. *
  609. * @since 1.3.3
  610. * @access private
  611. * @method _initNodeTextColor
  612. */
  613. _initNodeTextColor: function()
  614. {
  615. // Elements
  616. $.extend(this.elements, {
  617. textColor : $(this.classes.settings + ' input[name=text_color]'),
  618. linkColor : $(this.classes.settings + ' input[name=link_color]'),
  619. hoverColor : $(this.classes.settings + ' input[name=hover_color]'),
  620. headingColor : $(this.classes.settings + ' input[name=heading_color]')
  621. });
  622. // Events
  623. this.elements.textColor.on('change', $.proxy(this._textColorChange, this));
  624. this.elements.linkColor.on('change', $.proxy(this._textColorChange, this));
  625. this.elements.hoverColor.on('change', $.proxy(this._textColorChange, this));
  626. this.elements.headingColor.on('change', $.proxy(this._textColorChange, this));
  627. },
  628. /**
  629. * Fires when the text color field for a node
  630. * is changed.
  631. *
  632. * @since 1.3.3
  633. * @access private
  634. * @method _textColorChange
  635. * @param {Object} e An event object.
  636. */
  637. _textColorChange: function(e)
  638. {
  639. var textColor = this.elements.textColor.val(),
  640. linkColor = this.elements.linkColor.val(),
  641. hoverColor = this.elements.hoverColor.val(),
  642. headingColor = this.elements.headingColor.val();
  643. linkColor = linkColor === '' ? textColor : linkColor;
  644. hoverColor = hoverColor === '' ? textColor : hoverColor;
  645. headingColor = headingColor === '' ? textColor : headingColor;
  646. this.delay(100, $.proxy(function(){
  647. // Update Text color.
  648. if(textColor === '') {
  649. this.updateCSSRule(this.classes.node, 'color', 'inherit');
  650. }
  651. else {
  652. this.updateCSSRule(this.classes.node, 'color', '#' + textColor);
  653. }
  654. // Update Link Color
  655. if ( linkColor === '' ) {
  656. this.updateCSSRule(this.classes.node + ' a', 'color', 'inherit');
  657. }
  658. else {
  659. this.updateCSSRule(this.classes.node + ' a', 'color', '#' + linkColor);
  660. }
  661. // Hover Color
  662. if(hoverColor === '') {
  663. this.updateCSSRule(this.classes.node + ' a:hover', 'color', 'inherit');
  664. }
  665. else {
  666. this.updateCSSRule(this.classes.node + ' a:hover', 'color', '#' + hoverColor);
  667. }
  668. // Heading Color
  669. if(headingColor === '') {
  670. this.updateCSSRule(this.classes.node + ' h1', 'color', 'inherit');
  671. this.updateCSSRule(this.classes.node + ' h2', 'color', 'inherit');
  672. this.updateCSSRule(this.classes.node + ' h3', 'color', 'inherit');
  673. this.updateCSSRule(this.classes.node + ' h4', 'color', 'inherit');
  674. this.updateCSSRule(this.classes.node + ' h5', 'color', 'inherit');
  675. this.updateCSSRule(this.classes.node + ' h6', 'color', 'inherit');
  676. this.updateCSSRule(this.classes.node + ' h1 a', 'color', 'inherit');
  677. this.updateCSSRule(this.classes.node + ' h2 a', 'color', 'inherit');
  678. this.updateCSSRule(this.classes.node + ' h3 a', 'color', 'inherit');
  679. this.updateCSSRule(this.classes.node + ' h4 a', 'color', 'inherit');
  680. this.updateCSSRule(this.classes.node + ' h5 a', 'color', 'inherit');
  681. this.updateCSSRule(this.classes.node + ' h6 a', 'color', 'inherit');
  682. }
  683. else {
  684. this.updateCSSRule(this.classes.node + ' h1', 'color', '#' + headingColor);
  685. this.updateCSSRule(this.classes.node + ' h2', 'color', '#' + headingColor);
  686. this.updateCSSRule(this.classes.node + ' h3', 'color', '#' + headingColor);
  687. this.updateCSSRule(this.classes.node + ' h4', 'color', '#' + headingColor);
  688. this.updateCSSRule(this.classes.node + ' h5', 'color', '#' + headingColor);
  689. this.updateCSSRule(this.classes.node + ' h6', 'color', '#' + headingColor);
  690. this.updateCSSRule(this.classes.node + ' h1 a', 'color', '#' + headingColor);
  691. this.updateCSSRule(this.classes.node + ' h2 a', 'color', '#' + headingColor);
  692. this.updateCSSRule(this.classes.node + ' h3 a', 'color', '#' + headingColor);
  693. this.updateCSSRule(this.classes.node + ' h4 a', 'color', '#' + headingColor);
  694. this.updateCSSRule(this.classes.node + ' h5 a', 'color', '#' + headingColor);
  695. this.updateCSSRule(this.classes.node + ' h6 a', 'color', '#' + headingColor);
  696. }
  697. }, this));
  698. },
  699. /* Node Bg Settings
  700. ----------------------------------------------------------*/
  701. /**
  702. * Initializes node background previews.
  703. *
  704. * @since 1.3.3
  705. * @access private
  706. * @method _initNodeBg
  707. */
  708. _initNodeBg: function()
  709. {
  710. // Elements
  711. $.extend(this.elements, {
  712. bgType : $(this.classes.settings + ' select[name=bg_type]'),
  713. bgColor : $(this.classes.settings + ' input[name=bg_color]'),
  714. bgColorPicker : $(this.classes.settings + ' .fl-picker-bg_color'),
  715. bgOpacity : $(this.classes.settings + ' input[name=bg_opacity]'),
  716. bgImageSrc : $(this.classes.settings + ' select[name=bg_image_src]'),
  717. bgRepeat : $(this.classes.settings + ' select[name=bg_repeat]'),
  718. bgPosition : $(this.classes.settings + ' select[name=bg_position]'),
  719. bgAttachment : $(this.classes.settings + ' select[name=bg_attachment]'),
  720. bgSize : $(this.classes.settings + ' select[name=bg_size]'),
  721. bgVideoSource : $(this.classes.settings + ' select[name=bg_video_source]'),
  722. bgVideo : $(this.classes.settings + ' input[name=bg_video]'),
  723. bgVideoServiceUrl : $(this.classes.settings + ' input[name=bg_video_service_url]'),
  724. bgVideoFallbackSrc : $(this.classes.settings + ' select[name=bg_video_fallback_src]'),
  725. bgSlideshowSource : $(this.classes.settings + ' select[name=ss_source]'),
  726. bgSlideshowPhotos : $(this.classes.settings + ' input[name=ss_photos]'),
  727. bgSlideshowFeedUrl : $(this.classes.settings + ' input[name=ss_feed_url]'),
  728. bgSlideshowSpeed : $(this.classes.settings + ' input[name=ss_speed]'),
  729. bgSlideshowTrans : $(this.classes.settings + ' select[name=ss_transition]'),
  730. bgSlideshowTransSpeed : $(this.classes.settings + ' input[name=ss_transitionDuration]'),
  731. bgParallaxImageSrc : $(this.classes.settings + ' select[name=bg_parallax_image_src]'),
  732. bgOverlayColor : $(this.classes.settings + ' input[name=bg_overlay_color]'),
  733. bgOverlayOpacity : $(this.classes.settings + ' input[name=bg_overlay_opacity]')
  734. });
  735. // Events
  736. this.elements.bgType.on( 'change', $.proxy(this._bgTypeChange, this));
  737. this.elements.bgColor.on( 'change', $.proxy(this._bgColorChange, this));
  738. this.elements.bgOpacity.on( 'keyup', $.proxy(this._bgOpacityChange, this));
  739. this.elements.bgImageSrc.on( 'change', $.proxy(this._bgPhotoChange, this));
  740. this.elements.bgRepeat.on( 'change', $.proxy(this._bgPhotoChange, this));
  741. this.elements.bgPosition.on( 'change', $.proxy(this._bgPhotoChange, this));
  742. this.elements.bgAttachment.on( 'change', $.proxy(this._bgPhotoChange, this));
  743. this.elements.bgSize.on( 'change', $.proxy(this._bgPhotoChange, this));
  744. this.elements.bgVideoServiceUrl.on( 'change', $.proxy(this._bgVideoChange, this));
  745. this.elements.bgSlideshowSource.on( 'change', $.proxy(this._bgSlideshowChange, this));
  746. this.elements.bgSlideshowPhotos.on( 'change', $.proxy(this._bgSlideshowChange, this));
  747. this.elements.bgSlideshowFeedUrl.on( 'keyup', $.proxy(this._bgSlideshowChange, this));
  748. this.elements.bgSlideshowSpeed.on( 'keyup', $.proxy(this._bgSlideshowChange, this));
  749. this.elements.bgSlideshowTrans.on( 'change', $.proxy(this._bgSlideshowChange, this));
  750. this.elements.bgSlideshowTransSpeed.on( 'keyup', $.proxy(this._bgSlideshowChange, this));
  751. this.elements.bgParallaxImageSrc.on( 'change', $.proxy(this._bgParallaxChange, this));
  752. this.elements.bgOverlayColor.on( 'change', $.proxy(this._bgOverlayChange, this));
  753. this.elements.bgOverlayOpacity.on( 'keyup', $.proxy(this._bgOverlayChange, this));
  754. },
  755. /**
  756. * Fires when the background type field of
  757. * a node changes.
  758. *
  759. * @since 1.3.3
  760. * @access private
  761. * @method _bgTypeChange
  762. * @param {Object} e An event object.
  763. */
  764. _bgTypeChange: function(e)
  765. {
  766. var val = this.elements.bgType.val();
  767. // Clear bg styles first.
  768. this.elements.node.removeClass('fl-row-bg-video');
  769. this.elements.node.removeClass('fl-row-bg-slideshow');
  770. this.elements.node.removeClass('fl-row-bg-parallax');
  771. this.elements.node.find('.fl-bg-video').remove();
  772. this.elements.node.find('.fl-bg-slideshow').remove();
  773. this.elements.content.css('background-image', '');
  774. this.updateCSSRule(this.classes.content, {
  775. 'background-color' : 'transparent',
  776. 'background-image' : 'none'
  777. });
  778. // None
  779. if(val == 'none') {
  780. this._bgOverlayClear();
  781. }
  782. // Color
  783. else if(val == 'color') {
  784. this.elements.bgColor.trigger('change');
  785. this._bgOverlayClear();
  786. }
  787. // Photo
  788. else if(val == 'photo') {
  789. this.elements.bgColor.trigger('change');
  790. this.elements.bgImageSrc.trigger('change');
  791. }
  792. // Video
  793. else if(val == 'video') {
  794. this.elements.bgColor.trigger('change');
  795. this._bgVideoChange();
  796. }
  797. // Slideshow
  798. else if(val == 'slideshow') {
  799. this.elements.bgColor.trigger('change');
  800. this._bgSlideshowChange();
  801. }
  802. // Parallax
  803. else if(val == 'parallax') {
  804. this.elements.bgColor.trigger('change');
  805. this.elements.bgParallaxImageSrc.trigger('change');
  806. }
  807. },
  808. /**
  809. * Fires when the background color field of
  810. * a node changes.
  811. *
  812. * @since 1.3.3
  813. * @access private
  814. * @method _bgColorChange
  815. * @param {Object} e An event object.
  816. */
  817. _bgColorChange: function(e)
  818. {
  819. var rgb, alpha, value;
  820. if(this.elements.bgColor.val() === '' || isNaN(this.elements.bgOpacity.val())) {
  821. this.updateCSSRule(this.classes.content, 'background-color', 'transparent');
  822. }
  823. else {
  824. rgb = this.hexToRgb( this.elements.bgColor.val() );
  825. alpha = this.parseFloat(this.elements.bgOpacity.val())/100;
  826. value = 'rgba(' + rgb.join() + ', ' + alpha + ')';
  827. this.delay(100, $.proxy(function(){
  828. this.updateCSSRule(this.classes.content, 'background-color', value);
  829. }, this));
  830. }
  831. },
  832. /**
  833. * Fires when the background opacity field of
  834. * a node changes.
  835. *
  836. * @since 1.3.3
  837. * @access private
  838. * @method _bgOpacityChange
  839. * @param {Object} e An event object.
  840. */
  841. _bgOpacityChange: function(e)
  842. {
  843. this.elements.bgColor.trigger('change');
  844. },
  845. /**
  846. * Fires when the background photo field of
  847. * a node changes.
  848. *
  849. * @since 1.3.3
  850. * @access private
  851. * @method _bgPhotoChange
  852. * @param {Object} e An event object.
  853. */
  854. _bgPhotoChange: function(e)
  855. {
  856. if(this.elements.bgImageSrc.val()) {
  857. this.updateCSSRule(this.classes.content, {
  858. 'background-image' : 'url(' + this.elements.bgImageSrc.val() + ')',
  859. 'background-repeat' : this.elements.bgRepeat.val(),
  860. 'background-position' : this.elements.bgPosition.val(),
  861. 'background-attachment' : this.elements.bgAttachment.val(),
  862. 'background-size' : this.elements.bgSize.val()
  863. });
  864. }
  865. else {
  866. this.updateCSSRule(this.classes.content, {
  867. 'background-image' : 'none'
  868. });
  869. }
  870. },
  871. /**
  872. * Fires when the background video field of
  873. * a node changes.
  874. *
  875. * @since 1.9.2
  876. * @access private
  877. * @method _bgVideoChange
  878. * @param {Object} e An event object.
  879. */
  880. _bgVideoChange: function(e)
  881. {
  882. var eles = this.elements,
  883. source = eles.bgVideoSource.val(),
  884. video = eles.bgVideo.val(),
  885. videoUrl = eles.bgVideoServiceUrl.val(),
  886. youtubePlayer = 'https://www.youtube.com/iframe_api',
  887. vimeoPlayer = 'https://player.vimeo.com/api/player.js',
  888. scriptTag = $( '<script>' );
  889. // Only load the required API script library
  890. if(source == 'video_service' && videoUrl != '') {
  891. if (/^(?:(?:(?:https?:)?\/\/)?(?:www.)?(?:youtu(?:be.com|.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/i.test(videoUrl)
  892. && $( 'script[src*="youtube.com"' ).length < 1) {
  893. scriptTag.attr('src', youtubePlayer);
  894. }
  895. else if(/^(http\:\/\/|https\:\/\/)?(www\.)?(vimeo\.com\/)([0-9]+)$/.test(videoUrl)
  896. && $( 'script[src*="vimeo.com"' ).length < 1) {
  897. scriptTag.attr('src', vimeoPlayer);
  898. }
  899. scriptTag
  900. .attr('type', 'text/javascript')
  901. .appendTo('head');
  902. this.delay(500, $.proxy(this.preview, this));
  903. }
  904. else if(video != '') {
  905. this.preview();
  906. }
  907. },
  908. /**
  909. * Fires when the background slideshow field of
  910. * a node changes.
  911. *
  912. * @since 1.3.3
  913. * @access private
  914. * @method _bgSlideshowChange
  915. * @param {Object} e An event object.
  916. */
  917. _bgSlideshowChange: function(e)
  918. {
  919. var eles = this.elements,
  920. source = eles.bgSlideshowSource.val(),
  921. photos = eles.bgSlideshowPhotos.val(),
  922. feed = eles.bgSlideshowFeedUrl.val(),
  923. speed = eles.bgSlideshowSpeed.val(),
  924. transSpeed = eles.bgSlideshowTransSpeed.val();
  925. if(source == 'wordpress' && photos === '') {
  926. return;
  927. }
  928. else if(source == 'smugmug' && feed === '') {
  929. return;
  930. }
  931. else if(isNaN(parseInt(speed))) {
  932. return;
  933. }
  934. else if(isNaN(parseInt(transSpeed))) {
  935. return;
  936. }
  937. this.delay(500, $.proxy(this.preview, this));
  938. },
  939. /**
  940. * Fires when the background parallax field of
  941. * a node changes.
  942. *
  943. * @since 1.3.3
  944. * @access private
  945. * @method _bgParallaxChange
  946. * @param {Object} e An event object.
  947. */
  948. _bgParallaxChange: function(e)
  949. {
  950. if(this.elements.bgParallaxImageSrc.val()) {
  951. this.updateCSSRule(this.classes.content, {
  952. 'background-image' : 'url(' + this.elements.bgParallaxImageSrc.val() + ')',
  953. 'background-repeat' : 'no-repeat',
  954. 'background-position' : 'center center',
  955. 'background-attachment' : 'fixed',
  956. 'background-size' : 'cover'
  957. });
  958. }
  959. },
  960. /**
  961. * Fires when the background overlay field of
  962. * a node changes.
  963. *
  964. * @since 1.3.3
  965. * @access private
  966. * @method _bgOverlayChange
  967. * @param {Object} e An event object.
  968. */
  969. _bgOverlayChange: function(e)
  970. {
  971. var rgb, alpha, value;
  972. if(this.elements.bgOverlayColor.val() === '' || isNaN(this.elements.bgOverlayOpacity.val())) {
  973. this.elements.node.removeClass('fl-row-bg-overlay');
  974. this.elements.node.removeClass('fl-col-bg-overlay');
  975. this.updateCSSRule(this.classes.content + ':after', 'background-color', 'transparent');
  976. }
  977. else {
  978. rgb = this.hexToRgb(this.elements.bgOverlayColor.val());
  979. alpha = this.parseFloat(this.elements.bgOverlayOpacity.val())/100;
  980. value = 'rgba(' + rgb.join() + ', ' + alpha + ')';
  981. this.delay(100, $.proxy(function(){
  982. if ( this.elements.node.hasClass( 'fl-col' ) ) {
  983. this.elements.node.addClass( 'fl-col-bg-overlay' );
  984. }
  985. else {
  986. this.elements.node.addClass( 'fl-row-bg-overlay' );
  987. }
  988. this.updateCSSRule( this.classes.content + ':after', 'background-color', value );
  989. }, this));
  990. }
  991. },
  992. /**
  993. * Fires when a background overlay color is cleared.
  994. *
  995. * @since 1.3.3
  996. * @access private
  997. * @method _bgOverlayClear
  998. * @param {Object} e An event object.
  999. */
  1000. _bgOverlayClear: function(e)
  1001. {
  1002. this.elements.bgOverlayColor.prev('.fl-color-picker-clear').trigger('click');
  1003. },
  1004. /* Node Border Settings
  1005. ----------------------------------------------------------*/
  1006. /**
  1007. * Initializes node border previews.
  1008. *
  1009. * @since 1.3.3
  1010. * @access private
  1011. * @method _initNodeBorder
  1012. */
  1013. _initNodeBorder: function()
  1014. {
  1015. // Elements
  1016. $.extend(this.elements, {
  1017. borderType : $(this.classes.settings + ' select[name=border_type]'),
  1018. borderColor : $(this.classes.settings + ' input[name=border_color]'),
  1019. borderColorPicker : $(this.classes.settings + ' .fl-picker-border_color'),
  1020. borderOpacity : $(this.classes.settings + ' input[name=border_opacity]')
  1021. });
  1022. // Events
  1023. this.elements.borderType.on( 'change', $.proxy(this._borderTypeChange, this));
  1024. this.elements.borderColor.on( 'change', $.proxy(this._borderColorChange, this));
  1025. this.elements.borderOpacity.on( 'keyup', $.proxy(this._borderOpacityChange, this));
  1026. },
  1027. /**
  1028. * Fires when the border type field of
  1029. * a node changes.
  1030. *
  1031. * @since 1.3.3
  1032. * @access private
  1033. * @method _borderTypeChange
  1034. * @param {Object} e An event object.
  1035. */
  1036. _borderTypeChange: function(e)
  1037. {
  1038. var val = this.elements.borderType.val();
  1039. this.updateCSSRule(this.classes.content, {
  1040. 'border-style' : val === '' ? 'none' : val
  1041. });
  1042. this.elements.borderColor.trigger('change');
  1043. this.elements.borderTop.trigger('keyup');
  1044. },
  1045. /**
  1046. * Fires when the border color field of
  1047. * a node changes.
  1048. *
  1049. * @since 1.3.3
  1050. * @access private
  1051. * @method _borderColorChange
  1052. * @param {Object} e An event object.
  1053. */
  1054. _borderColorChange: function(e)
  1055. {
  1056. var rgb, alpha, value;
  1057. if(this.elements.borderColor.val() === '' || isNaN(this.elements.borderOpacity.val())) {
  1058. this.updateCSSRule(this.classes.content, 'border-color', 'transparent');
  1059. }
  1060. else {
  1061. rgb = this.hexToRgb(this.elements.borderColor.val());
  1062. alpha = parseInt(this.elements.borderOpacity.val())/100;
  1063. value = 'rgba(' + rgb.join() + ', ' + alpha + ')';
  1064. this.delay(100, $.proxy(function(){
  1065. this.updateCSSRule(this.classes.content, 'border-color', value);
  1066. }, this));
  1067. }
  1068. },
  1069. /**
  1070. * Fires when the border opacity field of
  1071. * a node changes.
  1072. *
  1073. * @since 1.3.3
  1074. * @access private
  1075. * @method _borderOpacityChange
  1076. * @param {Object} e An event object.
  1077. */
  1078. _borderOpacityChange: function(e)
  1079. {
  1080. this.elements.borderColor.trigger('change');
  1081. },
  1082. /* Node Class Name Settings
  1083. ----------------------------------------------------------*/
  1084. /**
  1085. * Initializes node classname previews.
  1086. *
  1087. * @since 1.3.3
  1088. * @access private
  1089. * @method _initNodeClassName
  1090. */
  1091. _initNodeClassName: function()
  1092. {
  1093. // Elements
  1094. $.extend(this.elements, {
  1095. className : $(this.classes.settings + ' input[name=class]')
  1096. });
  1097. // Events
  1098. this.elements.className.on('keyup', $.proxy(this._classNameChange, this));
  1099. this._lastClassName = this.elements.className.val();
  1100. },
  1101. /**
  1102. * Fires when the classname of a node changes.
  1103. *
  1104. * @since 1.3.3
  1105. * @access private
  1106. * @method _classNameChange
  1107. * @param {Object} e An event object.
  1108. */
  1109. _classNameChange: function(e)
  1110. {
  1111. var className = this.elements.className.val();
  1112. if(this._lastClassName !== null) {
  1113. this.elements.node.removeClass(this._lastClassName);
  1114. }
  1115. this.elements.node.addClass(className);
  1116. this._lastClassName = className;
  1117. },
  1118. /* Node Margin Settings
  1119. ----------------------------------------------------------*/
  1120. /**
  1121. * Initializes node responsive dimension previews for things
  1122. * like margins, padding and borders.
  1123. *
  1124. * @since 1.9
  1125. * @access private
  1126. * @method _initResponsiveDimensions
  1127. */
  1128. _initResponsiveDimensions: function( property )
  1129. {
  1130. var elements = {},
  1131. dimensions = [ 'Top', 'Bottom', 'Left', 'Right' ],
  1132. devices = [ '', 'Medium', 'Responsive' ],
  1133. settingsClass = this.classes.settings,
  1134. elementKey = '',
  1135. inputName = '',
  1136. i = null,
  1137. k = null;
  1138. for ( i = 0; i < dimensions.length; i++ ) {
  1139. for ( k = 0; k < devices.length; k++ ) {
  1140. elementKey = property + dimensions[ i ] + devices[ k ];
  1141. inputName = property + '_' + dimensions[ i ].toLowerCase();
  1142. if ( '' != devices[ k ] ) {
  1143. inputName += '_' + devices[ k ].toLowerCase();
  1144. }
  1145. elements[ elementKey ] = $( settingsClass + ' input[name=' + inputName + ']');
  1146. elements[ elementKey ].on( 'keyup', $.proxy( this._responsiveDimensionChange, this, property ) );
  1147. }
  1148. }
  1149. $.extend( this.elements, elements );
  1150. },
  1151. /**
  1152. * Get all dimensions from a node preview event.
  1153. *
  1154. * @since 1.9
  1155. * @access private
  1156. * @method _getDimensions
  1157. * @param {String} property
  1158. * @return {Object} An object with tblr data.
  1159. */
  1160. _getDimensions: function( property )
  1161. {
  1162. var mode = FLBuilderResponsiveEditing._mode,
  1163. dimensions = [ 'Top', 'Bottom', 'Left', 'Right' ],
  1164. device = 'default' == mode ? '' : mode.charAt(0).toUpperCase() + mode.slice(1),
  1165. values = {},
  1166. i = 0;
  1167. for ( ; i < dimensions.length; i++ ) {
  1168. values[ dimensions[ i ].toLowerCase() ] = this.elements[ property + dimensions[ i ] + device ].val();
  1169. }
  1170. return this._normalizeDimensionValues( values, property );
  1171. },
  1172. /**
  1173. * Fires when a dimension field of a node changes.
  1174. *
  1175. * @since 1.9
  1176. * @access private
  1177. * @method _responsiveDimensionChange
  1178. * @param {String} property
  1179. */
  1180. _responsiveDimensionChange: function( property )
  1181. {
  1182. var newDimensions = this._getDimensions( property ),
  1183. newRules = {},
  1184. isBorder = 'border' == property;
  1185. $.each( newDimensions, function( dir, val ) {
  1186. newRules[ property + '-' + dir + ( isBorder ? '-width' : '' ) ] = val;
  1187. } );
  1188. this.updateResponsiveCSSRule( this.classes.content, newRules );
  1189. this._positionAbsoluteBgs();
  1190. },
  1191. /**
  1192. * Normalize CSS dimension values.
  1193. *
  1194. * @since 1.9
  1195. * @access private
  1196. * @method _normalizeDimensionValues
  1197. * @param {Object} values Object of margins/paddings/border widths values.
  1198. * @param {String} property CSS property to get, can be 'margin', 'padding' or 'border-width'.
  1199. * @return {Object} An object of normalized margins/paddings/border widths values with units.
  1200. */
  1201. _normalizeDimensionValues: function( values, property )
  1202. {
  1203. var parent = this,
  1204. mode = FLBuilderResponsiveEditing._mode,
  1205. device = 'default' == mode ? '' : mode.charAt(0).toUpperCase() + mode.slice(1),
  1206. // Separate property name from property suffix
  1207. property = property.split( '-' );
  1208. // Set correct property suffix
  1209. if ( 'undefined' === typeof property[1] ) {
  1210. property[1] = '';
  1211. } else {
  1212. property[1] = property[1].charAt( 0 ).toUpperCase() + property[1].slice( 1 );
  1213. }
  1214. $.map( values, function( val, key ) {
  1215. val = val.toLowerCase().replace( /[^a-z0-9%.\-]/g, '' );
  1216. // Fall back to placeholder if the value is empty
  1217. if ( '' === val ) {
  1218. var option = property[0] + key.charAt( 0 ).toUpperCase() + key.slice( 1 ) + property[1] + device,
  1219. placeholder = parent.elements[ option ].attr( 'placeholder' );
  1220. if ( placeholder ) {
  1221. val = placeholder;
  1222. }
  1223. }
  1224. // Fall back to pixels when numeric value set
  1225. if ( null !== val && '' !== val && ! isNaN( val ) ) {
  1226. val = parseFloat( val ) + 'px';
  1227. }
  1228. values[ key ] = val;
  1229. } );
  1230. return values;
  1231. },
  1232. /* Absolutely Positioned Backgrounds
  1233. ----------------------------------------------------------*/
  1234. /**
  1235. * Positions the backgrounds of a node that need absolute
  1236. * positioning such as videos and slideshows.
  1237. *
  1238. * @since 1.3.3
  1239. * @access private
  1240. * @method _positionAbsoluteBgs
  1241. */
  1242. _positionAbsoluteBgs: function()
  1243. {
  1244. // @todo Oliver: Why is this actually needed???
  1245. var slideshow = this.elements.node.find('.fl-bg-slideshow'),
  1246. video = this.elements.node.find('.fl-bg-video'),
  1247. margins = null,
  1248. borders = null,
  1249. position = {
  1250. 'top' : 0,
  1251. 'bottom' : 0,
  1252. 'left' : 0,
  1253. 'right' : 0,
  1254. };
  1255. if ( slideshow.length > 0 || video.length > 0 ) {
  1256. margins = this._getDimensions( 'margin' );
  1257. borders = this._getDimensions( 'border' );
  1258. $.map( position, function( val, key ) {
  1259. if ( margins[ key ] && borders[ key ] ) {
  1260. position[ key ] = 'calc(' + margins[ key ] + '+' + borders[ key ] + ')';
  1261. } else if ( margins[ key ] ) {
  1262. position[ key ] = margins[ key ];
  1263. } else if ( borders[ key ] ) {
  1264. position[ key ] = borders[ key ];
  1265. }
  1266. } );
  1267. if ( slideshow.length > 0 ) {
  1268. this.updateCSSRule( this.classes.node + ' .fl-bg-slideshow', position );
  1269. FLBuilder._resizeLayout();
  1270. }
  1271. if ( video.length > 0 ) {
  1272. this.updateCSSRule( this.classes.node + ' .fl-bg-video', position );
  1273. }
  1274. }
  1275. },
  1276. /* Row Settings
  1277. ----------------------------------------------------------*/
  1278. /**
  1279. * Initializes a row preview.
  1280. *
  1281. * @since 1.3.3
  1282. * @access private
  1283. * @method _initRow
  1284. */
  1285. _initRow: function()
  1286. {
  1287. // Elements
  1288. $.extend(this.elements, {
  1289. width : $(this.classes.settings + ' select[name=width]'),
  1290. contentWidth : $(this.classes.settings + ' select[name=content_width]'),
  1291. maxContentWidth : $(this.classes.settings + ' input[name=max_content_width]'),
  1292. height : $(this.classes.settings + ' select[name=full_height]'),
  1293. align : $(this.classes.settings + ' select[name=content_alignment]')
  1294. });
  1295. // Events
  1296. this.elements.width.on( 'change', $.proxy(this._rowWidthChange, this));
  1297. this.elements.contentWidth.on( 'change', $.proxy(this._rowContentWidthChange, this));
  1298. this.elements.maxContentWidth.on( 'keyup', $.proxy(this._rowMaxContentWidthChange, this));
  1299. this.elements.height.on( 'change', $.proxy(this._rowHeightChange, this));
  1300. this.elements.align.on( 'change', $.proxy(this._rowHeightChange, this));
  1301. // Common Elements
  1302. this._initNodeTextColor();
  1303. this._initNodeBg();
  1304. this._initNodeClassName();
  1305. this._initNodeBorder();
  1306. this._initResponsiveDimensions( 'border' );
  1307. this._initResponsiveDimensions( 'margin' );
  1308. this._initResponsiveDimensions( 'padding' );
  1309. },
  1310. /**
  1311. * Fires when the width field of a row changes.
  1312. *
  1313. * @since 1.3.3
  1314. * @access private
  1315. * @method _rowWidthChange
  1316. * @param {Object} e An event object.
  1317. */
  1318. _rowWidthChange: function(e)
  1319. {
  1320. var row = this.elements.node,
  1321. maxWidth = this.elements.maxContentWidth.val();
  1322. if(this.elements.width.val() == 'full') {
  1323. row.css( 'max-width', 'none' );
  1324. row.removeClass('fl-row-fixed-width');
  1325. row.addClass('fl-row-full-width');
  1326. }
  1327. else {
  1328. row.removeClass('fl-row-full-width');
  1329. row.addClass('fl-row-fixed-width');
  1330. if ( '' !== maxWidth ) {
  1331. row.css( 'max-width', maxWidth + 'px' );
  1332. } else {
  1333. row.css( 'max-width', FLBuilderConfig.global.row_width + 'px' );
  1334. }
  1335. }
  1336. },
  1337. /**
  1338. * Fires when the height field of a row changes.
  1339. *
  1340. * @since 1.6.3
  1341. * @access private
  1342. * @method _rowHeightChange
  1343. * @param {Object} e An event object.
  1344. */
  1345. _rowHeightChange: function(e)
  1346. {
  1347. var row = this.elements.node;
  1348. row.removeClass('fl-row-align-top');
  1349. row.removeClass('fl-row-align-center');
  1350. row.removeClass('fl-row-align-bottom');
  1351. if(this.elements.height.val() == 'full') {
  1352. row.addClass('fl-row-full-height');
  1353. row.addClass('fl-row-align-' + this.elements.align.val());
  1354. }
  1355. else {
  1356. row.removeClass('fl-row-full-height');
  1357. }
  1358. },
  1359. /**
  1360. * Fires when the content width field of a row changes.
  1361. *
  1362. * @since 1.3.3
  1363. * @access private
  1364. * @method _rowContentWidthChange
  1365. * @param {Object} e An event object.
  1366. */
  1367. _rowContentWidthChange: function(e)
  1368. {
  1369. var content = this.elements.content.find('.fl-row-content'),
  1370. maxWidth = this.elements.maxContentWidth.val();
  1371. if(this.elements.contentWidth.val() == 'full') {
  1372. content.css( 'max-width', 'none' );
  1373. content.removeClass('fl-row-fixed-width');
  1374. content.addClass('fl-row-full-width');
  1375. }
  1376. else {
  1377. content.removeClass('fl-row-full-width');
  1378. content.addClass('fl-row-fixed-width');
  1379. if ( '' !== maxWidth ) {
  1380. content.css( 'max-width', maxWidth + 'px' );
  1381. } else {
  1382. content.css( 'max-width', FLBuilderConfig.global.row_width + 'px' );
  1383. }
  1384. }
  1385. },
  1386. /**
  1387. * Fires when the content width field of a row changes.
  1388. *
  1389. * @since 1.3.3
  1390. * @access private
  1391. * @method _rowContentWidthChange
  1392. * @param {Object} e An event object.
  1393. */
  1394. _rowMaxContentWidthChange: function(e)
  1395. {
  1396. var row = this.elements.node,
  1397. content = this.elements.content.find('.fl-row-content'),
  1398. width = this.elements.maxContentWidth.val();
  1399. if ( '' == width ) {
  1400. width = FLBuilderConfig.global.row_width + 'px';
  1401. } else {
  1402. width += 'px';
  1403. }
  1404. if ( 'fixed' == this.elements.width.val() ) {
  1405. row.css( 'max-width', width );
  1406. }
  1407. if ( 'fixed' == this.elements.contentWidth.val() ) {
  1408. content.css( 'max-width', width );
  1409. }
  1410. },
  1411. /* Columns Settings
  1412. ----------------------------------------------------------*/
  1413. /**
  1414. * Initializes a column preview.
  1415. *
  1416. * @since 1.3.3
  1417. * @access private
  1418. * @method _initRow
  1419. */
  1420. _initColumn: function()
  1421. {
  1422. // Elements
  1423. $.extend(this.elements, {
  1424. size : $(this.classes.settings + ' input[name=size]'),
  1425. columnHeight : $(this.classes.settings + ' select[name=equal_height]'),
  1426. columnAlign : $(this.classes.settings + ' select[name=content_alignment]'),
  1427. responsiveOrder : $(this.classes.settings + ' select[name=responsive_order]')
  1428. });
  1429. // Events
  1430. this.elements.size.on( 'keyup', $.proxy( this._colSizeChange, this ) );
  1431. this.elements.columnHeight.on( 'change', $.proxy( this._colHeightChange, this ) );
  1432. this.elements.columnAlign.on( 'change', $.proxy( this._colHeightChange, this ) );
  1433. this.elements.responsiveOrder.on( 'change', $.proxy( this._colResponsiveOrder, this ) );
  1434. // Common Elements
  1435. this._initNodeTextColor();
  1436. this._initNodeBg();
  1437. this._initNodeClassName();
  1438. this._initNodeBorder();
  1439. this._initResponsiveDimensions( 'border' );
  1440. this._initResponsiveDimensions( 'margin' );
  1441. this._initResponsiveDimensions( 'padding' );
  1442. },
  1443. /**
  1444. * Fires when the size field of a column changes.
  1445. *
  1446. * @since 1.3.3
  1447. * @access private
  1448. * @method _colSizeChange
  1449. */
  1450. _colSizeChange: function()
  1451. {
  1452. var preview = this,
  1453. minWidth = 8,
  1454. maxWidth = 100 - minWidth,
  1455. size = parseFloat(this.elements.size.val()),
  1456. prev = this.elements.node.prev('.fl-col'),
  1457. next = this.elements.node.next('.fl-col'),
  1458. sibling = next.length === 0 ? prev : next,
  1459. siblings = this.elements.node.siblings('.fl-col'),
  1460. siblingsWidth = 0;
  1461. // Don't resize if we onlt have one column or no size.
  1462. if(siblings.length === 0 || isNaN(size)) {
  1463. return;
  1464. }
  1465. // Adjust sizes based on other columns.
  1466. siblings.each(function() {
  1467. if($(this).data('node') == sibling.data('node')) {
  1468. return;
  1469. }
  1470. maxWidth -= parseFloat($(this)[0].style.width);
  1471. siblingsWidth += parseFloat($(this)[0].style.width);
  1472. });
  1473. // Make sure the new width isn't too small.
  1474. if(size < minWidth) {
  1475. size = minWidth;
  1476. }
  1477. // Make sure the new width isn't too big.
  1478. if(size > maxWidth) {
  1479. size = maxWidth;
  1480. }
  1481. // Update the widths.
  1482. sibling.css('width', (100 - siblingsWidth - size) + '%');
  1483. this.elements.node.css('width', size + '%');
  1484. },
  1485. /**
  1486. * Fires when the equal height field of a column changes.
  1487. *
  1488. * @since 1.6.3
  1489. * @access private
  1490. * @method _colHeightChange
  1491. */
  1492. _colHeightChange: function()
  1493. {
  1494. var parent = this.elements.node.parent('.fl-col-group');
  1495. parent.removeClass('fl-col-group-align-top');
  1496. parent.removeClass('fl-col-group-align-center');
  1497. parent.removeClass('fl-col-group-align-bottom');
  1498. if(this.elements.columnHeight.val() == 'yes') {
  1499. parent.addClass('fl-col-group-equal-height');
  1500. parent.addClass('fl-col-group-align-' + this.elements.columnAlign.val());
  1501. }
  1502. else {
  1503. parent.removeClass('fl-col-group-equal-height');
  1504. }
  1505. },
  1506. /**
  1507. * Fires when the responsive order field of a column changes.
  1508. *
  1509. * @since 1.8
  1510. * @access private
  1511. * @method _colResponsiveOrder
  1512. */
  1513. _colResponsiveOrder: function()
  1514. {
  1515. var parent = this.elements.node.parent('.fl-col-group');
  1516. if(this.elements.responsiveOrder.val() == 'reversed') {
  1517. parent.addClass('fl-col-group-responsive-reversed');
  1518. }
  1519. else {
  1520. parent.removeClass('fl-col-group-responsive-reversed');
  1521. }
  1522. },
  1523. /* Module Settings
  1524. ----------------------------------------------------------*/
  1525. /**
  1526. * Initializes a module preview.
  1527. *
  1528. * @since 1.3.3
  1529. * @access private
  1530. * @method _initRow
  1531. */
  1532. _initModule: function()
  1533. {
  1534. this._initNodeClassName();
  1535. this._initResponsiveDimensions( 'margin' );
  1536. },
  1537. /* Default Field Previews
  1538. ----------------------------------------------------------*/
  1539. /**
  1540. * Initializes the default preview logic for each
  1541. * field in a settings form.
  1542. *
  1543. * @since 1.3.3
  1544. * @access private
  1545. * @method _initDefaultFieldPreviews
  1546. * @param {Object} fields
  1547. */
  1548. _initDefaultFieldPreviews: function( fields )
  1549. {
  1550. var fields = ! _.isUndefined( fields ) ? fields : this.elements.settings.find('.fl-field'),
  1551. field = null,
  1552. fieldType = null,
  1553. preview = null,
  1554. i = 0;
  1555. for( ; i < fields.length; i++) {
  1556. field = fields.eq(i);
  1557. preview = field.data('preview');
  1558. if(preview.type == 'refresh') {
  1559. this._initFieldRefreshPreview(field);
  1560. }
  1561. if(preview.type == 'text') {
  1562. this._initFieldTextPreview(field);
  1563. }
  1564. if(preview.type == 'css') {
  1565. this._initFieldCSSPreview(field);
  1566. }
  1567. if(preview.type == 'widget') {
  1568. this._initFieldWidgetPreview(field);
  1569. }
  1570. if(preview.type == 'font') {
  1571. this._initFieldFontPreview(field);
  1572. }
  1573. }
  1574. },
  1575. /**
  1576. * Initializes the refresh preview for a field.
  1577. *
  1578. * @since 1.3.3
  1579. * @access private
  1580. * @method _initFieldRefreshPreview
  1581. * @param {Object} field The field to preview.
  1582. */
  1583. _initFieldRefreshPreview: function(field)
  1584. {
  1585. var fieldType = field.data('type'),
  1586. preview = field.data('preview'),
  1587. callback = $.proxy(this.delayPreview, this);
  1588. switch(fieldType) {
  1589. case 'text':
  1590. field.find('input[type=text]').on('keyup', callback);
  1591. break;
  1592. case 'textarea':
  1593. field.find('textarea').on('keyup', callback);
  1594. break;
  1595. case 'select':
  1596. field.find('select').on('change', callback);
  1597. break;
  1598. case 'color':
  1599. field.find('.fl-color-picker-value').on('change', callback);
  1600. break;
  1601. case 'photo':
  1602. field.find('select').on('change', callback);
  1603. break;
  1604. case 'multiple-photos':
  1605. field.find('input').on('change', callback);
  1606. break;
  1607. case 'photo-sizes':
  1608. field.find('select').on('change', callback);
  1609. break;
  1610. case 'video':
  1611. field.find('input').on('change', callback);
  1612. break;
  1613. case 'multiple-audios':
  1614. field.find('input').on('change', callback);
  1615. break;
  1616. case 'icon':
  1617. field.find('input').on('change', callback);
  1618. break;
  1619. case 'form':
  1620. field.delegate('input', 'change', callback);
  1621. break;
  1622. case 'editor':
  1623. this._addTextEditorCallback(field, preview);
  1624. break;
  1625. case 'code':
  1626. field.find('textarea').on('change', callback);
  1627. break;
  1628. case 'post-type':
  1629. field.find('select').on('change', callback);
  1630. break;
  1631. case 'suggest':
  1632. field.find('.as-values').on('change', callback);
  1633. field.find('select').on('change', callback);
  1634. break;
  1635. case 'unit':
  1636. case 'dimension':
  1637. field.find('input[type=number]').on('keyup', callback);
  1638. break;
  1639. case 'ordering':
  1640. field.find('input[type=hidden]').on('change', callback);
  1641. break;
  1642. default:
  1643. field.on('change', callback);
  1644. }
  1645. },
  1646. /**
  1647. * Initializes a text preview for a field.
  1648. *
  1649. * @since 1.3.3
  1650. * @access private
  1651. * @method _initFieldTextPreview
  1652. * @param {Object} field The field to preview.
  1653. */
  1654. _initFieldTextPreview: function(field)
  1655. {
  1656. var fieldType = field.data('type'),
  1657. preview = field.data('preview'),
  1658. callback = $.proxy(this._previewText, this, preview);
  1659. switch(fieldType) {
  1660. case 'text':
  1661. field.find('input[type=text]').on('keyup', callback);
  1662. break;
  1663. case 'unit':
  1664. field.find('input[type=number]').on('keyup', callback);
  1665. break;
  1666. case 'textarea':
  1667. field.find('textarea').on('keyup', callback);
  1668. break;
  1669. case 'code':
  1670. field.find('textarea').on('change', callback);
  1671. break;
  1672. case 'editor':
  1673. this._addTextEditorCallback(field, preview);
  1674. break;
  1675. }
  1676. },
  1677. /**
  1678. * Runs a real time preview for text fields.
  1679. *
  1680. * @since 1.3.3
  1681. * @access private
  1682. * @method _previewText
  1683. * @param {Object} preview A preview object.
  1684. * @param {Object} e An event object.
  1685. */
  1686. _previewText: function(preview, e)
  1687. {
  1688. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  1689. element = $( selector ),
  1690. text = $('<div>' + $(e.target).val() + '</div>');
  1691. if(element.length > 0) {
  1692. text.find('script').remove();
  1693. element.html(text.html());
  1694. } else {
  1695. this.delayPreview(e);
  1696. }
  1697. },
  1698. /**
  1699. * Runs a real time preview for text editor fields.
  1700. *
  1701. * @since 1.3.3
  1702. * @access private
  1703. * @method _previewText
  1704. * @param {Object} preview A preview object.
  1705. * @param {String} id The ID of the text editor.
  1706. * @param {Object} e An event object.
  1707. */
  1708. _previewTextEditor: function(preview, id, e)
  1709. {
  1710. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  1711. element = $( selector ),
  1712. editor = typeof tinyMCE != 'undefined' ? tinyMCE.get(id) : null,
  1713. textarea = $('#' + id),
  1714. text = '';
  1715. if(element.length > 0) {
  1716. if(editor && textarea.css('display') == 'none') {
  1717. text = $('<div>' + editor.getContent() + '</div>');
  1718. }
  1719. else {
  1720. if ( 'undefined' == typeof switchEditors || 'undefined' == typeof switchEditors.wpautop ) {
  1721. text = $('<div>' + textarea.val() + '</div>');
  1722. }
  1723. else {
  1724. text = $('<div>' + switchEditors.wpautop( textarea.val() ) + '</div>');
  1725. }
  1726. }
  1727. text.find('script').remove();
  1728. element.html(text.html());
  1729. }
  1730. },
  1731. /**
  1732. * Callback for text editor previews.
  1733. *
  1734. * @since 1.3.3
  1735. * @access private
  1736. * @method _previewText
  1737. * @param {Object} field A field object.
  1738. * @param {Object} preview A preview object.
  1739. */
  1740. _addTextEditorCallback: function(field, preview)
  1741. {
  1742. var id = field.find('textarea.wp-editor-area').attr('id'),
  1743. callback = null;
  1744. if(preview.type == 'refresh') {
  1745. callback = $.proxy(this.delayPreview, this);
  1746. }
  1747. else if(preview.type == 'text') {
  1748. callback = $.proxy(this._previewTextEditor, this, preview, id);
  1749. }
  1750. else {
  1751. return;
  1752. }
  1753. $('#' + id).on('keyup', callback);
  1754. if(typeof tinyMCE != 'undefined') {
  1755. editor = tinyMCE.get(id);
  1756. editor.on('change', callback);
  1757. editor.on('keyup', callback);
  1758. }
  1759. },
  1760. /**
  1761. * Initializes a font preview for a field.
  1762. *
  1763. * @since 1.3.3
  1764. * @access private
  1765. * @method _initFieldFontPreview
  1766. * @param {Object} field The field to preview.
  1767. */
  1768. _initFieldFontPreview: function(field)
  1769. {
  1770. var fieldType = field.data('type'),
  1771. preview = field.data('preview');
  1772. // store field id
  1773. preview.id = field.attr( 'id' );
  1774. var callback = $.proxy(this._previewFont, this, preview);
  1775. if( fieldType == 'font' ){
  1776. field.find('.fl-font-field').on('change', 'select', callback);
  1777. }
  1778. },
  1779. /**
  1780. * Gets the selected font and weight, and make the necessary updates for live preview.
  1781. *
  1782. * @since 1.6.3
  1783. * @access private
  1784. * @see _getPreviewSelector
  1785. * @see _buildFontStylesheet
  1786. * @see updateCSSRule
  1787. *
  1788. * @method _previewFont
  1789. * @param {Object} preview An object with data about the current field and css selector.
  1790. * @param {[type]} e The current field.
  1791. */
  1792. _previewFont: function( preview, e ){
  1793. var parent = $( e.delegateTarget ),
  1794. font = parent.find( '.fl-font-field-font' ),
  1795. selected = $( font ).find( ':selected' ),
  1796. fontGroup = selected.parent().attr( 'label' ),
  1797. weight = parent.find( '.fl-font-field-weight' ),
  1798. uniqueID = preview.id + '-' + this.nodeId,
  1799. selector = this._getPreviewSelector( this.classes.node, preview.selector );
  1800. // If the selected font is a Google Font, build the font stylesheet
  1801. if( fontGroup == 'Google' ){
  1802. this._buildFontStylesheet( uniqueID, font.val(), weight.val() );
  1803. }
  1804. if( font.val() == 'Default' ){
  1805. this.updateCSSRule( selector, 'font-family', '' );
  1806. this.updateCSSRule( selector, 'font-weight', '' );
  1807. } else {
  1808. // Updated CSS rules
  1809. this.updateCSSRule( selector, 'font-family', font.val() );
  1810. this.updateCSSRule( selector, 'font-weight', weight.val() );
  1811. }
  1812. },
  1813. /**
  1814. * Gets all fonts store insite FLBuilderPreview._fontsList and renders the respective
  1815. * link tag with Google Fonts.
  1816. *
  1817. * @since 1.6.3
  1818. * @access private
  1819. *
  1820. * @method _buildFontStylesheet
  1821. * @param {String} id The field unique ID.
  1822. * @param {String} font The selected font.
  1823. * @param {String} weight The selected weight.
  1824. */
  1825. _buildFontStylesheet: function( id, font, weight ){
  1826. var url = FLBuilderConfig.googleFontsUrl,
  1827. href = '',
  1828. fontObj = {},
  1829. fontArray = {};
  1830. // build the font family / weight object
  1831. fontObj[ font ] = [ weight ];
  1832. // adds to the list of fonts for this font setting
  1833. FLBuilderPreview._fontsList[ id ] = fontObj;
  1834. // iterate over the keys of the FLBuilderPreview._fontsList object
  1835. Object.keys( FLBuilderPreview._fontsList ).forEach( function( fieldFont ) {
  1836. var field = FLBuilderPreview._fontsList[ fieldFont ];
  1837. // iterate over the font / weight object
  1838. Object.keys( field ).forEach( function( key ) {
  1839. // get the weights of this font
  1840. var weights = field[ key ];
  1841. fontArray[ key ] = fontArray[ key ] || [];
  1842. // remove duplicates from the values array
  1843. weights = weights.filter( function( weight ) {
  1844. return fontArray[ key ].indexOf( weight ) < 0;
  1845. });
  1846. fontArray[ key ] = fontArray[ key ].concat( weights );
  1847. });
  1848. });
  1849. $.each( fontArray, function( font, weight ){
  1850. href += font + ':' + weight.join() + '|';
  1851. } );
  1852. // remove last character and replace spaces with plus signs
  1853. href = url + href.slice( 0, -1 ).replace( ' ', '+' );
  1854. if( $( '#fl-builder-google-fonts-preview' ).length < 1 ){
  1855. $( '<link>' )
  1856. .attr( 'id', 'fl-builder-google-fonts-preview' )
  1857. .attr( 'type', 'text/css' )
  1858. .attr( 'rel', 'stylesheet' )
  1859. .attr( 'href', href )
  1860. .appendTo('head');
  1861. } else{
  1862. $( '#fl-builder-google-fonts-preview' ).attr( 'href', href );
  1863. }
  1864. },
  1865. /**
  1866. * Initializes CSS previews for a node.
  1867. *
  1868. * @since 1.3.3
  1869. * @since 1.6.1 Reworked to accept a preview.rules array.
  1870. * @access private
  1871. * @method _initFieldCSSPreview
  1872. * @param {Object} field A field object.
  1873. */
  1874. _initFieldCSSPreview: function( field )
  1875. {
  1876. var preview = field.data( 'preview' ),
  1877. i = null;
  1878. if ( 'undefined' != typeof preview.rules ) {
  1879. for ( i in preview.rules ) {
  1880. this._initFieldCSSPreviewCallback( field, preview.rules[ i ] );
  1881. }
  1882. }
  1883. else {
  1884. this._initFieldCSSPreviewCallback( field, preview );
  1885. }
  1886. },
  1887. /**
  1888. * Initializes CSS preview callbacks for a field.
  1889. *
  1890. * @since 1.6.1
  1891. * @access private
  1892. * @method _initFieldCSSPreviewCallback
  1893. * @param {Object} field A field object.
  1894. * @param {Object} preview The preview data object.
  1895. */
  1896. _initFieldCSSPreviewCallback: function( field, preview )
  1897. {
  1898. switch( field.data( 'type' ) ) {
  1899. case 'text':
  1900. field.find( 'input[type=text]' ).on( 'keyup', $.proxy( this._previewCSS, this, preview ) );
  1901. break;
  1902. case 'unit':
  1903. field.find( 'input[type=number]' ).on( 'keyup', $.proxy( this._previewCSS, this, preview ) );
  1904. break;
  1905. case 'dimension':
  1906. field.find( 'input[type=number]' ).on( 'keyup', $.proxy( this._previewDimensionCSS, this, preview ) );
  1907. break;
  1908. case 'select':
  1909. field.find( 'select' ).on( 'change', $.proxy( this._previewCSS, this, preview ) );
  1910. break;
  1911. case 'color':
  1912. field.find( '.fl-color-picker-value' ).on( 'change', $.proxy( this._previewColor, this, preview ) );
  1913. break;
  1914. }
  1915. },
  1916. /**
  1917. * Updates the CSS rule for a preview.
  1918. *
  1919. * @since 1.3.3
  1920. * @access private
  1921. * @method _previewCSS
  1922. * @param {Object} preview A preview object.
  1923. * @param {Object} e An event object.
  1924. */
  1925. _previewCSS: function(preview, e)
  1926. {
  1927. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  1928. property = preview.property,
  1929. unit = typeof preview.unit == 'undefined' ? '' : preview.unit,
  1930. input = $(e.target),
  1931. value = input.val();
  1932. if('%' === unit && 'opacity' === property) {
  1933. value = parseInt(value)/100;
  1934. }
  1935. else if ( '' !== value ) {
  1936. value += unit;
  1937. }
  1938. if ( input.closest( '.fl-field-responsive-setting' ).length ) {
  1939. this.updateResponsiveCSSRule( selector, property, value );
  1940. }
  1941. else {
  1942. this.updateCSSRule( selector, property, value );
  1943. }
  1944. },
  1945. /**
  1946. * Updates the CSS rule for a dimension field preview.
  1947. *
  1948. * @since 2.0.7
  1949. * @access private
  1950. * @method _previewDimensionCSS
  1951. * @param {Object} preview A preview object.
  1952. * @param {Object} e An event object.
  1953. */
  1954. _previewDimensionCSS: function( preview, e )
  1955. {
  1956. var dimensionPreview = $.extend( {}, preview ),
  1957. property = dimensionPreview.property,
  1958. unit = $( e.target ).data( 'unit' );
  1959. if ( 'border-width' === property ) {
  1960. dimensionPreview.property = 'border-' + unit + '-width';
  1961. } else {
  1962. dimensionPreview.property = property + '-' + unit;
  1963. }
  1964. this._previewCSS( dimensionPreview, e );
  1965. },
  1966. /**
  1967. * Updates the CSS rule for a color preview.
  1968. *
  1969. * @since 1.3.3
  1970. * @access private
  1971. * @method _previewColor
  1972. * @param {Object} preview A preview object.
  1973. * @param {Object} e An event object.
  1974. */
  1975. _previewColor: function(preview, e)
  1976. {
  1977. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  1978. input = $(e.target),
  1979. val = input.val(),
  1980. color = val === '' ? 'inherit' : '#' + val;
  1981. if ( /^rgb/.test( val.replace(/\s+/g, '') ) ) {
  1982. color = val;
  1983. }
  1984. if ( input.closest( '.fl-field-responsive-setting' ).length ) {
  1985. this.updateResponsiveCSSRule( selector, preview.property, color );
  1986. }
  1987. else {
  1988. this.updateCSSRule( selector, preview.property, color );
  1989. }
  1990. },
  1991. /**
  1992. * Initializes the preview for a WordPress widget.
  1993. *
  1994. * @since 1.3.3
  1995. * @access private
  1996. * @method _initFieldWidgetPreview
  1997. * @param {Object} field A field object.
  1998. */
  1999. _initFieldWidgetPreview: function(field)
  2000. {
  2001. var callback = $.proxy(this.delayPreview, this);
  2002. field.find('input').on('keyup', callback);
  2003. field.find('input[type=checkbox]').on('click', callback);
  2004. field.find('textarea').on('keyup', callback);
  2005. field.find('select').on('change', callback);
  2006. },
  2007. /**
  2008. * Returns a formatted selector string for a preview.
  2009. *
  2010. * @since 1.6.1
  2011. * @access private
  2012. * @method _getPreviewSelector
  2013. * @param {String} selector A CSS selector string.
  2014. * @return {String}
  2015. */
  2016. _getPreviewSelector: function( prefix, selector )
  2017. {
  2018. return FLBuilderPreview.getFormattedSelector( prefix, selector );
  2019. }
  2020. };
  2021. })(jQuery);