fl-builder-preview.js 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628
  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 preview ID.
  12. this.id = new Date().getTime();
  13. // Set the type.
  14. this.type = config.type;
  15. // Save the current state.
  16. this._saveState();
  17. // Initialize the preview.
  18. if ( config.layout ) {
  19. FLBuilder._renderLayout( config.layout, function() {
  20. this._init();
  21. if ( config.callback ) {
  22. config.callback();
  23. }
  24. }.bind( this ) );
  25. } else {
  26. this._init();
  27. }
  28. };
  29. /**
  30. * Stores all the fonts and weights of all font fields.
  31. * This is used to render the stylesheet with Google Fonts.
  32. *
  33. * @since 1.6.3
  34. * @access private
  35. * @property {Array} _fontsList
  36. */
  37. FLBuilderPreview._fontsList = {};
  38. /**
  39. * Returns a formatted selector string for a preview.
  40. *
  41. * @since 2.1
  42. * @method getFormattedSelector
  43. * @param {String} selector A CSS selector string.
  44. * @return {String}
  45. */
  46. FLBuilderPreview.getFormattedSelector = function( prefix, selector = '' )
  47. {
  48. var formatted = '',
  49. pattern = /,(?![^()]*\))/,
  50. parts = selector.split( pattern ),
  51. i = 0;
  52. for ( ; i < parts.length; i++ ) {
  53. if ( parts[ i ].indexOf( '{node}' ) > -1 ) {
  54. formatted += parts[ i ].replace( '{node}', prefix );
  55. } else if ( parts[ i ].indexOf( '{node_id}' ) > -1 ) {
  56. formatted += parts[ i ].replace( /{node_id}/g, this.nodeId );
  57. } else {
  58. formatted += prefix + ' ' + parts[ i ];
  59. }
  60. if ( i != parts.length - 1 ) {
  61. formatted += ', ';
  62. }
  63. }
  64. return formatted;
  65. };
  66. /**
  67. * Prototype for new instances.
  68. *
  69. * @since 1.3.3
  70. * @property {Object} prototype
  71. */
  72. FLBuilderPreview.prototype = {
  73. /**
  74. * A unique ID for this preview.
  75. *
  76. * @since 1.3.3
  77. * @property {String} id
  78. */
  79. id : '',
  80. /**
  81. * The type of node that we are previewing.
  82. *
  83. * @since 1.3.3
  84. * @property {String} type
  85. */
  86. type : '',
  87. /**
  88. * The ID of node that we are previewing.
  89. *
  90. * @since 1.3.3
  91. * @property {String} nodeId
  92. */
  93. nodeId : null,
  94. /**
  95. * An object with data for each CSS class
  96. * in the preview.
  97. *
  98. * @since 1.3.3
  99. * @property {Object} classes
  100. */
  101. classes : {},
  102. /**
  103. * An object with references to each element
  104. * in the preview.
  105. *
  106. * @since 1.3.3
  107. * @property {Object} elements
  108. */
  109. elements : {},
  110. /**
  111. * An object that contains data for the current
  112. * state of a layout before changes are made.
  113. *
  114. * @since 1.3.3
  115. * @property {Object} state
  116. */
  117. state : null,
  118. /**
  119. * Node settings saved when the preview was initalized.
  120. *
  121. * @since 1.7
  122. * @access private
  123. * @property {Object} _savedSettings
  124. */
  125. _savedSettings : null,
  126. /**
  127. * An instance of FLStyleSheet for the current preview.
  128. *
  129. * @since 1.3.3
  130. * @access private
  131. * @property {FLStyleSheet} _styleSheet
  132. */
  133. _styleSheet : null,
  134. /**
  135. * An instance of FLStyleSheet for the large device preview.
  136. *
  137. * @since 2.6
  138. * @access private
  139. * @property {FLStyleSheet} _styleSheetLarge
  140. */
  141. _styleSheetLarge : null,
  142. /**
  143. * An instance of FLStyleSheet for the medium device preview.
  144. *
  145. * @since 1.9
  146. * @access private
  147. * @property {FLStyleSheet} _styleSheetMedium
  148. */
  149. _styleSheetMedium : null,
  150. /**
  151. * An instance of FLStyleSheet for the responsive device preview.
  152. *
  153. * @since 1.9
  154. * @access private
  155. * @property {FLStyleSheet} _styleSheet
  156. */
  157. _styleSheetResponsive : null,
  158. /**
  159. * A timeout object for delaying the current preview refresh.
  160. *
  161. * @since 1.3.3
  162. * @access private
  163. * @property {Object} _timeout
  164. */
  165. _timeout : null,
  166. /**
  167. * A timeout object for delaying when we show the loading
  168. * graphic for refresh previews.
  169. *
  170. * @since 1.10
  171. * @access private
  172. * @property {Object} _loaderTimeout
  173. */
  174. _loaderTimeout : null,
  175. /**
  176. * Stores the last classname for a classname preview.
  177. *
  178. * @since 1.3.3
  179. * @access private
  180. * @property {String} _lastClassName
  181. */
  182. _lastClassName : null,
  183. /**
  184. * A reference to the AJAX object for a preview refresh.
  185. *
  186. * @since 1.3.3
  187. * @access private
  188. * @property {Object} _xhr
  189. */
  190. _xhr : null,
  191. /**
  192. * Initializes a builder preview.
  193. *
  194. * @since 1.3.3
  195. * @access private
  196. * @method _init
  197. */
  198. _init: function()
  199. {
  200. // Node Id
  201. this.nodeId = $('.fl-builder-settings', window.parent.document).data('node');
  202. // Save settings
  203. this._saveSettings();
  204. // Elements and Class Names
  205. this._initElementsAndClasses();
  206. // Create the preview stylesheets
  207. this._createSheets();
  208. this._bindEvents()
  209. },
  210. _bindEvents() {
  211. // Responsive previews
  212. this._initResponsivePreviews();
  213. // Default field previews
  214. this._initDefaultFieldPreviews();
  215. // Init
  216. switch(this.type) {
  217. case 'row':
  218. this._initRow();
  219. break;
  220. case 'col':
  221. this._initColumn();
  222. break;
  223. case 'module':
  224. case 'form':
  225. this._initModule();
  226. break;
  227. }
  228. FLBuilder.triggerHook( 'preview-init', this );
  229. },
  230. /**
  231. * Saves the current settings to be checked to see if
  232. * anything has changed when a preview is canceled.
  233. *
  234. * @since 1.7
  235. * @access private
  236. * @method _saveSettings
  237. */
  238. _saveSettings: function()
  239. {
  240. var form = $('.fl-builder-settings-lightbox .fl-builder-settings', window.parent.document);
  241. this._savedSettings = FLBuilder._getSettingsForChangedCheck( this.nodeId, form );
  242. },
  243. /**
  244. * Checks to see if the settings have changed.
  245. *
  246. * @since 1.7
  247. * @access private
  248. * @method _settingsHaveChanged
  249. * @return bool
  250. */
  251. _settingsHaveChanged: function()
  252. {
  253. var form = $('.fl-builder-settings-lightbox .fl-builder-settings', window.parent.document),
  254. settings = FLBuilder._getSettings( form );
  255. return JSON.stringify( this._savedSettings ) != JSON.stringify( settings );
  256. },
  257. /**
  258. * Initializes the classname and element references
  259. * for this preview.
  260. *
  261. * @since 1.3.3
  262. * @access private
  263. * @method _initElementsAndClasses
  264. */
  265. _initElementsAndClasses: function()
  266. {
  267. var contentClass;
  268. // Content Class
  269. if(this.type == 'row') {
  270. contentClass = '.fl-row-content-wrap';
  271. }
  272. else {
  273. contentClass = '.fl-' + this.type + '-content';
  274. }
  275. let formSelector = '.fl-builder-settings-lightbox:visible form.fl-builder-settings'
  276. // Class Names
  277. $.extend(this.classes, {
  278. settings : formSelector,
  279. settingsHeader : formSelector + ' .fl-lightbox-header',
  280. node : FLBuilder._contentClass + ' .fl-node-' + this.nodeId,
  281. content : FLBuilder._contentClass + ' .fl-node-' + this.nodeId + ' > ' + contentClass
  282. });
  283. // Elements
  284. $.extend(this.elements, {
  285. settings : $(this.classes.settings, window.parent.document),
  286. settingsHeader : $(this.classes.settingsHeader, window.parent.document),
  287. node : $(this.classes.node),
  288. content : $(this.classes.content)
  289. });
  290. },
  291. /**
  292. * Creates the stylesheets for default, medium
  293. * and responsive previews.
  294. *
  295. * @since 1.9
  296. * @method _createSheets
  297. */
  298. _createSheets: function()
  299. {
  300. this._destroySheets();
  301. if ( ! this._styleSheet ) {
  302. this._styleSheet = new FLStyleSheet( {
  303. id : 'fl-builder-preview',
  304. className : 'fl-builder-preview-style'
  305. } );
  306. }
  307. if ( ! this._styleSheetLarge) {
  308. this._styleSheetLarge = new FLStyleSheet( {
  309. id : 'fl-builder-preview-large',
  310. className : 'fl-builder-preview-style'
  311. } );
  312. this._styleSheetLarge.disable();
  313. }
  314. if ( ! this._styleSheetMedium ) {
  315. this._styleSheetMedium = new FLStyleSheet( {
  316. id : 'fl-builder-preview-medium',
  317. className : 'fl-builder-preview-style'
  318. } );
  319. this._styleSheetMedium.disable();
  320. }
  321. if ( ! this._styleSheetResponsive ) {
  322. this._styleSheetResponsive = new FLStyleSheet( {
  323. id : 'fl-builder-preview-responsive',
  324. className : 'fl-builder-preview-style'
  325. } );
  326. this._styleSheetResponsive.disable();
  327. }
  328. },
  329. /**
  330. * Destroys all preview sheets.
  331. *
  332. * @since 1.9
  333. * @method _destroySheets
  334. */
  335. _destroySheets: function()
  336. {
  337. if ( this._styleSheet ) {
  338. this._styleSheet.destroy();
  339. this._styleSheet = null;
  340. }
  341. if ( this._styleSheetLarge ) {
  342. this._styleSheetLarge.destroy();
  343. this._styleSheetLarge = null;
  344. }
  345. if ( this._styleSheetMedium ) {
  346. this._styleSheetMedium.destroy();
  347. this._styleSheetMedium = null;
  348. }
  349. if ( this._styleSheetResponsive ) {
  350. this._styleSheetResponsive.destroy();
  351. this._styleSheetResponsive = null;
  352. }
  353. $( '.fl-builder-preview-style' ).remove();
  354. },
  355. /**
  356. * Disables preview styles for the current
  357. * responsive editing mode.
  358. *
  359. * @since 2.2
  360. * @method _disableStyles
  361. */
  362. _disableStyles: function()
  363. {
  364. var mode = FLBuilderResponsiveEditing._mode,
  365. config = FLBuilderConfig.global,
  366. node = this.elements.node;
  367. if ( 'responsive' === mode ) {
  368. FLBuilderSimulateMediaQuery.disableStyles( config.responsive_breakpoint );
  369. this._styleSheetResponsive.disable();
  370. } else if ( 'large' === mode ) {
  371. FLBuilderSimulateMediaQuery.disableStyles( config.large_breakpoint );
  372. this._styleSheetLarge.disable();
  373. } else if ( 'medium' === mode ) {
  374. FLBuilderSimulateMediaQuery.disableStyles( config.medium_breakpoint );
  375. this._styleSheetMedium.disable();
  376. } else {
  377. node.removeClass( function( i, className ) {
  378. return ( className.match( /fl-node-[^\s]*/g ) || [] ).join( ' ' );
  379. } );
  380. }
  381. },
  382. /**
  383. * Enables preview styles for the current
  384. * responsive editing mode.
  385. *
  386. * @since 2.2
  387. * @method _enableStyles
  388. */
  389. _enableStyles: function()
  390. {
  391. var mode = FLBuilderResponsiveEditing._mode,
  392. node = this.elements.node;
  393. if ( 'responsive' === mode ) {
  394. FLBuilderSimulateMediaQuery.enableStyles();
  395. this._styleSheetResponsive.enable();
  396. } else if ( 'medium' === mode ) {
  397. FLBuilderSimulateMediaQuery.enableStyles();
  398. this._styleSheetMedium.enable();
  399. } else if ( 'large' === mode ) {
  400. FLBuilderSimulateMediaQuery.enableStyles();
  401. this._styleSheetLarge.enable();
  402. } else {
  403. node.addClass( 'fl-node-' + node.data( 'node' ) );
  404. }
  405. },
  406. /**
  407. * Attempt to find the default value for a CSS property.
  408. *
  409. * @since 2.2
  410. * @method _getDefaultValue
  411. * @param {String} selector
  412. * @param {String} property
  413. * @return {String}
  414. */
  415. _getDefaultValue: function( selector, property )
  416. {
  417. var value = '',
  418. element = $( selector ),
  419. node = element.closest( '[data-node]' ),
  420. ignore = [ 'line-height', 'font-weight' ];
  421. if ( 'width' === property ) {
  422. value = 'auto';
  423. } else if ( -1 === $.inArray( property, ignore ) && node.length ) {
  424. this._disableStyles();
  425. value = element.css( property );
  426. this._enableStyles();
  427. }
  428. return value;
  429. },
  430. /**
  431. * Updates a CSS rule for this preview.
  432. *
  433. * @since 1.3.3
  434. * @method updateCSSRule
  435. * @param {String} selector The CSS selector to update.
  436. * @param {String} property The CSS property to update.
  437. * @param {String} value The CSS value to update.
  438. * @param {String|Boolean} responsive If this preview is responsive or not.
  439. */
  440. updateCSSRule: function( selector, property, value, responsive )
  441. {
  442. var mode = FLBuilderResponsiveEditing._mode,
  443. sheetKey = '';
  444. // Get the default value if needed.
  445. if ( '' === value || 'null' === value ) {
  446. value = this._getDefaultValue( selector, property );
  447. }
  448. // Update the rule.
  449. if ( responsive ) {
  450. if ( 'string' === typeof responsive ) {
  451. sheetKey = this.toUpperCaseWords( responsive );
  452. } else {
  453. sheetKey = 'default' === mode ? '' : this.toUpperCaseWords( mode );
  454. }
  455. this[ '_styleSheet' + sheetKey ].updateRule( selector, property, value );
  456. } else {
  457. this._styleSheet.updateRule( selector, property, value );
  458. }
  459. },
  460. /**
  461. * Runs a delay with a callback.
  462. *
  463. * @since 1.3.3
  464. * @method delay
  465. * @param {Number} length How long to wait before running the callback.
  466. * @param {Function} callback A function to call when the delay is complete.
  467. */
  468. delay: function(length, callback)
  469. {
  470. this._cancelDelay();
  471. this._timeout = setTimeout(callback, length);
  472. },
  473. /**
  474. * Cancels a preview refresh delay.
  475. *
  476. * @since 1.3.3
  477. * @access private
  478. * @method _cancelDelay
  479. */
  480. _cancelDelay: function()
  481. {
  482. if(this._timeout !== null) {
  483. clearTimeout(this._timeout);
  484. }
  485. if(this._loaderTimeout !== null) {
  486. clearTimeout(this._loaderTimeout);
  487. }
  488. },
  489. /**
  490. * Converts a hex value to an array of RGB values.
  491. *
  492. * @since 1.3.3
  493. * @method hexToRgb
  494. * @param {String} hex
  495. * @return {Array}
  496. */
  497. hexToRgb: function(hex)
  498. {
  499. var bigInt = parseInt(hex, 16),
  500. r = (bigInt >> 16) & 255,
  501. g = (bigInt >> 8) & 255,
  502. b = bigInt & 255;
  503. return [r, g, b];
  504. },
  505. /**
  506. * Returns a hex or rgb formatted value.
  507. *
  508. * @since 2.0.3
  509. * @method hexOrRgb
  510. * @param {String} value
  511. * @return {String}
  512. */
  513. hexOrRgb: function( value )
  514. {
  515. if ( value.indexOf( 'rgb' ) < 0 && value.indexOf( '#' ) < 0 ) {
  516. value = '#' + value;
  517. }
  518. return value;
  519. },
  520. /**
  521. * Parses a float or returns 0 if we don't have a number.
  522. *
  523. * @since 1.3.3
  524. * @method parseFloat
  525. * @param {Number} value
  526. * @return {Number}
  527. */
  528. parseFloat: function(value)
  529. {
  530. return isNaN(parseFloat(value)) ? 0 : parseFloat(value);
  531. },
  532. /* Responsive Previews
  533. ----------------------------------------------------------*/
  534. /**
  535. * Initializes logic for responsive previews.
  536. *
  537. * @since 1.9
  538. * @method _initResponsivePreviews
  539. */
  540. _initResponsivePreviews: function()
  541. {
  542. var namespace = '.preview-' + this.id;
  543. FLBuilder.addHook( 'responsive-editing-switched' + namespace, $.proxy( this._responsiveEditingSwitched, this ) );
  544. FLBuilder.addHook( 'responsive-editing-before-preview-fields' + namespace, $.proxy( this._responsiveEditingPreviewFields, this ) );
  545. },
  546. /**
  547. * Destroys responsive preview events.
  548. *
  549. * @since 1.9
  550. * @method _destroyResponsivePreviews
  551. */
  552. _destroyResponsivePreviews: function()
  553. {
  554. var namespace = '.preview-' + this.id;
  555. FLBuilder.removeHook( 'responsive-editing-switched' + namespace );
  556. FLBuilder.removeHook( 'responsive-editing-before-preview-fields' + namespace );
  557. },
  558. /**
  559. * Initializes logic for responsive previews.
  560. *
  561. * @since 1.9
  562. * @method _responsiveEditingSwitched
  563. */
  564. _responsiveEditingSwitched: function( e, mode )
  565. {
  566. if ( 'default' == mode ) {
  567. this._styleSheetLarge.disable();
  568. this._styleSheetMedium.disable();
  569. this._styleSheetResponsive.disable();
  570. }
  571. else if ( 'large' == mode ) {
  572. this._styleSheetLarge.enable();
  573. this._styleSheetMedium.disable();
  574. this._styleSheetResponsive.disable();
  575. }
  576. else if ( 'medium' == mode ) {
  577. this._styleSheetLarge.enable();
  578. this._styleSheetMedium.enable();
  579. this._styleSheetResponsive.disable();
  580. }
  581. else if ( 'responsive' == mode ) {
  582. this._styleSheetLarge.enable();
  583. this._styleSheetMedium.enable();
  584. this._styleSheetResponsive.enable();
  585. }
  586. },
  587. /**
  588. * Logic that needs to run before field previews are triggered
  589. * after responsive editing mode switches.
  590. *
  591. * @since 2.2
  592. * @method _responsiveEditingPreviewFields
  593. */
  594. _responsiveEditingPreviewFields: function( e, mode )
  595. {
  596. if ( 'large' === mode || 'medium' === mode ) {
  597. if ( 'col' === this.type && this.elements.node[0].style.width ) {
  598. size = parseFloat( this.elements.node[0].style.width );
  599. this.elements.size.val( size );
  600. }
  601. }
  602. },
  603. /**
  604. * Deprecated. Use updateCSSRule instead.
  605. *
  606. * @since 1.9
  607. */
  608. updateResponsiveCSSRule: function( selector, property, value )
  609. {
  610. this.updateCSSRule( selector, property, value, true );
  611. },
  612. /* States
  613. ----------------------------------------------------------*/
  614. /**
  615. * Saves the current state of a layout.
  616. *
  617. * @since 1.3.3
  618. * @access private
  619. * @method _saveState
  620. */
  621. _saveState: function()
  622. {
  623. var post = FLBuilderConfig.postId,
  624. css = $('link[href*="/cache/' + post + '"]').attr('href'),
  625. js = $('script[src*="/cache/' + post + '"]').attr('src'),
  626. html = $(FLBuilder._contentClass).html();
  627. this.state = {
  628. css : css,
  629. js : js,
  630. html : html
  631. };
  632. },
  633. /**
  634. * Runs a preview refresh for the current settings lightbox.
  635. *
  636. * @since 1.3.3
  637. * @method preview
  638. */
  639. preview: function()
  640. {
  641. var form = $('.fl-builder-settings-lightbox .fl-builder-settings', window.parent.document),
  642. nodeId = form.attr('data-node'),
  643. settings = FLBuilder._getSettings(form);
  644. // Show the node as loading.
  645. FLBuilder._showNodeLoading( nodeId );
  646. // Abort an existing preview request.
  647. this._cancelPreview();
  648. settings = FLBuilder._inputVarsCheck( settings );
  649. if ( 'error' === settings ) {
  650. return 0;
  651. }
  652. // Make a new preview request.
  653. this._xhr = FLBuilder.ajax({
  654. action : 'render_layout',
  655. node_id : nodeId,
  656. node_preview : settings
  657. }, $.proxy(this._renderPreview, this));
  658. },
  659. /**
  660. * Runs a preview refresh with a delay.
  661. *
  662. * @since 1.3.3
  663. * @method delayPreview
  664. */
  665. delayPreview: function(e)
  666. {
  667. var heading = typeof e == 'undefined' ? [] : $(e.target).closest('tr').find('th'),
  668. widgetHeading = $('.fl-builder-widget-settings .fl-builder-settings-title', window.parent.document),
  669. lightboxHeading = $('.fl-builder-settings .fl-lightbox-header', window.parent.document),
  670. loaderSrc = FLBuilderLayoutConfig.paths.pluginUrl + 'img/ajax-loader-small.svg',
  671. loader = $('<img class="fl-builder-preview-loader" src="' + loaderSrc + '" />');
  672. this.delay(1000, $.proxy(this.preview, this));
  673. this._loaderTimeout = setTimeout( function() {
  674. $('.fl-builder-preview-loader', window.parent.document).remove();
  675. if(heading.length > 0) {
  676. heading.append(loader);
  677. }
  678. else if(widgetHeading.length > 0) {
  679. widgetHeading.append(loader);
  680. }
  681. else if(lightboxHeading.length > 0) {
  682. lightboxHeading.append(loader);
  683. }
  684. }, 1500 );
  685. },
  686. /**
  687. * Cancels a preview refresh.
  688. *
  689. * @since 1.3.3
  690. * @access private
  691. * @method _cancelPreview
  692. */
  693. _cancelPreview: function()
  694. {
  695. if(this._xhr) {
  696. this._xhr.abort();
  697. this._xhr = null;
  698. }
  699. },
  700. /**
  701. * Renders the response of a preview refresh.
  702. *
  703. * @since 1.3.3
  704. * @access private
  705. * @method _renderPreview
  706. * @param {String} response The JSON encoded response.
  707. */
  708. _renderPreview: function(response)
  709. {
  710. this._xhr = null;
  711. FLBuilder._renderLayout(response, $.proxy(this._renderPreviewComplete, this));
  712. },
  713. /**
  714. * Fires when a preview refresh has finished rendering.
  715. *
  716. * @since 1.3.3
  717. * @access private
  718. * @method _renderPreviewComplete
  719. */
  720. _renderPreviewComplete: function()
  721. {
  722. // Refresh the preview styles.
  723. this._createSheets();
  724. // Refresh the elements.
  725. this._initElementsAndClasses();
  726. // Refresh preview config for element references.
  727. this._initDefaultFieldPreviews();
  728. // Clear the loader timeout.
  729. if(this._loaderTimeout !== null) {
  730. clearTimeout(this._loaderTimeout);
  731. }
  732. /**
  733. * Remove the loading graphic
  734. * 2.6.0.2 added 500ms delay to fix #2234
  735. */
  736. setTimeout( function() {
  737. $('.fl-builder-preview-loader', window.parent.document).remove();
  738. }, 500 );
  739. // Fire the preview rendered event.
  740. $( FLBuilder._contentClass ).trigger( 'fl-builder.preview-rendered' );
  741. },
  742. /**
  743. * Reverts a preview to the state that was saved
  744. * before the preview was initialized.
  745. *
  746. * @since 1.3.3
  747. * @method revert
  748. */
  749. revert: function()
  750. {
  751. var nodeId = this.nodeId;
  752. if ( ! this._settingsHaveChanged() ) {
  753. this.clear();
  754. return;
  755. }
  756. if ( 'col' === this.type ) {
  757. nodeId = this.elements.node.closest( '.fl-col-group' ).data( 'node' );
  758. }
  759. FLBuilder._updateNode( nodeId, function() {
  760. this.clear();
  761. }.bind( this ) );
  762. },
  763. /**
  764. * Cancels a preview refresh.
  765. *
  766. * @since 1.3.3
  767. * @method clear
  768. */
  769. cancel: function()
  770. {
  771. this._cancelDelay();
  772. this._cancelPreview();
  773. },
  774. /**
  775. * Cancels a preview refresh and removes
  776. * any stylesheet changes.
  777. *
  778. * @since 1.3.3
  779. * @method clear
  780. */
  781. clear: function()
  782. {
  783. // Canel any preview delays or requests.
  784. this.cancel();
  785. // Destroy the preview stylesheet.
  786. this._destroySheets();
  787. // Destroy responsive editing previews.
  788. this._destroyResponsivePreviews();
  789. },
  790. /* Node Text Color Settings
  791. ----------------------------------------------------------*/
  792. /**
  793. * Initializes node text color previews.
  794. *
  795. * @since 1.3.3
  796. * @access private
  797. * @method _initNodeTextColor
  798. */
  799. _initNodeTextColor: function()
  800. {
  801. // Elements
  802. $.extend(this.elements, {
  803. textColor : $(this.classes.settings + ' input[name=text_color]', window.parent.document),
  804. linkColor : $(this.classes.settings + ' input[name=link_color]', window.parent.document),
  805. hoverColor : $(this.classes.settings + ' input[name=hover_color]', window.parent.document),
  806. headingColor : $(this.classes.settings + ' input[name=heading_color]', window.parent.document)
  807. });
  808. // Events
  809. this.elements.textColor.on('change', $.proxy(this._textColorChange, this));
  810. this.elements.linkColor.on('change', $.proxy(this._textColorChange, this));
  811. this.elements.hoverColor.on('change', $.proxy(this._textColorChange, this));
  812. this.elements.headingColor.on('change', $.proxy(this._textColorChange, this));
  813. },
  814. /**
  815. * Fires when the text color field for a node
  816. * is changed.
  817. *
  818. * @since 1.3.3
  819. * @access private
  820. * @method _textColorChange
  821. * @param {Object} e An event object.
  822. */
  823. _textColorChange: function(e)
  824. {
  825. var textColor = this.elements.textColor.val(),
  826. linkColor = this.elements.linkColor.val(),
  827. hoverColor = this.elements.hoverColor.val(),
  828. headingColor = this.elements.headingColor.val();
  829. linkColor = linkColor === '' ? textColor : linkColor;
  830. hoverColor = hoverColor === '' ? textColor : hoverColor;
  831. headingColor = headingColor === '' ? textColor : headingColor;
  832. if ( textColor && textColor.indexOf( 'rgb' ) < 0 ) {
  833. textColor = '#' + textColor;
  834. }
  835. if ( linkColor && linkColor.indexOf( 'rgb' ) < 0 ) {
  836. linkColor = '#' + linkColor;
  837. }
  838. if ( hoverColor && hoverColor.indexOf( 'rgb' ) < 0 ) {
  839. hoverColor = '#' + hoverColor;
  840. }
  841. if ( headingColor && headingColor.indexOf( 'rgb' ) < 0 ) {
  842. headingColor = '#' + headingColor;
  843. }
  844. this.delay(50, $.proxy(function(){
  845. // Update Text color.
  846. if(textColor === '') {
  847. this.updateCSSRule(this.classes.node, 'color', '');
  848. }
  849. else {
  850. this.updateCSSRule(this.classes.node, 'color', textColor);
  851. }
  852. // Update Link Color
  853. if ( linkColor === '' ) {
  854. this.updateCSSRule(this.classes.node + ' a', 'color', '');
  855. }
  856. else {
  857. this.updateCSSRule(this.classes.node + ' a', 'color', linkColor);
  858. }
  859. // Hover Color
  860. if(hoverColor === '') {
  861. this.updateCSSRule(this.classes.node + ' a:hover', 'color', '');
  862. }
  863. else {
  864. this.updateCSSRule(this.classes.node + ' a:hover', 'color', hoverColor);
  865. }
  866. // Heading Color
  867. if(headingColor === '') {
  868. this.updateCSSRule(this.classes.node + ' h1', 'color', '');
  869. this.updateCSSRule(this.classes.node + ' h2', 'color', '');
  870. this.updateCSSRule(this.classes.node + ' h3', 'color', '');
  871. this.updateCSSRule(this.classes.node + ' h4', 'color', '');
  872. this.updateCSSRule(this.classes.node + ' h5', 'color', '');
  873. this.updateCSSRule(this.classes.node + ' h6', 'color', '');
  874. this.updateCSSRule(this.classes.node + ' h1 a', 'color', '');
  875. this.updateCSSRule(this.classes.node + ' h2 a', 'color', '');
  876. this.updateCSSRule(this.classes.node + ' h3 a', 'color', '');
  877. this.updateCSSRule(this.classes.node + ' h4 a', 'color', '');
  878. this.updateCSSRule(this.classes.node + ' h5 a', 'color', '');
  879. this.updateCSSRule(this.classes.node + ' h6 a', 'color', '');
  880. }
  881. else {
  882. this.updateCSSRule(this.classes.node + ' h1', 'color', headingColor);
  883. this.updateCSSRule(this.classes.node + ' h2', 'color', headingColor);
  884. this.updateCSSRule(this.classes.node + ' h3', 'color', headingColor);
  885. this.updateCSSRule(this.classes.node + ' h4', 'color', headingColor);
  886. this.updateCSSRule(this.classes.node + ' h5', 'color', headingColor);
  887. this.updateCSSRule(this.classes.node + ' h6', 'color', headingColor);
  888. this.updateCSSRule(this.classes.node + ' h1 a', 'color', headingColor);
  889. this.updateCSSRule(this.classes.node + ' h2 a', 'color', headingColor);
  890. this.updateCSSRule(this.classes.node + ' h3 a', 'color', headingColor);
  891. this.updateCSSRule(this.classes.node + ' h4 a', 'color', headingColor);
  892. this.updateCSSRule(this.classes.node + ' h5 a', 'color', headingColor);
  893. this.updateCSSRule(this.classes.node + ' h6 a', 'color', headingColor);
  894. }
  895. }, this));
  896. },
  897. /* Node Bg Settings
  898. ----------------------------------------------------------*/
  899. /**
  900. * Initializes node background previews.
  901. *
  902. * @since 1.3.3
  903. * @access private
  904. * @method _initNodeBg
  905. */
  906. _initNodeBg: function()
  907. {
  908. // Elements
  909. $.extend(this.elements, {
  910. bgType : $(this.classes.settings + ' select[name=bg_type]', window.parent.document),
  911. bgColor : $(this.classes.settings + ' input[name=bg_color]', window.parent.document),
  912. bgColorPicker : $(this.classes.settings + ' .fl-picker-bg_color', window.parent.document),
  913. bgGradientType : $(this.classes.settings + ' select.fl-gradient-picker-type-select', window.parent.document),
  914. bgVideoSource : $(this.classes.settings + ' select[name=bg_video_source]', window.parent.document),
  915. bgVideo : $(this.classes.settings + ' input[name=bg_video]', window.parent.document),
  916. bgVideoServiceUrl : $(this.classes.settings + ' input[name=bg_video_service_url]', window.parent.document),
  917. bgVideoFallbackSrc : $(this.classes.settings + ' select[name=bg_video_fallback_src]', window.parent.document),
  918. bgSlideshowSource : $(this.classes.settings + ' select[name=ss_source]', window.parent.document),
  919. bgSlideshowPhotos : $(this.classes.settings + ' input[name=ss_photos]', window.parent.document),
  920. bgSlideshowFeedUrl : $(this.classes.settings + ' input[name=ss_feed_url]', window.parent.document),
  921. bgSlideshowSpeed : $(this.classes.settings + ' input[name=ss_speed]', window.parent.document),
  922. bgSlideshowTrans : $(this.classes.settings + ' select[name=ss_transition]', window.parent.document),
  923. bgSlideshowTransSpeed : $(this.classes.settings + ' input[name=ss_transitionDuration]', window.parent.document),
  924. bgParallaxImageSrc : $(this.classes.settings + ' select[name=bg_parallax_image_src]', window.parent.document),
  925. bgOverlayType : $(this.classes.settings + ' select[name=bg_overlay_type]', window.parent.document),
  926. bgOverlayColor : $(this.classes.settings + ' input[name=bg_overlay_color]', window.parent.document),
  927. bgOverlayGradient : $(this.classes.settings + ' #fl-field-bg_overlay_gradient select', window.parent.document),
  928. });
  929. // Events
  930. this.elements.bgType.on( 'change', $.proxy(this._bgTypeChange, this));
  931. this.elements.bgColor.on( 'change', $.proxy(this._bgColorChange, this));
  932. this.elements.bgVideoServiceUrl.on( 'change', $.proxy(this._bgVideoChange, this));
  933. this.elements.bgSlideshowSource.on( 'change', $.proxy(this._bgSlideshowChange, this));
  934. this.elements.bgSlideshowPhotos.on( 'change', $.proxy(this._bgSlideshowChange, this));
  935. this.elements.bgSlideshowFeedUrl.on( 'keyup', $.proxy(this._bgSlideshowChange, this));
  936. this.elements.bgSlideshowSpeed.on( 'keyup', $.proxy(this._bgSlideshowChange, this));
  937. this.elements.bgSlideshowTrans.on( 'change', $.proxy(this._bgSlideshowChange, this));
  938. this.elements.bgSlideshowTransSpeed.on( 'keyup', $.proxy(this._bgSlideshowChange, this));
  939. this.elements.bgParallaxImageSrc.on( 'change', $.proxy(this._bgParallaxChange, this));
  940. this.elements.bgOverlayType.on( 'change', $.proxy(this._bgOverlayChange, this));
  941. this.elements.bgOverlayColor.on( 'change', $.proxy(this._bgOverlayChange, this));
  942. },
  943. /**
  944. * Fires when the background type field of
  945. * a node changes.
  946. *
  947. * @since 1.3.3
  948. * @access private
  949. * @method _bgTypeChange
  950. * @param {Object} e An event object.
  951. */
  952. _bgTypeChange: function(e)
  953. {
  954. var val = this.elements.bgType.val(),
  955. mode = FLBuilderResponsiveEditing._mode;
  956. // Clear bg styles first.
  957. this.elements.node.removeClass('fl-row-bg-video');
  958. this.elements.node.removeClass('fl-row-bg-slideshow');
  959. this.elements.node.removeClass('fl-row-bg-parallax');
  960. this.elements.node.find('.fl-bg-video').remove();
  961. this.elements.node.find('.fl-bg-slideshow').remove();
  962. this.elements.content.css('background-image', '');
  963. this.updateCSSRule(this.classes.content, 'background-color', 'transparent');
  964. this.updateCSSRule(this.classes.content, 'background-image', 'none');
  965. this.updateCSSRule(this.classes.content, 'background-image', 'none', 'medium');
  966. this.updateCSSRule(this.classes.content, 'background-image', 'none', 'responsive');
  967. // None
  968. if(val == 'none') {
  969. this._bgOverlayClear();
  970. }
  971. // Color
  972. else if(val == 'color') {
  973. this.elements.bgColor.trigger('change');
  974. this._bgOverlayClear();
  975. }
  976. // Gradient
  977. else if(val == 'gradient') {
  978. this.elements.bgGradientType.trigger('change');
  979. this._bgOverlayClear();
  980. }
  981. // Photo
  982. else if(val == 'photo') {
  983. this.elements.bgColor.trigger('change');
  984. this.elements.settings.find( '[data-device="' + mode + '"] select[name*="bg_"]' ).trigger( 'change' );
  985. }
  986. // Video
  987. else if(val == 'video') {
  988. this.elements.bgColor.trigger('change');
  989. this._bgVideoChange();
  990. }
  991. // Slideshow
  992. else if(val == 'slideshow') {
  993. this.elements.bgColor.trigger('change');
  994. this._bgSlideshowChange();
  995. }
  996. // Parallax
  997. else if(val == 'parallax') {
  998. this.elements.bgColor.trigger('change');
  999. this.elements.bgParallaxImageSrc.trigger('change');
  1000. }
  1001. },
  1002. /**
  1003. * Fires when the background color field of
  1004. * a node changes.
  1005. *
  1006. * @since 1.3.3
  1007. * @access private
  1008. * @method _bgColorChange
  1009. * @param {Object} e An event object.
  1010. */
  1011. _bgColorChange: function(e)
  1012. {
  1013. var rgb, alpha, value;
  1014. if(this.elements.bgColor.val() === '') {
  1015. this.updateCSSRule(this.classes.content, 'background-color', 'transparent');
  1016. }
  1017. else {
  1018. value = this.hexOrRgb( this.elements.bgColor.val() );
  1019. this.delay(100, $.proxy(function(){
  1020. this.updateCSSRule(this.classes.content, 'background-color', value);
  1021. }, this));
  1022. }
  1023. },
  1024. /**
  1025. * Fires when the background video field of
  1026. * a node changes.
  1027. *
  1028. * @since 1.9.2
  1029. * @access private
  1030. * @method _bgVideoChange
  1031. * @param {Object} e An event object.
  1032. */
  1033. _bgVideoChange: function(e)
  1034. {
  1035. var eles = this.elements,
  1036. source = eles.bgVideoSource.val(),
  1037. video = eles.bgVideo.val(),
  1038. videoUrl = eles.bgVideoServiceUrl.val(),
  1039. youtubePlayer = 'https://www.youtube.com/iframe_api',
  1040. vimeoPlayer = 'https://player.vimeo.com/api/player.js',
  1041. scriptTag = $( '<script>' );
  1042. // Only load the required API script library
  1043. if(source == 'video_service' && videoUrl != '') {
  1044. if (/^(?:(?:(?:https?:)?\/\/)?(?:www.)?(?:youtu(?:be.com|.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/i.test(videoUrl)
  1045. && $( 'script[src*="youtube.com"' ).length < 1) {
  1046. scriptTag.attr('src', youtubePlayer);
  1047. }
  1048. else if(/^(http\:\/\/|https\:\/\/)?(www\.)?(vimeo\.com\/)([0-9]+)$/.test(videoUrl)
  1049. && $( 'script[src*="vimeo.com"' ).length < 1) {
  1050. scriptTag.attr('src', vimeoPlayer);
  1051. }
  1052. scriptTag
  1053. .attr('type', 'text/javascript')
  1054. .appendTo('head');
  1055. this.delay(500, $.proxy(this.preview, this));
  1056. }
  1057. else if(video != '') {
  1058. this.preview();
  1059. }
  1060. },
  1061. /**
  1062. * Fires when the background slideshow field of
  1063. * a node changes.
  1064. *
  1065. * @since 1.3.3
  1066. * @access private
  1067. * @method _bgSlideshowChange
  1068. * @param {Object} e An event object.
  1069. */
  1070. _bgSlideshowChange: function(e)
  1071. {
  1072. var eles = this.elements,
  1073. source = eles.bgSlideshowSource.val(),
  1074. photos = eles.bgSlideshowPhotos.val(),
  1075. feed = eles.bgSlideshowFeedUrl.val(),
  1076. speed = eles.bgSlideshowSpeed.val(),
  1077. transSpeed = eles.bgSlideshowTransSpeed.val();
  1078. if(source == 'wordpress' && photos === '') {
  1079. return;
  1080. }
  1081. else if(source == 'smugmug' && feed === '') {
  1082. return;
  1083. }
  1084. else if(isNaN(parseInt(speed))) {
  1085. return;
  1086. }
  1087. else if(isNaN(parseInt(transSpeed))) {
  1088. return;
  1089. }
  1090. this.delay(500, $.proxy(this.preview, this));
  1091. },
  1092. /**
  1093. * Fires when the background parallax field of
  1094. * a node changes.
  1095. *
  1096. * @since 1.3.3
  1097. * @access private
  1098. * @method _bgParallaxChange
  1099. * @param {Object} e An event object.
  1100. */
  1101. _bgParallaxChange: function(e)
  1102. {
  1103. if(this.elements.bgParallaxImageSrc.val()) {
  1104. this.updateCSSRule(this.classes.content, {
  1105. 'background-image' : 'url(' + this.elements.bgParallaxImageSrc.val() + ')',
  1106. 'background-repeat' : 'no-repeat',
  1107. 'background-position' : 'center center',
  1108. 'background-attachment' : 'fixed',
  1109. 'background-size' : 'cover'
  1110. });
  1111. }
  1112. },
  1113. /**
  1114. * Fires when the background overlay field of
  1115. * a node changes.
  1116. *
  1117. * @since 1.3.3
  1118. * @access private
  1119. * @method _bgOverlayChange
  1120. * @param {Object} e An event object.
  1121. */
  1122. _bgOverlayChange: function(e)
  1123. {
  1124. var type = this.elements.bgOverlayType.val(),
  1125. color = this.elements.bgOverlayColor.val(),
  1126. rgb, alpha, value;
  1127. if ( 'color' === type ) {
  1128. if ( color === '' ) {
  1129. this.elements.node.removeClass('fl-row-bg-overlay');
  1130. this.elements.node.removeClass('fl-col-bg-overlay');
  1131. this.updateCSSRule(this.classes.content + '::after', 'background-color', 'transparent');
  1132. } else {
  1133. value = this.hexOrRgb( this.elements.bgOverlayColor.val() );
  1134. this.delay(100, $.proxy(function(){
  1135. this._bgOverlayAddClasses();
  1136. this.updateCSSRule( this.classes.content + '::after', 'background-color', value );
  1137. }, this));
  1138. }
  1139. this.updateCSSRule(this.classes.content + '::after', 'background-image', 'none');
  1140. } else if ( 'gradient' === type ) {
  1141. this._bgOverlayAddClasses();
  1142. this.updateCSSRule(this.classes.content + '::after', 'background-color', 'transparent');
  1143. this.elements.bgOverlayGradient.trigger( 'change' );
  1144. } else {
  1145. this.elements.node.removeClass('fl-row-bg-overlay');
  1146. this.elements.node.removeClass('fl-col-bg-overlay');
  1147. this.updateCSSRule(this.classes.content + '::after', 'background-color', 'transparent');
  1148. this.updateCSSRule(this.classes.content + '::after', 'background-image', 'none');
  1149. }
  1150. },
  1151. /**
  1152. * Adds the necessary classes for background overlays.
  1153. *
  1154. * @since 2.2
  1155. * @access private
  1156. * @method _bgOverlayAddClasses
  1157. */
  1158. _bgOverlayAddClasses: function() {
  1159. if ( this.elements.node.hasClass( 'fl-col' ) ) {
  1160. this.elements.node.addClass( 'fl-col-bg-overlay' );
  1161. } else {
  1162. this.elements.node.addClass( 'fl-row-bg-overlay' );
  1163. }
  1164. },
  1165. /**
  1166. * Fires when a background overlay color is cleared.
  1167. *
  1168. * @since 1.3.3
  1169. * @access private
  1170. * @method _bgOverlayClear
  1171. * @param {Object} e An event object.
  1172. */
  1173. _bgOverlayClear: function(e)
  1174. {
  1175. this.elements.bgOverlayColor.prev('.fl-color-picker-clear').trigger('click');
  1176. this.elements.bgOverlayType.val( 'color' ).trigger( 'change' );
  1177. },
  1178. /* Node Class Name Settings
  1179. ----------------------------------------------------------*/
  1180. /**
  1181. * Initializes node classname previews.
  1182. *
  1183. * @since 1.3.3
  1184. * @access private
  1185. * @method _initNodeClassName
  1186. */
  1187. _initNodeClassName: function()
  1188. {
  1189. // Elements
  1190. $.extend(this.elements, {
  1191. className : $(this.classes.settings + ' input[name=class]', window.parent.document)
  1192. });
  1193. // Events
  1194. this.elements.className.on('keyup', $.proxy(this._classNameChange, this));
  1195. this._lastClassName = this.elements.className.val();
  1196. },
  1197. /**
  1198. * Fires when the classname of a node changes.
  1199. *
  1200. * @since 1.3.3
  1201. * @access private
  1202. * @method _classNameChange
  1203. * @param {Object} e An event object.
  1204. */
  1205. _classNameChange: function(e)
  1206. {
  1207. var className = this.elements.className.val();
  1208. if(this._lastClassName !== null) {
  1209. this.elements.node.removeClass(this._lastClassName);
  1210. }
  1211. this.elements.node.addClass(className);
  1212. this._lastClassName = className;
  1213. },
  1214. /* Node Spacing Settings
  1215. ----------------------------------------------------------*/
  1216. /**
  1217. * Initializes node responsive dimension previews for things
  1218. * like margins, padding and borders.
  1219. *
  1220. * @since 1.9
  1221. * @access private
  1222. * @method _initNodeDimensions
  1223. */
  1224. _initNodeDimensions: function( property )
  1225. {
  1226. var elements = {},
  1227. dimensions = [ 'Top', 'Bottom', 'Left', 'Right' ],
  1228. devices = [ '', 'Large', 'Medium', 'Responsive' ],
  1229. settingsClass = this.classes.settings,
  1230. elementKey = '',
  1231. inputName = '',
  1232. i = null,
  1233. k = null;
  1234. for ( i = 0; i < dimensions.length; i++ ) {
  1235. for ( k = 0; k < devices.length; k++ ) {
  1236. elementKey = property + dimensions[ i ] + devices[ k ];
  1237. inputName = property + '_' + dimensions[ i ].toLowerCase();
  1238. if ( '' != devices[ k ] ) {
  1239. inputName += '_' + devices[ k ].toLowerCase();
  1240. }
  1241. elements[ elementKey ] = $( settingsClass + ' input[name=' + inputName + ']', window.parent.document );
  1242. }
  1243. }
  1244. $.extend( this.elements, elements );
  1245. },
  1246. /* Row Settings
  1247. ----------------------------------------------------------*/
  1248. /**
  1249. * Initializes a row preview.
  1250. *
  1251. * @since 1.3.3
  1252. * @access private
  1253. * @method _initRow
  1254. */
  1255. _initRow: function()
  1256. {
  1257. // Elements
  1258. $.extend(this.elements, {
  1259. width : $(this.classes.settings + ' select[name=width]', window.parent.document),
  1260. contentWidth : $(this.classes.settings + ' select[name=content_width]', window.parent.document),
  1261. maxContentWidth : $(this.classes.settings + ' input[name=max_content_width]', window.parent.document),
  1262. maxContentWidthUnit : $(this.classes.settings + ' select[name=max_content_width_unit]', window.parent.document),
  1263. height : $(this.classes.settings + ' select[name=full_height]', window.parent.document),
  1264. minHeight : $(this.classes.settings + ' input[name=min_height]', window.parent.document),
  1265. align : $(this.classes.settings + ' select[name=content_alignment]', window.parent.document)
  1266. });
  1267. // Events
  1268. this.elements.width.on( 'change', $.proxy(this._rowWidthChange, this));
  1269. this.elements.contentWidth.on( 'change', $.proxy(this._rowContentWidthChange, this));
  1270. this.elements.maxContentWidth.on( 'input', $.proxy(this._rowMaxContentWidthChange, this));
  1271. this.elements.maxContentWidthUnit.on( 'change', $.proxy(this._rowMaxContentWidthChange, this));
  1272. this.elements.height.on( 'change', $.proxy(this._rowHeightChange, this));
  1273. this.elements.align.on( 'change', $.proxy(this._rowHeightChange, this));
  1274. // Common Elements
  1275. this._initNodeTextColor();
  1276. this._initNodeBg();
  1277. this._initNodeClassName();
  1278. this._initNodeDimensions( 'border' );
  1279. this._initNodeDimensions( 'margin' );
  1280. this._initNodeDimensions( 'padding' );
  1281. },
  1282. /**
  1283. * Fires when the width field of a row changes.
  1284. *
  1285. * @since 1.3.3
  1286. * @access private
  1287. * @method _rowWidthChange
  1288. * @param {Object} e An event object.
  1289. */
  1290. _rowWidthChange: function(e)
  1291. {
  1292. var settings = FLBuilderConfig.global,
  1293. row = this.elements.node,
  1294. content = this.elements.content.find('.fl-row-content'),
  1295. maxWidth = this.elements.maxContentWidth.val(),
  1296. maxWidthUnit = this.elements.maxContentWidthUnit.val();
  1297. row.css( 'max-width', 'none' );
  1298. content.css( 'max-width', 'none' );
  1299. if(this.elements.width.val() == 'full') {
  1300. row.removeClass('fl-row-fixed-width');
  1301. row.addClass('fl-row-full-width');
  1302. }
  1303. else {
  1304. row.removeClass('fl-row-full-width');
  1305. row.addClass('fl-row-fixed-width');
  1306. }
  1307. this._rowMaxContentWidthChange();
  1308. },
  1309. /**
  1310. * Fires when the content width field of a row changes.
  1311. *
  1312. * @since 1.3.3
  1313. * @access private
  1314. * @method _rowContentWidthChange
  1315. * @param {Object} e An event object.
  1316. */
  1317. _rowContentWidthChange: function(e)
  1318. {
  1319. var settings = FLBuilderConfig.global,
  1320. row = this.elements.node,
  1321. content = this.elements.content.find('.fl-row-content'),
  1322. maxWidth = this.elements.maxContentWidth.val(),
  1323. maxWidthUnit = this.elements.maxContentWidthUnit.val();
  1324. row.css( 'max-width', 'none' );
  1325. content.css( 'max-width', 'none' );
  1326. if(this.elements.contentWidth.val() == 'full') {
  1327. content.removeClass('fl-row-fixed-width');
  1328. content.addClass('fl-row-full-width');
  1329. }
  1330. else {
  1331. content.removeClass('fl-row-full-width');
  1332. content.addClass('fl-row-fixed-width');
  1333. this._rowMaxContentWidthChange();
  1334. }
  1335. },
  1336. /**
  1337. * Fires when the content width field of a row changes.
  1338. *
  1339. * @since 1.3.3
  1340. * @access private
  1341. * @method _rowContentWidthChange
  1342. * @param {Object} e An event object.
  1343. */
  1344. _rowMaxContentWidthChange: function(e)
  1345. {
  1346. var settings = FLBuilderConfig.global,
  1347. row = this.elements.node,
  1348. content = this.elements.content.find('.fl-row-content'),
  1349. width = this.elements.maxContentWidth.val(),
  1350. unit = this.elements.maxContentWidthUnit.val();
  1351. if ( '' == width ) {
  1352. width = settings.row_width + settings.row_width_unit;
  1353. } else {
  1354. width += unit;
  1355. }
  1356. if ( 'fixed' === this.elements.width.val() ) {
  1357. row.css( 'max-width', width );
  1358. }
  1359. content.css( 'max-width', width );
  1360. },
  1361. /**
  1362. * Fires when the height field of a row changes.
  1363. *
  1364. * @since 1.6.3
  1365. * @access private
  1366. * @method _rowHeightChange
  1367. * @param {Object} e An event object.
  1368. */
  1369. _rowHeightChange: function(e)
  1370. {
  1371. var row = this.elements.node;
  1372. row.removeClass('fl-row-align-top');
  1373. row.removeClass('fl-row-align-center');
  1374. row.removeClass('fl-row-align-bottom');
  1375. row.removeClass('fl-row-default-height');
  1376. row.removeClass('fl-row-full-height');
  1377. row.removeClass('fl-row-custom-height');
  1378. if(this.elements.height.val() == 'full') {
  1379. row.addClass('fl-row-full-height');
  1380. row.addClass('fl-row-align-' + this.elements.align.val());
  1381. this.elements.minHeight.val( '' ).trigger( 'input' );
  1382. } else if(this.elements.height.val() == 'custom') {
  1383. row.addClass('fl-row-custom-height');
  1384. row.addClass('fl-row-align-' + this.elements.align.val());
  1385. this.elements.minHeight.trigger( 'input' );
  1386. } else {
  1387. row.addClass('fl-row-default-height');
  1388. row.addClass('fl-row-align-' + this.elements.align.val());
  1389. this.elements.minHeight.val( '' ).trigger( 'input' );
  1390. }
  1391. },
  1392. /* Columns Settings
  1393. ----------------------------------------------------------*/
  1394. /**
  1395. * Initializes a column preview.
  1396. *
  1397. * @since 1.3.3
  1398. * @access private
  1399. * @method _initRow
  1400. */
  1401. _initColumn: function()
  1402. {
  1403. // Elements
  1404. $.extend(this.elements, {
  1405. size : $(this.classes.settings + ' input[name=size]', window.parent.document),
  1406. sizeLarge : $(this.classes.settings + ' input[name=size_large]', window.parent.document),
  1407. sizeMedium : $(this.classes.settings + ' input[name=size_medium]', window.parent.document),
  1408. sizeResponsive : $(this.classes.settings + ' input[name=size_responsive]', window.parent.document),
  1409. columnHeight : $(this.classes.settings + ' select[name=equal_height]', window.parent.document),
  1410. columnAlign : $(this.classes.settings + ' select[name=content_alignment]', window.parent.document),
  1411. responsiveOrder : $(this.classes.settings + ' select[name=responsive_order]', window.parent.document)
  1412. });
  1413. // Events
  1414. this.elements.size.on( 'input', $.proxy( this._colSizeChange, this ) );
  1415. this.elements.sizeLarge.on( 'input', $.proxy( this._colSizeChange, this ) );
  1416. this.elements.sizeMedium.on( 'input', $.proxy( this._colSizeChange, this ) );
  1417. this.elements.sizeResponsive.on( 'input', $.proxy( this._colSizeChange, this ) );
  1418. this.elements.columnHeight.on( 'change', $.proxy( this._colHeightChange, this ) );
  1419. this.elements.columnAlign.on( 'change', $.proxy( this._colHeightChange, this ) );
  1420. this.elements.responsiveOrder.on( 'change', $.proxy( this._colResponsiveOrder, this ) );
  1421. // Common Elements
  1422. this._initNodeTextColor();
  1423. this._initNodeBg();
  1424. this._initNodeClassName();
  1425. this._initNodeDimensions( 'border' );
  1426. this._initNodeDimensions( 'margin' );
  1427. this._initNodeDimensions( 'padding' );
  1428. },
  1429. /**
  1430. * Fires when the size field of a column changes.
  1431. *
  1432. * @since 1.3.3
  1433. * @access private
  1434. * @method _colSizeChange
  1435. */
  1436. _colSizeChange: function( e )
  1437. {
  1438. var input = $( e.target ),
  1439. minWidth = 8,
  1440. maxWidth = 100 - minWidth,
  1441. size = parseFloat( input.val() ),
  1442. group = this.elements.node.closest( '.fl-col-group' ),
  1443. prev = this.elements.node.prev('.fl-col'),
  1444. next = this.elements.node.next('.fl-col'),
  1445. sibling = next.length === 0 ? prev : next,
  1446. siblings = this.elements.node.siblings('.fl-col'),
  1447. siblingsWidth = 0,
  1448. mode = FLBuilderResponsiveEditing._mode;
  1449. // Don't resize if we only have one column.
  1450. if(siblings.length === 0) {
  1451. return;
  1452. }
  1453. // Find the fallback size if we don't have a number.
  1454. if ( isNaN( size ) ) {
  1455. if ( 'large' === mode ) {
  1456. size = this.elements.size.val();
  1457. } else if ( 'medium' === mode ) {
  1458. if ( this.elements.sizeLarge.val() ) {
  1459. size = this.elements.sizeLarge.val();
  1460. } else {
  1461. size = this.elements.size.val();
  1462. }
  1463. } else if ( 'responsive' === mode ) {
  1464. if ( this.elements.sizeMedium.val() ) {
  1465. size = this.elements.sizeMedium.val();
  1466. } else if ( this.elements.sizeLarge.val() ) {
  1467. size = this.elements.sizeLarge.val();
  1468. } else {
  1469. size = 'auto';
  1470. }
  1471. }
  1472. if ( 'auto' !== size && isNaN( size ) ) {
  1473. size = minWidth;
  1474. }
  1475. }
  1476. // Default mode logic to keep columns from stacking because of resize.
  1477. if ( 'default' === mode ) {
  1478. // Adjust sizes based on other columns.
  1479. siblings.each(function() {
  1480. if($(this).data('node') == sibling.data('node')) {
  1481. return;
  1482. }
  1483. maxWidth -= parseFloat($(this)[0].style.width);
  1484. siblingsWidth += parseFloat($(this)[0].style.width);
  1485. });
  1486. // Make sure the new width isn't too small.
  1487. if(size < minWidth) {
  1488. size = minWidth;
  1489. }
  1490. // Make sure the new width isn't too big.
  1491. if(size > maxWidth) {
  1492. size = maxWidth;
  1493. }
  1494. // Update the width.
  1495. this.elements.node.css('width', size + '%');
  1496. sibling.css('width', (100 - siblingsWidth - size) + '%');
  1497. } else {
  1498. // Don't allow resizing past 100%.
  1499. if ( size > 100 ) {
  1500. size = 100;
  1501. input.val( 100 );
  1502. }
  1503. // Update the width for responsive sizes.
  1504. this.updateCSSRule( this.classes.node, {
  1505. 'max-width': ( 'auto' === size ? 100 : size ) + '% !important',
  1506. 'width': ( 'auto' === size ? size : size + '%' ) + ' !important',
  1507. }, undefined, true );
  1508. // Float the column only if we have a responsive size.
  1509. if ( 'responsive' === mode ) {
  1510. if ( input.val() ) {
  1511. this.updateCSSRule( this.classes.node, 'float', ( FLBuilderConfig.isRtl ? 'right' : 'left' ), true );
  1512. this.updateCSSRule( this.classes.node, 'clear', 'none', true );
  1513. } else {
  1514. this.updateCSSRule( this.classes.node, 'float', 'none', true );
  1515. this.updateCSSRule( this.classes.node, 'clear', 'both', true );
  1516. }
  1517. if ( input.val() || this._colsHaveCustomResponsiveWidth( siblings ) ) {
  1518. group.addClass( 'fl-col-group-custom-width' );
  1519. } else {
  1520. group.removeClass( 'fl-col-group-custom-width' );
  1521. }
  1522. }
  1523. }
  1524. },
  1525. /**
  1526. * Checks to see if any columns in a group have
  1527. * custom responsive widths.
  1528. *
  1529. * @since 2.2
  1530. * @access private
  1531. * @method _colsHaveCustomResponsiveWidth
  1532. * @return {Boolean}
  1533. */
  1534. _colsHaveCustomResponsiveWidth: function( cols )
  1535. {
  1536. var settings = FLBuilderSettingsConfig.nodes,
  1537. hasWidth = false;
  1538. cols.each( function() {
  1539. var id = $( this ).data( 'node' );
  1540. if ( settings[ id ] && settings[ id ].size_responsive ) {
  1541. hasWidth = true;
  1542. }
  1543. } );
  1544. return hasWidth;
  1545. },
  1546. /**
  1547. * Fires when the equal height field of a column changes.
  1548. *
  1549. * @since 1.6.3
  1550. * @access private
  1551. * @method _colHeightChange
  1552. */
  1553. _colHeightChange: function()
  1554. {
  1555. var parent = this.elements.node.parent('.fl-col-group');
  1556. parent.removeClass('fl-col-group-align-top');
  1557. parent.removeClass('fl-col-group-align-center');
  1558. parent.removeClass('fl-col-group-align-bottom');
  1559. if(this.elements.columnHeight.val() == 'yes') {
  1560. parent.addClass('fl-col-group-equal-height');
  1561. parent.addClass('fl-col-group-align-' + this.elements.columnAlign.val());
  1562. }
  1563. else {
  1564. parent.removeClass('fl-col-group-equal-height');
  1565. }
  1566. },
  1567. /**
  1568. * Fires when the responsive order field of a column changes.
  1569. *
  1570. * @since 1.8
  1571. * @access private
  1572. * @method _colResponsiveOrder
  1573. */
  1574. _colResponsiveOrder: function()
  1575. {
  1576. var parent = this.elements.node.parent('.fl-col-group'),
  1577. enabledOn = this.elements.responsiveOrder.val().split(',');
  1578. if ( enabledOn.includes( 'medium' ) ) {
  1579. parent.addClass('fl-col-group-medium-reversed');
  1580. } else {
  1581. parent.removeClass('fl-col-group-medium-reversed');
  1582. }
  1583. if ( enabledOn.includes( 'mobile' ) ) {
  1584. parent.addClass('fl-col-group-responsive-reversed');
  1585. } else {
  1586. parent.removeClass('fl-col-group-responsive-reversed');
  1587. }
  1588. },
  1589. /* Module Settings
  1590. ----------------------------------------------------------*/
  1591. /**
  1592. * Initializes a module preview.
  1593. *
  1594. * @since 1.3.3
  1595. * @access private
  1596. * @method _initModule
  1597. */
  1598. _initModule: function()
  1599. {
  1600. this._initNodeClassName();
  1601. this._initNodeDimensions( 'margin' );
  1602. },
  1603. /* Default Field Previews
  1604. ----------------------------------------------------------*/
  1605. /**
  1606. * Initializes the default preview logic for each
  1607. * field in a settings form.
  1608. *
  1609. * @since 1.3.3
  1610. * @access private
  1611. * @method _initDefaultFieldPreviews
  1612. * @param {Object} fields
  1613. */
  1614. _initDefaultFieldPreviews: function( fields )
  1615. {
  1616. var fields = ! FLBuilder.isUndefined(fields) ? fields : this.elements.settings.find('.fl-field'),
  1617. field = null,
  1618. fieldType = null,
  1619. preview = null,
  1620. i = 0;
  1621. if ( FLBuilderConfig.safemode ) {
  1622. return false;
  1623. }
  1624. for( ; i < fields.length; i++) {
  1625. field = fields.eq(i);
  1626. fieldType = field.data( 'type' );
  1627. preview = field.data('preview');
  1628. if(preview.type == 'refresh') {
  1629. this._initFieldRefreshPreview(field);
  1630. }
  1631. if(preview.type == 'text') {
  1632. this._initFieldTextPreview(field);
  1633. }
  1634. if(preview.type == 'css') {
  1635. this._initFieldCSSPreview(field);
  1636. }
  1637. if(preview.type == 'widget') {
  1638. this._initFieldWidgetPreview(field);
  1639. }
  1640. if(preview.type == 'font') {
  1641. this._initFieldFontPreview(field);
  1642. }
  1643. if(preview.type == 'attribute') {
  1644. this._initFieldAttributePreview(field);
  1645. }
  1646. if(preview.type == 'animation') {
  1647. this._initFieldAnimationPreview(preview, field);
  1648. }
  1649. if(preview.type == 'callback') {
  1650. this._initFieldCallbackPreview( preview, field, fieldType, fields );
  1651. }
  1652. this._initFieldUnitSelect(field);
  1653. }
  1654. },
  1655. /**
  1656. * Setup callback type previews
  1657. *
  1658. * @since 2.2
  1659. * @access private
  1660. * @method _initFieldCallbackPreview
  1661. * @param {Object} preview - the preview args from the field configuration
  1662. * @param {Object} field - reference to the .fl-field DOM element
  1663. * @return void
  1664. */
  1665. _initFieldCallbackPreview: function ( preview, field, fieldType, fields ) {
  1666. var callback,
  1667. callback_name = preview['callback'],
  1668. form = $( '.fl-builder-settings:visible', window.parent.document ),
  1669. nodeID = form.data('node'),
  1670. node = $('.fl-builder-content .fl-node-' + nodeID );
  1671. if ( 'undefined' !== typeof FLBuilderPreviewCallbacks[callback_name] ) {
  1672. callback = FLBuilderPreviewCallbacks[callback_name];
  1673. } else if ( 'undefined' !== typeof window[callback_name] ) {
  1674. callback = window[callback_name];
  1675. } else if ( 'undefined' !== typeof window.parent[callback_name] ) {
  1676. callback = window.parent[callback_name];
  1677. }
  1678. if ( 'function' === typeof callback ) {
  1679. var args = {
  1680. field: field,
  1681. fields: fields,
  1682. type: fieldType,
  1683. preview: preview,
  1684. form: form,
  1685. nodeID: nodeID,
  1686. node: node,
  1687. };
  1688. // Grab input references
  1689. switch( fieldType ) {
  1690. case 'align':
  1691. case 'button-group':
  1692. case 'text':
  1693. case 'multiple-photos':
  1694. case 'video':
  1695. case 'icon':
  1696. case 'ordering':
  1697. args.input = field.find('input:not(.fl-preview-ignore)');
  1698. args.getValue = function() {
  1699. return args.input.val();
  1700. }
  1701. break;
  1702. case 'color':
  1703. args.input = field.find('input.fl-color-picker-value');
  1704. args.getValues = function() {
  1705. var value = args.input.val(),
  1706. values = {
  1707. value: value,
  1708. formattedValue: FLBuilderPreview.formatColor( value ),
  1709. };
  1710. }
  1711. break;
  1712. case 'textarea':
  1713. case 'code':
  1714. args.textarea = field.find('textarea');
  1715. args.getValue = function() {
  1716. return args.textarea.val();
  1717. }
  1718. break;
  1719. case 'select':
  1720. case 'photo-sizes':
  1721. case 'post-type':
  1722. args.select = field.find('select');
  1723. args.getValue = function() {
  1724. return args.select.val();
  1725. }
  1726. break;
  1727. case 'photo':
  1728. args.input = field.find('input[type=hidden]:not(.fl-preview-ignore)');
  1729. args.sizeSelect = field.find('select');
  1730. args.getValues = function() {
  1731. return {
  1732. value: args.input.val(),
  1733. size: args.sizeSelect.val(),
  1734. };
  1735. }
  1736. break;
  1737. case 'unit':
  1738. args.input = field.find('input[type=number]');
  1739. args.unitSelect = field.find( '.fl-field-unit-select' );
  1740. args.getValues = function() {
  1741. var inputVal = args.input.val(),
  1742. unitVal = args.unitSelect.val(),
  1743. values = {
  1744. value: inputVal,
  1745. unit: unitVal,
  1746. formattedValue: inputVal + unitVal
  1747. };
  1748. return values;
  1749. }
  1750. break;
  1751. case 'dimension':
  1752. args.inputs = field.find('input[type=number]');
  1753. args.unitSelect = field.find( '.fl-field-unit-select' );
  1754. args.getValues = function() {
  1755. var values = {
  1756. inputs: [],
  1757. props: {},
  1758. unit: args.unitSelect.val(),
  1759. };
  1760. args.inputs.each( function( i, input ) {
  1761. var input = $( input ),
  1762. val = input.val(),
  1763. prop = input.data('unit');
  1764. values.inputs.push( val );
  1765. values.props[prop] = val;
  1766. } );
  1767. return values;
  1768. }
  1769. break;
  1770. case 'animation':
  1771. args.input = field.find('input:not(.fl-preview-ignore)');
  1772. args.select = field.find('select');
  1773. args.getValues = function() {
  1774. return {
  1775. delay: args.input.val(),
  1776. style: args.select.val(),
  1777. };
  1778. }
  1779. break;
  1780. case 'link':
  1781. args.input = field.find('.fl-link-field-input-wrap input:not(.fl-preview-ignore)');
  1782. args.targetInput = field.find('input[name$=_target]');
  1783. args.noFollowInput = field.find('input[name$=_nofollow]');
  1784. args.getValues = function() {
  1785. return {
  1786. url: args.input.val(),
  1787. target: args.targetInput.val(),
  1788. noFollow: args.noFollowInput.val(),
  1789. }
  1790. }
  1791. break;
  1792. case 'shadow':
  1793. args.colorInput = field.find('input.fl-color-picker-value');
  1794. args.inputs = field.find('input[type=number]');
  1795. args.getValues = function() {
  1796. var values = {
  1797. color: args.colorInput.val(),
  1798. x: args.inputs[0].val(),
  1799. y: args.inputs[1].val(),
  1800. blur: args.inputs[2].val(),
  1801. spread: args.inputs[3].val(),
  1802. }
  1803. }
  1804. break;
  1805. case 'gradient':
  1806. // for event setup
  1807. args.inputs = field.find('input:not(.fl-preview-ignore)');
  1808. args.select = field.find('select');
  1809. // callback helpers
  1810. args.gradientInputs = {};
  1811. args.gradientInputs.type = field.find('select[name$="[type]"]');
  1812. args.gradientInputs.angle = field.find('input[name$="[angle]"]');
  1813. args.gradientInputs.position = field.find('select[name$="[position]"]');
  1814. args.gradientInputs.stops = [];
  1815. field.find('.fl-gradient-picker-colors .fl-gradient-picker-color-row').each( function( i, row ) {
  1816. row = $(row);
  1817. args.gradientInputs.stops.push({
  1818. color: row.find('.fl-gradient-picker-color input'),
  1819. stop: row.find('.fl-gradient-picker-stop input'),
  1820. });
  1821. });
  1822. args.getValues = function() {
  1823. var values = {
  1824. type: args.gradientInputs.type.val(),
  1825. angle: args.gradientInputs.angle.val(),
  1826. position: args.gradientInputs.position.val(),
  1827. stops: [],
  1828. };
  1829. for( var i in args.gradientInputs.stops ) {
  1830. var stop = args.gradientInputs.stops[i];
  1831. values.stops[i] = {
  1832. color: stop.color.val(),
  1833. stop: stop.stop.val(),
  1834. }
  1835. }
  1836. return values;
  1837. }
  1838. break;
  1839. case 'shape-transform':
  1840. args.inputs = field.find('input:not(.fl-preview-ignore)');
  1841. args.getValues = function() {
  1842. return {
  1843. scaleXSign: args.inputs.eq(0).val(),
  1844. scaleYSign: args.inputs.eq(1).val(),
  1845. skewX: args.inputs.eq(2).val(),
  1846. skewY: args.inputs.eq(3).val(),
  1847. scaleX: args.inputs.eq(4).val(),
  1848. rotate: args.inputs.eq(5).val(),
  1849. scaleY: args.inputs.eq(6).val(), /* hidden field */
  1850. }
  1851. }
  1852. break;
  1853. default:
  1854. args.input = field.find('input:not(.fl-preview-ignore)');
  1855. args.getValue = function() {
  1856. return args.input.val();
  1857. }
  1858. }
  1859. // Grab reference to responsive toggle
  1860. var toggle = field.find( '.fl-field-responsive-toggle');
  1861. args.responsiveToggle = toggle.length ? toggle : false;
  1862. callback = callback.bind( this, args );
  1863. // Loop over gathered inputs and setup event listeners
  1864. var props = {
  1865. input: 'change keyup input',
  1866. inputs: 'change keyup input',
  1867. targetInput: 'change keyup input',
  1868. noFollowInput: 'change keyup input',
  1869. colorInput: 'change input',
  1870. textarea: 'change keyup input',
  1871. select: 'change',
  1872. sizeSelect: 'change',
  1873. unitSelect: 'change',
  1874. };
  1875. for( var i in props ) {
  1876. if ( 'undefined' !== typeof args[i] ) {
  1877. args[i].on( props[i], callback );
  1878. }
  1879. }
  1880. }
  1881. },
  1882. /* Refresh Preview
  1883. ----------------------------------------------------------*/
  1884. /**
  1885. * Initializes the refresh preview for a field.
  1886. *
  1887. * @since 1.3.3
  1888. * @access private
  1889. * @method _initFieldRefreshPreview
  1890. * @param {Object} field The field to preview.
  1891. */
  1892. _initFieldRefreshPreview: function(field)
  1893. {
  1894. var fieldType = field.data('type'),
  1895. preview = field.data('preview'),
  1896. callback = $.proxy(this.delayPreview, this);
  1897. switch(fieldType) {
  1898. case 'align':
  1899. field.find( 'input:not(.fl-preview-ignore)' ).on( 'change', callback );
  1900. break;
  1901. case 'text':
  1902. field.find('input[type=text]:not(.fl-preview-ignore)').on('keyup', callback);
  1903. break;
  1904. case 'textarea':
  1905. field.find('textarea:not(.fl-preview-ignore)').on('keyup', callback);
  1906. break;
  1907. case 'select':
  1908. field.find('select:not(.fl-preview-ignore)').on('change', callback);
  1909. break;
  1910. case 'color':
  1911. field.find('.fl-color-picker-value').on('change', callback);
  1912. break;
  1913. case 'photo':
  1914. field.find('select:not(.fl-preview-ignore)').on('change', callback);
  1915. break;
  1916. case 'multiple-photos':
  1917. field.find('input:not(.fl-preview-ignore)').on('change', callback);
  1918. break;
  1919. case 'photo-sizes':
  1920. field.find('select:not(.fl-preview-ignore)').on('change', callback);
  1921. break;
  1922. case 'video':
  1923. field.find('input:not(.fl-preview-ignore)').on('change', callback);
  1924. break;
  1925. case 'multiple-audios':
  1926. field.find('input:not(.fl-preview-ignore)').on('change', callback);
  1927. break;
  1928. case 'icon':
  1929. field.find('input:not(.fl-preview-ignore)').on('change', callback);
  1930. break;
  1931. case 'form':
  1932. field.on( 'change', 'input:not(.fl-preview-ignore)', callback);
  1933. break;
  1934. case 'editor':
  1935. this._addTextEditorCallback(field, preview);
  1936. break;
  1937. case 'code':
  1938. field.find('textarea:not(.fl-preview-ignore)').on('change', callback);
  1939. break;
  1940. case 'post-type':
  1941. field.find('select:not(.fl-preview-ignore)').on('change', callback);
  1942. break;
  1943. case 'suggest':
  1944. field.find('.as-values').on('change', callback);
  1945. field.find('select:not(.fl-preview-ignore)').on('change', callback);
  1946. break;
  1947. case 'unit':
  1948. case 'dimension':
  1949. field.find('input[type=number]:not(.fl-preview-ignore)').on('input', callback);
  1950. break;
  1951. case 'ordering':
  1952. field.find('input[type=hidden]:not(.fl-preview-ignore)').on('change', callback);
  1953. break;
  1954. default:
  1955. field.on('change', callback);
  1956. }
  1957. },
  1958. /* Text Preview
  1959. ----------------------------------------------------------*/
  1960. /**
  1961. * Initializes a text preview for a field.
  1962. *
  1963. * @since 1.3.3
  1964. * @access private
  1965. * @method _initFieldTextPreview
  1966. * @param {Object} field The field to preview.
  1967. */
  1968. _initFieldTextPreview: function(field)
  1969. {
  1970. var fieldType = field.data('type'),
  1971. preview = field.data('preview'),
  1972. callback = $.proxy(this._previewText, this, preview);
  1973. switch(fieldType) {
  1974. case 'text':
  1975. field.find('input[type=text]:not(.fl-preview-ignore)').on('keyup', callback);
  1976. break;
  1977. case 'unit':
  1978. field.find('input[type=number]:not(.fl-preview-ignore)').on('keyup', callback);
  1979. break;
  1980. case 'textarea':
  1981. field.find('textarea:not(.fl-preview-ignore)').on('keyup', callback);
  1982. break;
  1983. case 'code':
  1984. field.find('textarea:not(.fl-preview-ignore)').on('change', callback);
  1985. break;
  1986. case 'editor':
  1987. this._addTextEditorCallback(field, preview);
  1988. break;
  1989. }
  1990. },
  1991. /**
  1992. * Runs a real time preview for text fields.
  1993. *
  1994. * @since 1.3.3
  1995. * @access private
  1996. * @method _previewText
  1997. * @param {Object} preview A preview object.
  1998. * @param {Object} e An event object.
  1999. */
  2000. _previewText: function(preview, e)
  2001. {
  2002. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2003. element = $( selector ),
  2004. text = $('<div>' + $(e.target).val() + '</div>');
  2005. if(element.length > 0) {
  2006. text.find('script').remove();
  2007. element.html(text.html());
  2008. } else {
  2009. this.delayPreview(e);
  2010. }
  2011. },
  2012. /**
  2013. * Runs a real time preview for text editor fields.
  2014. *
  2015. * @since 1.3.3
  2016. * @access private
  2017. * @method _previewText
  2018. * @param {Object} preview A preview object.
  2019. * @param {String} id The ID of the text editor.
  2020. * @param {Object} e An event object.
  2021. */
  2022. _previewTextEditor: function(preview, id, e)
  2023. {
  2024. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2025. element = $( selector ),
  2026. editor = typeof window.parent.tinyMCE != 'undefined' ? window.parent.tinyMCE.get(id) : null,
  2027. textarea = $('#' + id, window.parent.document),
  2028. text = '';
  2029. if(element.length > 0) {
  2030. if(editor && textarea.css('display') == 'none') {
  2031. text = $('<div>' + editor.getContent() + '</div>');
  2032. }
  2033. else {
  2034. if ( 'undefined' == typeof switchEditors || 'undefined' == typeof switchEditors.wpautop ) {
  2035. text = $('<div>' + textarea.val() + '</div>');
  2036. }
  2037. else {
  2038. text = $('<div>' + switchEditors.wpautop( textarea.val() ) + '</div>');
  2039. }
  2040. }
  2041. text.find('script').remove();
  2042. element.html(text.html());
  2043. }
  2044. },
  2045. /**
  2046. * Callback for text editor previews.
  2047. *
  2048. * @since 1.3.3
  2049. * @access private
  2050. * @method _previewText
  2051. * @param {Object} field A field object.
  2052. * @param {Object} preview A preview object.
  2053. */
  2054. _addTextEditorCallback: function(field, preview)
  2055. {
  2056. var id = field.find('textarea.wp-editor-area').attr('id'),
  2057. callback = null;
  2058. if(preview.type == 'refresh') {
  2059. callback = $.proxy(this.delayPreview, this);
  2060. }
  2061. else if(preview.type == 'text') {
  2062. callback = $.proxy(this._previewTextEditor, this, preview, id);
  2063. }
  2064. else {
  2065. return;
  2066. }
  2067. $('#' + id, window.parent.document).on('keyup', callback);
  2068. if(typeof window.parent.tinyMCE != 'undefined') {
  2069. editor = window.parent.tinyMCE.get(id);
  2070. editor.on('change', callback);
  2071. editor.on('keyup', callback);
  2072. }
  2073. },
  2074. /* Font Field Preview
  2075. ----------------------------------------------------------*/
  2076. /**
  2077. * Initializes a font preview for a field.
  2078. *
  2079. * @since 1.3.3
  2080. * @access private
  2081. * @method _initFieldFontPreview
  2082. * @param {Object} field The field to preview.
  2083. */
  2084. _initFieldFontPreview: function(field)
  2085. {
  2086. var fieldType = field.data('type'),
  2087. preview = field.data('preview');
  2088. // store field id
  2089. preview.id = field.attr( 'id' );
  2090. var callback = $.proxy(this._previewFont, this, preview);
  2091. if( fieldType == 'font' ){
  2092. field.find('.fl-font-field').on('change', 'select', callback);
  2093. }
  2094. },
  2095. /**
  2096. * Gets the selected font and weight, and make the necessary updates for live preview.
  2097. *
  2098. * @since 1.6.3
  2099. * @access private
  2100. * @see _getPreviewSelector
  2101. * @see _buildFontStylesheet
  2102. * @see updateCSSRule
  2103. *
  2104. * @method _previewFont
  2105. * @param {Object} preview An object with data about the current field and css selector.
  2106. * @param {[type]} e The current field.
  2107. */
  2108. _previewFont: function( preview, e ){
  2109. var parent = $( e.delegateTarget ),
  2110. font = parent.find( '.fl-font-field-font' ),
  2111. selected = $( font ).find( ':selected' ),
  2112. fontGroup = selected.parent().attr( 'label' ),
  2113. weight = parent.find( '.fl-font-field-weight' ),
  2114. uniqueID = preview.id + '-' + this.nodeId,
  2115. selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2116. important = preview.important ? ' !important' : '',
  2117. val = ''
  2118. // If the selected font is a Google Font, build the font stylesheet
  2119. if( fontGroup == 'Google' || fontGroup == 'Recently Used' ){
  2120. this._buildFontStylesheet( uniqueID, font.val(), weight.val() );
  2121. }
  2122. val = font.val();
  2123. // Some google fonts that end with numbers need to be wrapped in quotes.
  2124. var checkNum = new RegExp('[0-9]');
  2125. if( checkNum.test( font.val() ) ){
  2126. val = '"' + font.val() + '"';
  2127. }
  2128. this.updateCSSRule( selector, 'font-family', 'Default' === font.val() ? '' : val + important );
  2129. this.updateCSSRule( selector, 'font-weight', 'default' === weight.val() ? '' : weight.val() + important );
  2130. },
  2131. /**
  2132. * Gets all fonts store insite FLBuilderPreview._fontsList and renders the respective
  2133. * link tag with Google Fonts.
  2134. *
  2135. * @since 1.6.3
  2136. * @access private
  2137. *
  2138. * @method _buildFontStylesheet
  2139. * @param {String} id The field unique ID.
  2140. * @param {String} font The selected font.
  2141. * @param {String} weight The selected weight.
  2142. */
  2143. _buildFontStylesheet: function( id, font, weight ){
  2144. var url = FLBuilderConfig.googleFontsUrl,
  2145. href = '',
  2146. fontObj = {},
  2147. fontArray = {};
  2148. // build the font family / weight object
  2149. fontObj[ font ] = [ weight ];
  2150. // adds to the list of fonts for this font setting
  2151. FLBuilderPreview._fontsList[ id ] = fontObj;
  2152. // iterate over the keys of the FLBuilderPreview._fontsList object
  2153. Object.keys( FLBuilderPreview._fontsList ).forEach( function( fieldFont ) {
  2154. var field = FLBuilderPreview._fontsList[ fieldFont ];
  2155. // iterate over the font / weight object
  2156. Object.keys( field ).forEach( function( key ) {
  2157. // get the weights of this font
  2158. var weights = field[ key ];
  2159. fontArray[ key ] = fontArray[ key ] || [];
  2160. // remove duplicates from the values array
  2161. weights = weights.filter( function( weight ) {
  2162. return fontArray[ key ].indexOf( weight ) < 0;
  2163. });
  2164. fontArray[ key ] = fontArray[ key ].concat( weights );
  2165. });
  2166. });
  2167. $.each( fontArray, function( font, weight ){
  2168. if ( 'Molle' === font ) {
  2169. href += font + ':i|';
  2170. } else {
  2171. href += font + ':' + weight.join() + '|';
  2172. }
  2173. } );
  2174. // remove last character and replace spaces with plus signs
  2175. href = url + href.slice( 0, -1 ).replace( ' ', '+' );
  2176. if( $( '#fl-builder-google-fonts-preview' ).length < 1 ){
  2177. $( '<link>' )
  2178. .attr( 'id', 'fl-builder-google-fonts-preview' )
  2179. .attr( 'type', 'text/css' )
  2180. .attr( 'rel', 'stylesheet' )
  2181. .attr( 'href', href )
  2182. .appendTo('head');
  2183. } else{
  2184. $( '#fl-builder-google-fonts-preview' ).attr( 'href', href );
  2185. }
  2186. },
  2187. /* CSS Preview
  2188. ----------------------------------------------------------*/
  2189. /**
  2190. * Initializes CSS previews for a node.
  2191. *
  2192. * @since 1.3.3
  2193. * @since 1.6.1 Reworked to accept a preview.rules array.
  2194. * @access private
  2195. * @method _initFieldCSSPreview
  2196. * @param {Object} field A field object.
  2197. */
  2198. _initFieldCSSPreview: function( field )
  2199. {
  2200. var preview = field.data( 'preview' ),
  2201. i = null;
  2202. if ( 'undefined' != typeof preview.rules ) {
  2203. for ( i in preview.rules ) {
  2204. this._initFieldCSSPreviewCallback( field, preview.rules[ i ] );
  2205. }
  2206. }
  2207. else {
  2208. this._initFieldCSSPreviewCallback( field, preview );
  2209. }
  2210. },
  2211. /**
  2212. * Initializes CSS preview callbacks for a field.
  2213. *
  2214. * @since 1.6.1
  2215. * @access private
  2216. * @method _initFieldCSSPreviewCallback
  2217. * @param {Object} field A field object.
  2218. * @param {Object} preview The preview data object.
  2219. */
  2220. _initFieldCSSPreviewCallback: function( field, preview )
  2221. {
  2222. switch ( field.data( 'type' ) ) {
  2223. case 'align':
  2224. case 'justify':
  2225. case 'grid-auto-flow':
  2226. field.find( 'input:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2227. break;
  2228. case 'object-fit':
  2229. // Position Sub-field
  2230. field.find( 'input:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewCSS, this, preview, field ) );
  2231. // Fit Button Group
  2232. const fit = field.find( 'input[type="hidden"]:not(.fl-preview-ignore)' )
  2233. fit.on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2234. fit.on( 'change', $.proxy( this._previewFieldObjectFit, this, preview, field ) );
  2235. break;
  2236. case 'border':
  2237. field.find( 'select:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewBorderCSS, this, preview, field ) );
  2238. field.find( 'input[type=number]:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewBorderCSS, this, preview, field ) );
  2239. field.find( 'input[type=hidden]:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewBorderCSS, this, preview, field ) );
  2240. break;
  2241. case 'color':
  2242. field.find( '.fl-color-picker-value' ).on( 'change', $.proxy( this._previewColorCSS, this, preview, field ) );
  2243. break;
  2244. case 'dimension':
  2245. field.find( 'input[type=number]:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewDimensionCSS, this, preview, field ) );
  2246. break;
  2247. case 'gradient':
  2248. field.find( 'select:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewGradientCSS, this, preview, field ) );
  2249. field.find( '.fl-gradient-picker-angle' ).on( 'input', $.proxy( this._previewGradientCSS, this, preview, field ) );
  2250. field.find( '.fl-color-picker-value' ).on( 'change', $.proxy( this._previewGradientCSS, this, preview, field ) );
  2251. field.find( '.fl-gradient-picker-stop' ).on( 'input', $.proxy( this._previewGradientCSS, this, preview, field ) );
  2252. break;
  2253. case 'photo':
  2254. field.find( 'select:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2255. break;
  2256. case 'select':
  2257. case 'aspect-ratio':
  2258. case 'placement':
  2259. field.find( 'select:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2260. break;
  2261. case 'shadow':
  2262. field.find( 'input:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewShadowCSS, this, preview, field ) );
  2263. field.find( '.fl-color-picker-value' ).on( 'change', $.proxy( this._previewShadowCSS, this, preview, field ) );
  2264. break;
  2265. case 'text':
  2266. field.find( 'input[type=text]:not(.fl-preview-ignore)' ).on( 'keyup input change', $.proxy( this._previewCSS, this, preview, field ) );
  2267. break;
  2268. case 'hidden':
  2269. field.find( 'input[type=hidden]:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2270. break;
  2271. case 'typography':
  2272. field.find( 'select:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewTypographyCSS, this, preview, field ) );
  2273. field.find( 'input[type=number]:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewTypographyCSS, this, preview, field ) );
  2274. field.find( 'input[type=hidden]:not(.fl-preview-ignore)' ).on( 'change', $.proxy( this._previewTypographyCSS, this, preview, field ) );
  2275. break;
  2276. case 'unit':
  2277. field.find( 'input[type=number]:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewCSS, this, preview, field ) );
  2278. break;
  2279. case 'button-group':
  2280. case 'grid-tracklist':
  2281. case 'grid-area':
  2282. field.find( 'input[type=hidden]:not(.fl-preview-ignore)').on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2283. break;
  2284. case 'flex':
  2285. case 'size':
  2286. field.find( 'input[type=number]:not(.fl-preview-ignore)' ).on( 'input', $.proxy( this._previewCSS, this, preview, field ) );
  2287. field.find( 'input[type=hidden]:not(.fl-preview-ignore)').on( 'change', $.proxy( this._previewCSS, this, preview, field ) );
  2288. break;
  2289. }
  2290. },
  2291. /**
  2292. * Updates the CSS rule for a preview.
  2293. *
  2294. * @since 1.3.3
  2295. * @access private
  2296. * @method _previewCSS
  2297. * @param {Object} preview A preview object.
  2298. * @param {Object} field A preview field element.
  2299. * @param {Object} e An event object.
  2300. */
  2301. _previewCSS: function( preview, field, e )
  2302. {
  2303. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2304. property = preview.property,
  2305. unit = this._getPreviewCSSUnit( preview, field, e ),
  2306. input = $( e.target ),
  2307. subVals = preview.substitute_values, /* substitute the css value for a staticly-defined alternative */
  2308. rawVal = input.val(),
  2309. value = subVals && Object.keys( subVals ).includes( rawVal ) ? subVals[rawVal] : rawVal,
  2310. responsive = input.closest( '.fl-field-responsive-setting' ).length ? true : false,
  2311. important = preview.important && '' !== value ? ' !important' : '';
  2312. if ( property.indexOf( 'image' ) > -1 && value ) {
  2313. value = 'url(' + value + ')';
  2314. } else if ( '%' === unit && 'opacity' === property ) {
  2315. value = parseInt( value ) / 100;
  2316. } else if ( '' !== value && ! isNaN(value) ) {
  2317. value += unit;
  2318. }
  2319. if ( preview.format_value ) {
  2320. value = wp.i18n.sprintf( preview.format_value, value )
  2321. }
  2322. /**
  2323. * Support for sub_value key when previewing compound fields.
  2324. */
  2325. if ( undefined !== preview.sub_value ) {
  2326. const name = input.attr('name')
  2327. if ( 'string' === typeof preview.sub_value && ! name.endsWith( `[${preview.sub_value}]` ) ) {
  2328. return
  2329. } else if ( 'object' === typeof preview.sub_value && ! name.includes(`[${preview.sub_value.setting_name}]`) ) {
  2330. return
  2331. }
  2332. }
  2333. /**
  2334. * Support for enabled property
  2335. * Meant to closely match FLBuilderCSS::is_rule_enabled()
  2336. */
  2337. if ( 'boolean' === typeof preview.enabled ) {
  2338. return preview.enabled
  2339. } else if ( preview.enabled && 'object' === typeof preview.enabled ) {
  2340. const form = $( '.fl-builder-settings:visible', window.parent.document )
  2341. const settings = FLBuilder._getSettings( form )
  2342. // Test each setting in the object
  2343. for( let name in preview.enabled ) {
  2344. const test = preview.enabled[name]
  2345. if ( 'object' === typeof test ) {
  2346. if ( 'nearest_value' in test ) {
  2347. const mode = FLBuilderResponsiveEditing._mode
  2348. const inherited = this.getInheritedSettingValue( name, mode, settings )
  2349. if ( 'string' === typeof test.nearest_value && test.nearest_value !== inherited ) {
  2350. return
  2351. } else if ( Array.isArray( test.nearest_value ) && ! test.nearest_value.includes( inherited ) ) {
  2352. return
  2353. }
  2354. }
  2355. } else if ( Array.isArray( preview.enabled[name] ) ) {
  2356. if ( ! preview.enabled[name].some( v => v === settings[name] ) ) {
  2357. return
  2358. }
  2359. } else if ( preview.enabled[name] !== settings[name] ) {
  2360. return
  2361. }
  2362. }
  2363. }
  2364. if ( Array.isArray( property ) ) {
  2365. for( let i in property ) {
  2366. this.updateCSSRule( selector, property[i], value + important, responsive );
  2367. }
  2368. } else {
  2369. this.updateCSSRule( selector, property, value + important, responsive );
  2370. }
  2371. },
  2372. /**
  2373. * Find the nearest inherited value for a particular setting from a given breakpoint.
  2374. */
  2375. getInheritedSettingValue: function( settingBaseName, currentBreakpoint, settings ) {
  2376. const isDefault = '' === currentBreakpoint || 'default' === currentBreakpoint
  2377. let name = isDefault ? settingBaseName : `${settingBaseName}_${currentBreakpoint}`
  2378. if ( undefined !== settings[name] ) {
  2379. return settings[name]
  2380. }
  2381. // Meant to be in reverse (most-to-least specific)
  2382. const current = 'default' === currentBreakpoint ? '' : currentBreakpoint
  2383. let breakpoints = [ 'responsive', 'medium', 'large', '' ]
  2384. let i = breakpoints.indexOf( current )
  2385. if ( -1 === i ) {
  2386. return null
  2387. }
  2388. breakpoints = breakpoints.slice(i)
  2389. for( let i in breakpoints ) {
  2390. const breakpoint = breakpoints[i]
  2391. name = '' === breakpoint ? settingBaseName : `${settingBaseName}_${breakpoint}`
  2392. if ( undefined !== settings[name] && '' !== settings[name] ) {
  2393. return settings[name]
  2394. }
  2395. }
  2396. return null
  2397. },
  2398. /* Border Field CSS Preview
  2399. ----------------------------------------------------------*/
  2400. /**
  2401. * Updates the CSS rule for a border preview.
  2402. *
  2403. * @since 2.2
  2404. * @access private
  2405. * @method _previewBorderCSS
  2406. * @param {Object} preview A preview object.
  2407. * @param {Object} field A field object.
  2408. * @param {Object} e An event object.
  2409. */
  2410. _previewBorderCSS: function( preview, field, e )
  2411. {
  2412. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2413. target = $( e.target ),
  2414. field = target.closest( '.fl-field' ),
  2415. wrap = target.closest( '.fl-compound-field-setting' ),
  2416. property = wrap.data( 'property' ),
  2417. value = target.val(),
  2418. unit = wrap.find( '.fl-field-unit-select' ),
  2419. responsive = target.closest( '.fl-field-responsive-setting' ).length ? true : false,
  2420. important = preview.important && '' !== value ? ' !important' : '';
  2421. preview.property = property;
  2422. if ( 'border-color' === property ) {
  2423. this._previewColorCSS( preview, field, e );
  2424. } else if ( 'border-width' === property || 'border-radius' === property ) {
  2425. this._previewDimensionCSS( preview, field, e );
  2426. } else if ( 'box-shadow' === property ) {
  2427. this._previewShadowCSS( preview, wrap, e );
  2428. } else {
  2429. if ( 'border-style' === property ) {
  2430. field.find( '.fl-border-field-width input:visible' ).trigger( 'input' );
  2431. }
  2432. this.updateCSSRule( selector, property, value + important, responsive );
  2433. }
  2434. },
  2435. /* Color Field CSS Preview
  2436. ----------------------------------------------------------*/
  2437. /**
  2438. * Updates the CSS rule for a color preview.
  2439. *
  2440. * @since 1.3.3
  2441. * @access private
  2442. * @method _previewColorCSS
  2443. * @param {Object} preview A preview object.
  2444. * @param {Object} field A field object.
  2445. * @param {Object} e An event object.
  2446. */
  2447. _previewColorCSS: function(preview, field, e)
  2448. {
  2449. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2450. input = $(e.target),
  2451. value = input.val(),
  2452. responsive = input.closest( '.fl-field-responsive-setting' ).length ? true : false,
  2453. important = preview.important && '' !== value ? ' !important' : '';
  2454. if ( '' !== value && value.indexOf( 'rgb' ) < 0 && value.indexOf( 'var' ) < 0 ) {
  2455. value = '#' + value;
  2456. }
  2457. this.updateCSSRule( selector, preview.property, value + important, responsive );
  2458. },
  2459. /* Dimension Field CSS Preview
  2460. ----------------------------------------------------------*/
  2461. /**
  2462. * Updates the CSS rule for a dimension field preview.
  2463. *
  2464. * @since 2.0.7
  2465. * @access private
  2466. * @method _previewDimensionCSS
  2467. * @param {Object} preview A preview object.
  2468. * @param {Object} field A preview field element.
  2469. * @param {Object} e An event object.
  2470. */
  2471. _previewDimensionCSS: function( preview, field, e )
  2472. {
  2473. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2474. property = preview.property,
  2475. key = field.attr( 'id' ).replace( 'fl-field-', '' ),
  2476. dimension = $( e.target ).data( 'unit' ),
  2477. value = this._getDimensionValue( preview, field, dimension, e ),
  2478. responsive = field.find( '.fl-field-responsive-setting' ).length ? true : false,
  2479. important = preview.important && '' !== value ? ' !important' : '';
  2480. if ( 'border-radius' === property ) {
  2481. property = 'border-' + dimension.replace( '_', '-' ) + '-radius';
  2482. } else if ( 'border-width' === property ) {
  2483. property = 'border-' + dimension + '-width';
  2484. } else if ( 'gap' === property ) {
  2485. property = dimension + '-' + property;
  2486. } else {
  2487. property = property + '-' + dimension;
  2488. }
  2489. this.updateCSSRule( selector, property, value + important, responsive );
  2490. if ( 'margin' === key || 'padding' === key || 'border' === key ) {
  2491. if ( this.elements.node.find('.fl-bg-slideshow').length ) {
  2492. FLBuilder._resizeLayout();
  2493. }
  2494. }
  2495. },
  2496. /**
  2497. * Get a preview dimension value for a property.
  2498. *
  2499. * @since 2.2
  2500. * @access private
  2501. * @param {Object} preview A preview object.
  2502. * @param {Object} field A preview field element.
  2503. * @param {String} dimension The dimension key.
  2504. * @param {Object} e An event object.
  2505. * @return {String}
  2506. */
  2507. _getDimensionValue: function( preview, field, dimension, e )
  2508. {
  2509. var input = $( e.target ),
  2510. value = input.val(),
  2511. unit = '';
  2512. value = value.toLowerCase().replace( /[^a-z0-9%.\-]/g, '' );
  2513. if ( null !== value && '' !== value && ! isNaN( value ) ) {
  2514. unit = this._getPreviewCSSUnit( preview, field, e );
  2515. value = parseFloat( value ) + ( unit ? unit : 'px' );
  2516. } else if ( input.attr( 'placeholder' ) ) {
  2517. unit = this._getPreviewCSSUnit( preview, field, e );
  2518. value = parseFloat( input.attr( 'placeholder' ) ) + ( unit ? unit : 'px' );
  2519. }
  2520. return value;
  2521. },
  2522. /**
  2523. * Get the value's unit for a CSS preview.
  2524. *
  2525. * @since 2.2
  2526. * @access private
  2527. * @param {Object} preview A preview object.
  2528. * @param {Object} field A preview field element.
  2529. * @param {Object} e An event object.
  2530. * @return {String}
  2531. */
  2532. _getPreviewCSSUnit: function( preview, field, e )
  2533. {
  2534. var input = $( e.target ),
  2535. mode = FLBuilderResponsiveEditing._mode,
  2536. compound = input.closest( '.fl-compound-field-setting' ).length ? true : false,
  2537. responsive = input.closest( '.fl-field-responsive-setting' ).length ? true : false,
  2538. select = null;
  2539. if ( compound ) {
  2540. select = input.closest( '.fl-compound-field-setting' ).find( '.fl-field-unit-select' );
  2541. } else if ( responsive ) {
  2542. select = input.closest( '.fl-field-responsive-setting' ).find( '.fl-field-unit-select' );
  2543. } else {
  2544. select = field.find( '.fl-field-unit-select' );
  2545. }
  2546. if ( select && select.length ) {
  2547. if ( 'SELECT' === select.prop( 'tagName' ) ) {
  2548. return select.val();
  2549. } else {
  2550. return select.text();
  2551. }
  2552. } else if ( preview.unit ) {
  2553. return preview.unit;
  2554. }
  2555. return '';
  2556. },
  2557. /**
  2558. * Initializes the custom unit select for a field.
  2559. *
  2560. * @since 2.2
  2561. * @access private
  2562. * @method _initFieldUnitSelect
  2563. * @param {Object} field
  2564. */
  2565. _initFieldUnitSelect: function(field)
  2566. {
  2567. field.find( '.fl-field-unit-select' ).on( 'change', function() {
  2568. var select = $( this ),
  2569. responsive = select.closest( '.fl-field-responsive-setting' ),
  2570. field = select.closest( '.fl-field' );
  2571. if ( responsive.length ) {
  2572. responsive.find( 'input' ).trigger( 'input' );
  2573. } else {
  2574. field.find( 'input' ).trigger( 'input' );
  2575. }
  2576. } );
  2577. },
  2578. /* Gradient Field CSS Preview
  2579. ----------------------------------------------------------*/
  2580. /**
  2581. * Updates the CSS rule for a gradient preview.
  2582. *
  2583. * @since 2.2
  2584. * @access private
  2585. * @method _previewGradientCSS
  2586. * @param {Object} preview A preview object.
  2587. * @param {Object} field A field object.
  2588. * @param {Object} e An event object.
  2589. */
  2590. _previewGradientCSS: function( preview, field, e )
  2591. {
  2592. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2593. type = field.find( '.fl-gradient-picker-type-select' ).val(),
  2594. angle = field.find( '.fl-gradient-picker-angle' ).val(),
  2595. position = field.find( '.fl-gradient-picker-position' ).val(),
  2596. colors = field.find( '.fl-color-picker-value' ),
  2597. stops = field.find( '.fl-gradient-picker-stop input' ),
  2598. values = [],
  2599. value = '',
  2600. important = '';
  2601. colors.each( function( i ) {
  2602. var color = $( this ).val(),
  2603. stop = stops.eq( i ).val();
  2604. if ( '' === color ) {
  2605. color = 'rgba(255,255,255,0)';
  2606. }
  2607. if ( color.indexOf( 'rgb' ) < 0 ) {
  2608. color = '#' + color;
  2609. }
  2610. if ( isNaN( stop ) ) {
  2611. stop = 0;
  2612. }
  2613. values.push( color + ' ' + stop + '%' );
  2614. } );
  2615. values = values.join( ', ' );
  2616. if ( 'linear' === type ) {
  2617. if ( isNaN( angle ) ) {
  2618. angle = 0;
  2619. }
  2620. value = 'linear-gradient(' + angle + 'deg, ' + values + ')';
  2621. } else {
  2622. value = 'radial-gradient(at ' + position + ', ' + values + ')';
  2623. }
  2624. important = preview.important && '' !== value ? ' !important' : '';
  2625. this.updateCSSRule( selector, preview.property, value + important );
  2626. },
  2627. /* Shadow Field CSS Preview
  2628. ----------------------------------------------------------*/
  2629. /**
  2630. * Updates the CSS rule for a shadow preview.
  2631. *
  2632. * @since 2.2
  2633. * @access private
  2634. * @method _previewShadowCSS
  2635. * @param {Object} preview A preview object.
  2636. * @param {Object} field A field object.
  2637. * @param {Object} e An event object.
  2638. */
  2639. _previewShadowCSS: function( preview, field, e )
  2640. {
  2641. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2642. color = field.find( '.fl-shadow-field-color input' ).val(),
  2643. horizontal = field.find( '.fl-shadow-field-horizontal input' ).val(),
  2644. vertical = field.find( '.fl-shadow-field-vertical input' ).val(),
  2645. blur = field.find( '.fl-shadow-field-blur input' ).val(),
  2646. spread = field.find( '.fl-shadow-field-spread input' ).val(),
  2647. hasSpread = field.find( '.fl-shadow-field-spread input' ).length ? true : false,
  2648. responsive = $( e.target ).closest( '.fl-field-responsive-setting' ).length ? true : false,
  2649. value = '',
  2650. important = '';
  2651. if ( '' !== color ) {
  2652. if ( '' === horizontal ) {
  2653. horizontal = 0;
  2654. }
  2655. if ( '' === vertical ) {
  2656. vertical = 0;
  2657. }
  2658. if ( '' === blur ) {
  2659. blur = 0;
  2660. }
  2661. if ( '' === spread ) {
  2662. spread = 0;
  2663. }
  2664. if ( color.indexOf( 'rgb' ) < 0 ) {
  2665. color = '#' + color;
  2666. }
  2667. value = horizontal + 'px ';
  2668. value += vertical + 'px ';
  2669. value += blur + 'px ';
  2670. if ( hasSpread ) {
  2671. value += spread + 'px ';
  2672. }
  2673. value += color;
  2674. value += important;
  2675. }
  2676. important = preview.important && '' !== value ? ' !important' : '';
  2677. this.updateCSSRule( selector, preview.property, value, responsive );
  2678. },
  2679. /* Typography Field CSS Preview
  2680. ----------------------------------------------------------*/
  2681. /**
  2682. * Updates the CSS rule for a typography preview.
  2683. *
  2684. * @since 2.2
  2685. * @access private
  2686. * @method _previewTypographyCSS
  2687. * @param {Object} preview A preview object.
  2688. * @param {Object} field A field object.
  2689. * @param {Object} e An event object.
  2690. */
  2691. _previewTypographyCSS: function( preview, field, e )
  2692. {
  2693. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2694. target = $( e.target ),
  2695. field = target.closest( '.fl-field' ),
  2696. wrap = target.closest( '.fl-compound-field-setting' ),
  2697. property = wrap.data( 'property' ),
  2698. value = target.val(),
  2699. unit = wrap.find( '.fl-field-unit-select' ),
  2700. responsive = target.closest( '.fl-field-responsive-setting' ).length ? true : false,
  2701. important = preview.important && '' !== value ? ' !important' : '';
  2702. if ( 'font-family' === property ) {
  2703. preview.id = field.attr( 'id' );
  2704. this._previewFont( preview, { delegateTarget: wrap } );
  2705. } else if ( 'text-shadow' === property ) {
  2706. preview.property = 'text-shadow';
  2707. this._previewShadowCSS( preview, wrap, e );
  2708. } else {
  2709. if ( unit.length && '' !== value ) {
  2710. if ( 'vw' === unit.val() ) {
  2711. // calc(14px + 5vw);
  2712. value = 'calc(' + FLBuilderConfig.global.responsive_base_fontsize + 'px + ' + value + 'vw)'
  2713. } else {
  2714. value += 'SELECT' === unit.prop( 'tagName' ) ? unit.val() : 'px';
  2715. }
  2716. }
  2717. this.updateCSSRule( selector, property, value + important, responsive );
  2718. }
  2719. },
  2720. /* Widget Preview
  2721. ----------------------------------------------------------*/
  2722. /**
  2723. * Initializes the attribute preview for a field.
  2724. *
  2725. * @since 2.2
  2726. * @access private
  2727. * @method _initFieldAttributePreview
  2728. * @param {Object} field The field to preview.
  2729. */
  2730. _initFieldAttributePreview: function(field)
  2731. {
  2732. var preview = field.data('preview'),
  2733. attrName = preview.attribute,
  2734. input = field.find('input'),
  2735. value = field.val(),
  2736. formatValue = window[preview.format_callback];
  2737. var fullSelector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2738. element = $( fullSelector );
  2739. var callback = this._previewAttribute.bind( this, input, element, attrName, formatValue );
  2740. input.on('change', callback );
  2741. input.on('keyup', callback );
  2742. input.on('input', callback );
  2743. },
  2744. /**
  2745. * Runs a real time preview for attribute fields.
  2746. *
  2747. * @since 2.2
  2748. * @access private
  2749. * @method _previewAttribute
  2750. * @param {Object} input A ref to the input control.
  2751. * @param {Object} element A ref to the selected element within the node.
  2752. * @param String attrName The name of the attribute to be changed.
  2753. */
  2754. _previewAttribute: function( input, element, attrName, formatValue ) {
  2755. var value = input.val();
  2756. if ( 'function' === typeof formatValue ) {
  2757. value = formatValue( value );
  2758. }
  2759. for (i = 0; i < element.length; i++) {
  2760. element[i].setAttribute( attrName, value );
  2761. }
  2762. },
  2763. /**
  2764. * Initializes the preview for a WordPress widget.
  2765. *
  2766. * @since 1.3.3
  2767. * @access private
  2768. * @method _initFieldWidgetPreview
  2769. * @param {Object} field A field object.
  2770. */
  2771. _initFieldWidgetPreview: function(field)
  2772. {
  2773. var callback = $.proxy(this.delayPreview, this);
  2774. field.find('input').on('keyup', callback);
  2775. field.find('input[type=checkbox]').on('click', callback);
  2776. field.find('textarea').on('keyup', callback);
  2777. field.find('select').on('change', callback);
  2778. },
  2779. /* Animation Field Preview
  2780. ----------------------------------------------------------*/
  2781. /**
  2782. * Initializes animation previews.
  2783. *
  2784. * @since 2.2
  2785. * @access private
  2786. * @method _initFieldAnimationPreview
  2787. */
  2788. _initFieldAnimationPreview: function( preview, field )
  2789. {
  2790. field.find( '.fl-animation-field-style select' ).on( 'change', $.proxy( this._previewAnimationField, this, preview, field ) );
  2791. field.find( '.fl-animation-field-duration input' ).on( 'input', $.proxy( this._previewAnimationField, this, preview, field ) );
  2792. },
  2793. /**
  2794. * Previews an animation field.
  2795. *
  2796. * @since 2.2
  2797. * @access private
  2798. * @method _previewAnimationField
  2799. */
  2800. _previewAnimationField: function( preview, field, e )
  2801. {
  2802. var selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2803. element = $( selector ),
  2804. animation = field.find( '.fl-animation-field-style select' ),
  2805. duration = field.find( '.fl-animation-field-duration input' ),
  2806. options = animation[0].options;
  2807. element.removeClass( 'fl-animated' );
  2808. element.removeClass( 'fl-animation' );
  2809. element.css( 'animation-duration', '' );
  2810. for ( var i = 0; i < options.length; i++ ) {
  2811. element.removeClass( 'fl-' + options[i].value );
  2812. }
  2813. if ( '' !== animation.val() ) {
  2814. element.addClass( 'fl-animation' );
  2815. element.addClass( 'fl-' + animation.val() );
  2816. element.data( 'animation-delay', 0 );
  2817. element.data( 'animation-duration', duration.val() );
  2818. }
  2819. FLBuilderLayout._doModuleAnimation.apply( element );
  2820. },
  2821. _previewFieldObjectFit: function( preview, field, e ) {
  2822. const selector = this._getPreviewSelector( this.classes.node, preview.selector ),
  2823. element = $( selector ).closest('.fl-module').get(0),
  2824. className = 'fl-fill-container',
  2825. fit = field.find( '.fl-button-group-field input' ).val();
  2826. if ( '' === fit && element.classList.contains( className ) ) {
  2827. element.classList.remove( className )
  2828. }
  2829. if ( '' !== fit && ! element.classList.contains( className ) ) {
  2830. element.classList.add( className )
  2831. }
  2832. },
  2833. /**
  2834. * Returns a formatted selector string for a preview.
  2835. *
  2836. * @since 1.6.1
  2837. * @access private
  2838. * @method _getPreviewSelector
  2839. * @param {String} selector A CSS selector string.
  2840. * @return {String}
  2841. */
  2842. _getPreviewSelector: function( prefix, selector )
  2843. {
  2844. return FLBuilderPreview.getFormattedSelector.call( this, prefix, selector );
  2845. },
  2846. /**
  2847. * Converts words in a string to upper case.
  2848. *
  2849. * @since 2.2
  2850. * @method toUpperCaseWords
  2851. * @param {String} string
  2852. * @return {String}
  2853. */
  2854. toUpperCaseWords: function( string ) {
  2855. return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
  2856. },
  2857. };
  2858. /**
  2859. * Format a color value for use with CSS
  2860. * @since 2.2
  2861. * @method formatColor
  2862. * @param {String} value
  2863. * @return {String}
  2864. */
  2865. FLBuilderPreview.formatColor = function( value ) {
  2866. if ( '' !== value && ( value.indexOf( 'rgb' ) < 0 && value.indexOf( 'url' ) < 0 ) ) {
  2867. value = '#' + value;
  2868. }
  2869. return value;
  2870. };
  2871. FLBuilderPreviewCallbacks = {
  2872. /**
  2873. * Setup the shape when selected
  2874. */
  2875. previewShape: function( args, e ) {
  2876. var position = args.preview.position,
  2877. prefix = args.preview.prefix,
  2878. form = args.form,
  2879. yOrientationInputName = prefix + 'transform[][scaleYSign]';
  2880. yOrientation = form.find('input[name="' + yOrientationInputName + '"]');
  2881. if ( 'bottom' === position ) {
  2882. yOrientation.val('invert');
  2883. } else {
  2884. yOrientation.val('');
  2885. }
  2886. yOrientation.trigger('change');
  2887. // Cause refresh
  2888. this.delayPreview();
  2889. },
  2890. /**
  2891. * Preview the layer's width, height and Y offset
  2892. */
  2893. previewShapeLayerSize: function( args, e ) {
  2894. var values = args.getValues(),
  2895. unitValue = values.unit,
  2896. width = values.props.width,
  2897. height = values.props.height,
  2898. top = values.props.top,
  2899. /* static data from field config */
  2900. prefix = args.preview.prefix,
  2901. position = args.preview.position,
  2902. layerSelector = this._getPreviewSelector( this.classes.node, '.fl-builder-' + position + '-edge-layer' ),
  2903. shapeSelector = layerSelector + ' > *',
  2904. /* the align field */
  2905. align = args.form.find('[name="' + prefix + 'align"]'),
  2906. alignValue = align.val(),
  2907. alignParts = alignValue.split(' '),
  2908. yAlign = alignParts[0],
  2909. xAlign = alignParts[1],
  2910. /* calculated props */
  2911. shapeField = args.form.find('[name="' + prefix + 'shape"]'),
  2912. shapeValue = shapeField.val(),
  2913. shapePreset = FLBuilderConfig.presets.shape[shapeValue]
  2914. shapeProps = {};
  2915. // Defaults
  2916. shapeProps.width = '100%';
  2917. shapeProps.left = 'auto';
  2918. shapeProps.right = 'auto';
  2919. shapeProps.height = 'auto';
  2920. shapeProps.top = 'auto';
  2921. shapeProps.bottom = 'auto';
  2922. // Width
  2923. if ( width ) {
  2924. shapeProps.width = width + unitValue;
  2925. var offset = ( width / 2 ) + unitValue;
  2926. switch( xAlign ) {
  2927. case 'left':
  2928. shapeProps.left = '0';
  2929. shapeProps.right = 'auto';
  2930. break;
  2931. case 'right':
  2932. shapeProps.left = 'auto';
  2933. shapeProps.right = '0';
  2934. break;
  2935. case 'center':
  2936. shapeProps.left = 'calc( 50% - ' + offset + ')';
  2937. shapeProps.right = 'auto';
  2938. break;
  2939. }
  2940. }
  2941. this.updateCSSRule( shapeSelector, 'width', shapeProps.width );
  2942. this.updateCSSRule( shapeSelector, 'left', shapeProps.left );
  2943. this.updateCSSRule( shapeSelector, 'right', shapeProps.right );
  2944. // Height
  2945. // We need a height for vertical centering to work, but it doesn't have to be explicit.
  2946. var heightOffset;
  2947. if ( height ) {
  2948. heightOffset = ( height / 2 ) + unitValue;
  2949. } else if ( width ) {
  2950. var viewBoxHeight = shapePreset.data.viewBox.width,
  2951. impliedHeight = ( width / viewBoxHeight ) * 100;
  2952. heightOffset = ( impliedHeight / 2 ) + unitValue ;
  2953. } else {
  2954. heightOffset = ''
  2955. }
  2956. if ( height ) {
  2957. shapeProps.height = height + unitValue;
  2958. }
  2959. switch( yAlign ) {
  2960. case 'top':
  2961. shapeProps.top = '0';
  2962. shapeProps.bottom = 'auto';
  2963. break;
  2964. case 'bottom':
  2965. shapeProps.top = 'auto';
  2966. shapeProps.bottom = '0';
  2967. break;
  2968. case 'center':
  2969. shapeProps.top = 'calc( 50% - ' + heightOffset + ')';
  2970. shapeProps.bottom = 'auto';
  2971. break;
  2972. }
  2973. this.updateCSSRule( shapeSelector, 'height', shapeProps.height + ' !important' );
  2974. this.updateCSSRule( shapeSelector, 'top', shapeProps.top );
  2975. this.updateCSSRule( shapeSelector, 'bottom', shapeProps.bottom );
  2976. // Y offset
  2977. if ( '' === top ) {
  2978. this.updateCSSRule( layerSelector, position, '0' );
  2979. } else {
  2980. this.updateCSSRule( layerSelector, position, top + unitValue );
  2981. }
  2982. },
  2983. previewShapeAlign: function( args, e ) {
  2984. // Let width and height preview do the work.
  2985. var prefix = args.preview.prefix,
  2986. widthField = args.form.find('[name="' + prefix + 'size_width"]');
  2987. widthField.trigger('input');
  2988. },
  2989. /**
  2990. * Process the fill style when toggled
  2991. *
  2992. * @param {Object} args - a collection of helper references setup on field init
  2993. * @param Event e - the event passed by the event listener
  2994. * @return void
  2995. */
  2996. previewShapeFillStyle: function( args, e ) {
  2997. var value = args.input.val(),
  2998. preview = args.preview,
  2999. prefix = args.preview.prefix,
  3000. linearGradientId = 'fl-row-' + args.nodeID + '-' + prefix + '-linear-gradient',
  3001. radialGradientId = 'fl-row-' + args.nodeID + '-' + prefix + '-radial-gradient',
  3002. patternId = 'fl-row-' + args.nodeID + '-' + prefix + '-pattern',
  3003. form = args.form;
  3004. if ( 'undefined' !== typeof value ) {
  3005. var selector = this._getPreviewSelector( this.classes.node, preview.selector );
  3006. switch( value ) {
  3007. case 'color':
  3008. var colorValue = form.find('[name=' + prefix + 'fill_color]').val();
  3009. this.updateCSSRule( selector, 'fill', FLBuilderPreview.formatColor( colorValue ) );
  3010. break;
  3011. case 'gradient':
  3012. var gradientField = form.find('#fl-field-' + prefix + 'fill_gradient'),
  3013. gradientType = gradientField.find('select[name$="[type]"]').val();
  3014. var gradientId = 'radial' === gradientType ? radialGradientId : linearGradientId ;
  3015. this.updateCSSRule( selector, 'fill', 'url(#' + gradientId + ')' );
  3016. break;
  3017. case 'pattern':
  3018. var fill = 'url(#' + patternId + ')';
  3019. this.updateCSSRule( selector, 'fill', fill );
  3020. }
  3021. }
  3022. },
  3023. /**
  3024. * Process the gradient control values
  3025. *
  3026. * @param {Object} args - a collection of helper references setup on field init
  3027. * @param Event e - the event passed by the event listener
  3028. * @return void
  3029. */
  3030. previewShapeGradientFill: function( args, e ) {
  3031. var values = args.getValues(),
  3032. node = args.node,
  3033. preview = args.preview,
  3034. layerSelector = '.fl-builder-' + preview.position + '-edge-layer',
  3035. gradientDef = node.find( layerSelector + ' ' + values.type + 'Gradient' ),
  3036. fill = 'url(#' + gradientDef.attr('id') + ')',
  3037. shapeSelector = this._getPreviewSelector( this.classes.node, layerSelector + ' .fl-shape' );
  3038. this.updateCSSRule( shapeSelector, 'fill', fill );
  3039. // Set stops
  3040. var stopEls = gradientDef.find('stop');
  3041. for( var i in values.stops ) {
  3042. var stopVal = values.stops[i],
  3043. stop = stopEls.eq(i),
  3044. color = stopVal.color,
  3045. offset = stopVal.stop,
  3046. opacity = 1;
  3047. if ( color.indexOf( 'rgba' ) === 0 ) {
  3048. var rawValues = color.substring( color.indexOf('(') + 1, color.lastIndexOf(')') ).split( /,\s*/ );
  3049. opacity = rawValues.pop();
  3050. color = 'rgb(' + rawValues.join(',') + ')';
  3051. }
  3052. stop.attr('stop-color', FLBuilderPreview.formatColor( color ) );
  3053. stop.attr('stop-opacity', opacity );
  3054. stop.attr('offset', offset + '%' );
  3055. }
  3056. // Set Angle
  3057. if ( 'linear' === values.type && 'undefined' !== typeof gradientDef[0] ) {
  3058. gradientDef[0].setAttribute( 'gradientTransform', 'rotate(' + values.angle + ' .5 .5 )' );
  3059. }
  3060. // Set Position
  3061. if ( 'radial' === values.type ) {
  3062. // Split string by space
  3063. parts = values.position.split(' ');
  3064. var x = parts[0],
  3065. y = parts[1],
  3066. cx,
  3067. cy,
  3068. r;
  3069. switch( x ) {
  3070. case 'top':
  3071. case 'left':
  3072. cx = 0;
  3073. break;
  3074. case 'center':
  3075. cx = .5;
  3076. break;
  3077. case 'bottom':
  3078. case 'right':
  3079. cx = 1;
  3080. break;
  3081. }
  3082. switch( y ) {
  3083. case 'top':
  3084. case 'left':
  3085. cy = 0;
  3086. break;
  3087. case 'center':
  3088. cy = .5;
  3089. break;
  3090. case 'bottom':
  3091. case 'right':
  3092. cy = 1;
  3093. break;
  3094. }
  3095. r = .5;
  3096. if ( cx !== .5 || cy !== .5 ) r = 1;
  3097. gradientDef.attr( 'cx', cx );
  3098. gradientDef.attr( 'cy', cy );
  3099. gradientDef.attr( 'r', r );
  3100. }
  3101. },
  3102. /**
  3103. * Process the transform control values
  3104. *
  3105. * @param {Object} args - a collection of helper references setup on field init
  3106. * @param Event e - the event passed by the event listener
  3107. * @return void
  3108. */
  3109. previewShapeTransform: function ( args, e ) {
  3110. var form = args.form,
  3111. preview = args.preview,
  3112. prefix = preview.prefix,
  3113. layerSelector = this._getPreviewSelector( this.classes.node, preview.selector ),
  3114. shapeSelector = layerSelector + ' > *',
  3115. values = args.getValues(),
  3116. shapeTransforms = [];
  3117. Object.keys( values ).map( function( prop ) {
  3118. var value = values[prop];
  3119. var unit = '',
  3120. sign = '';
  3121. switch( prop ) {
  3122. case 'scaleXSign':
  3123. case 'scaleYSign':
  3124. return;
  3125. case 'scaleX':
  3126. case 'scaleY':
  3127. if ( !value || '' === value || 0 === value ) value = '1';
  3128. sign = 'scaleX' === prop ? values['scaleXSign'] : values['scaleYSign'] ; // Positive or negative?
  3129. if ( 'invert' === sign ) {
  3130. value = -Math.abs( value );
  3131. } else {
  3132. value = Math.abs( value );
  3133. }
  3134. shapeTransforms.push( prop + '(' + value + ')' ); // scale has no unit
  3135. break;
  3136. case 'translateX':
  3137. case 'translateY':
  3138. if ( value ) {
  3139. unit = 'px';
  3140. shapeTransforms.push( prop + '(' + value + unit + ')' );
  3141. }
  3142. break;
  3143. case 'skewX':
  3144. case 'skewY':
  3145. if ( value ) {
  3146. unit = 'deg';
  3147. shapeTransforms.push( prop + '(' + value + unit + ')' );
  3148. }
  3149. break;
  3150. case 'rotate':
  3151. unit = 'deg';
  3152. if ( value !== '' && value !== '0' ) {
  3153. shapeTransforms.push( 'rotate(' + value + unit + ')' );
  3154. }
  3155. break;
  3156. }
  3157. } );
  3158. this.updateCSSRule( shapeSelector, 'transform', shapeTransforms.join(' ') );
  3159. },
  3160. }
  3161. })(jQuery);