sab-preview.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. wp.SAB = 'undefined' === typeof( wp.SAB ) ? {} : wp.SAB;
  2. wp.SAB.views = 'undefined' === typeof( wp.SAB.views ) ? {} : wp.SAB.views;
  3. wp.SAB.models = 'undefined' === typeof( wp.SAB.models ) ? {} : wp.SAB.models;
  4. wp.SAB.contructors = 'undefined' === typeof( wp.SAB.contructors ) ? [] : wp.SAB.contructors;
  5. wp.SAB.models.Settings = Backbone.Model.extend({
  6. initialize: function(){
  7. var model = this;
  8. var view = new wp.SAB.views.Settings({
  9. model: this,
  10. el: jQuery( '#sabox-container' )
  11. });
  12. this.set( 'view', view );
  13. },
  14. getAttribute: function( type ){
  15. var value = this.get( type );
  16. if ( 'undefined' == typeof value ) {
  17. value = jQuery( '#' + type ).val();
  18. }
  19. return value;
  20. }
  21. });
  22. wp.SAB.views.Settings = Backbone.View.extend({
  23. events: {
  24. // Settings specific events
  25. 'keyup input': 'updateModel',
  26. 'keyup textarea': 'updateModel',
  27. 'change input': 'updateModel',
  28. 'change textarea': 'updateModel',
  29. 'blur textarea': 'updateModel',
  30. 'change select': 'updateModel',
  31. },
  32. initialize: function( args ) {
  33. // Check for Google Fonts
  34. this.checkGoogleFonts();
  35. this.listenTo( this.model, 'change:sab_email', this.emailVisibility );
  36. // Author website
  37. this.listenTo( this.model, 'change:sab_web', this.websiteVisibility );
  38. this.listenTo( this.model, 'change:sab_web_position', this.websitePosition );
  39. // Social Icons
  40. this.listenTo( this.model, 'change:sab_hide_socials', this.socialsVisibility );
  41. this.listenTo( this.model, 'change:sab_colored', this.socialIconTypeVisibility );
  42. this.listenTo( this.model, 'change:sab_icons_style', this.socialIconTypeVisibility );
  43. this.listenTo( this.model, 'change:sab_social_hover', this.socialIconHover );
  44. this.listenTo( this.model, 'change:sab_box_long_shadow', this.socialIconShadow );
  45. this.listenTo( this.model, 'change:sab_box_thin_border', this.socialIconBorder );
  46. // Avatar
  47. this.listenTo( this.model, 'change:sab_avatar_style', this.avatarStyle );
  48. this.listenTo( this.model, 'change:sab_avatar_hover', this.avatarHover );
  49. // Padding
  50. this.listenTo( this.model, 'change:sab_box_padding_top_bottom', this.adjustPadding );
  51. this.listenTo( this.model, 'change:sab_box_padding_left_right', this.adjustPadding );
  52. // Author Box Border
  53. this.listenTo( this.model, 'change:sab_box_border_width', this.adjustBorder );
  54. this.listenTo( this.model, 'change:sab_box_border', this.adjustBorder );
  55. // Adjust Author name settings
  56. this.listenTo( this.model, 'change:sab_box_name_size', this.adjustAuthorName );
  57. this.listenTo( this.model, 'change:sab_box_name_font', this.adjustAuthorName );
  58. this.listenTo( this.model, 'change:sab_box_author_color', this.adjustAuthorName );
  59. // Adjust Author website settings
  60. this.listenTo( this.model, 'change:sab_box_web_font', this.adjustAuthorWebsite );
  61. this.listenTo( this.model, 'change:sab_box_web_size', this.adjustAuthorWebsite );
  62. this.listenTo( this.model, 'change:sab_box_web_color', this.adjustAuthorWebsite );
  63. // Adjust Author description settings
  64. this.listenTo( this.model, 'change:sab_box_desc_font', this.adjustAuthorDescription );
  65. this.listenTo( this.model, 'change:sab_box_desc_size', this.adjustAuthorDescription );
  66. this.listenTo( this.model, 'change:sab_box_author_p_color', this.adjustAuthorDescription );
  67. this.listenTo( this.model, 'change:sab_box_author_a_color', this.adjustAuthorDescription );
  68. this.listenTo( this.model, 'change:sab_desc_style', this.adjustAuthorDescription );
  69. // Icon Size
  70. this.listenTo( this.model, 'change:sab_box_icon_size', this.adjustIconSize );
  71. // Social Bar Background Color
  72. this.listenTo( this.model, 'change:sab_box_icons_back', this.changeSocialBarBackground );
  73. // Author Box Background Color
  74. this.listenTo( this.model, 'change:sab_box_author_back', this.changeAuthorBoxBackground );
  75. // Author Box Background Color
  76. this.listenTo( this.model, 'change:sab_box_icons_color', this.changeSocialIconsColor );
  77. },
  78. emailVisibility: function() {
  79. var showEmail = wp.SAB.Settings.get( 'sab_email' );
  80. if ( '1' == showEmail ) {
  81. jQuery('.sab-user_email').parent().show();
  82. }else{
  83. jQuery('.sab-user_email').parent().hide();
  84. }
  85. },
  86. socialsVisibility: function(){
  87. var hideSocials = wp.SAB.Settings.get( 'sab_hide_socials' );
  88. if ( '1' == hideSocials ) {
  89. jQuery('.saboxplugin-socials').hide();
  90. }else{
  91. jQuery('.saboxplugin-socials').show();
  92. }
  93. },
  94. websiteVisibility: function(){
  95. var showWesite = wp.SAB.Settings.get( 'sab_web' );
  96. if ( '1' == showWesite ) {
  97. jQuery('.saboxplugin-web').show();
  98. }else{
  99. jQuery('.saboxplugin-web').hide();
  100. }
  101. },
  102. websitePosition: function() {
  103. var attribute = wp.SAB.Settings.get( 'sab_web_position' );
  104. if ( '1' == attribute ) {
  105. jQuery('.saboxplugin-web').addClass( 'sab-web-position' );
  106. }else{
  107. jQuery('.saboxplugin-web').removeClass( 'sab-web-position' );
  108. }
  109. },
  110. socialIconTypeVisibility: function() {
  111. var iconType = wp.SAB.Settings.getAttribute( 'sab_colored' ),
  112. iconStyle = wp.SAB.Settings.getAttribute( 'sab_icons_style' );
  113. jQuery('.saboxplugin-socials').removeClass( 'sab-show-simple sab-show-circle sab-show-square' );
  114. if ( '1' == iconType ) {
  115. if ( '1' == iconStyle ) {
  116. jQuery('.saboxplugin-socials').addClass( 'sab-show-circle' );
  117. }else{
  118. jQuery('.saboxplugin-socials').addClass( 'sab-show-square' );
  119. }
  120. }else{
  121. jQuery('.saboxplugin-socials').addClass( 'sab-show-simple' );
  122. }
  123. },
  124. socialIconHover: function() {
  125. var attribute = wp.SAB.Settings.get( 'sab_social_hover' );
  126. if ( '1' == attribute ) {
  127. jQuery('.saboxplugin-socials').addClass( 'sab-rotate-icons' );
  128. }else{
  129. jQuery('.saboxplugin-socials').removeClass( 'sab-rotate-icons' );
  130. }
  131. },
  132. socialIconShadow: function() {
  133. var attribute = wp.SAB.Settings.get( 'sab_box_long_shadow' );
  134. if ( '1' == attribute ) {
  135. jQuery('.saboxplugin-socials').removeClass( 'without-long-shadow' );
  136. }else{
  137. jQuery('.saboxplugin-socials').addClass( 'without-long-shadow' );
  138. }
  139. },
  140. socialIconBorder: function() {
  141. var attribute = wp.SAB.Settings.get( 'sab_box_thin_border' );
  142. if ( '1' == attribute ) {
  143. jQuery('.saboxplugin-socials').addClass( 'sab-icons-with-border' );
  144. }else{
  145. jQuery('.saboxplugin-socials').removeClass( 'sab-icons-with-border' );
  146. }
  147. },
  148. avatarStyle: function() {
  149. var attribute = wp.SAB.Settings.get( 'sab_avatar_style' );
  150. if ( '1' == attribute ) {
  151. jQuery('.saboxplugin-gravatar').addClass( 'sab-round-image' );
  152. }else{
  153. jQuery('.saboxplugin-gravatar').removeClass( 'sab-round-image' );
  154. }
  155. },
  156. avatarHover: function() {
  157. var attribute = wp.SAB.Settings.get( 'sab_avatar_hover' );
  158. if ( '1' == attribute ) {
  159. jQuery('.saboxplugin-gravatar').addClass( 'sab-rotate-img' );
  160. }else{
  161. jQuery('.saboxplugin-gravatar').removeClass( 'sab-rotate-img' );
  162. }
  163. },
  164. adjustPadding: function() {
  165. var paddingTopBottom = wp.SAB.Settings.getAttribute( 'sab_box_padding_top_bottom' ),
  166. paddingLeftRight = wp.SAB.Settings.getAttribute( 'sab_box_padding_left_right' );
  167. jQuery( '.saboxplugin-wrap' ).css({ 'padding' : paddingTopBottom + ' ' + paddingLeftRight });
  168. },
  169. adjustBorder: function() {
  170. var border = wp.SAB.Settings.getAttribute( 'sab_box_border_width' ),
  171. borderColor = wp.SAB.Settings.getAttribute( 'sab_box_border' );
  172. if ( '' == borderColor ) {
  173. borderColor = 'inherit';
  174. }
  175. jQuery( '.saboxplugin-wrap' ).css({ 'border-width' : border, 'border-color' : borderColor });
  176. jQuery( '.saboxplugin-wrap .saboxplugin-socials' ).css({ 'border-width' : border, 'border-color' : borderColor });
  177. },
  178. adjustAuthorName: function() {
  179. var font = wp.SAB.Settings.getAttribute( 'sab_box_name_font' ),
  180. size = wp.SAB.Settings.getAttribute( 'sab_box_name_size' ),
  181. color = wp.SAB.Settings.getAttribute( 'sab_box_author_color' ),
  182. lineHeight = parseInt( size ) + 7;
  183. if ( '' == color ) {
  184. color = 'inherit';
  185. }
  186. if ( '' == font || 'None' == font ) {
  187. font = 'inherit';
  188. }else{
  189. this.loadGoogleFonts( font );
  190. }
  191. jQuery( '.saboxplugin-wrap .saboxplugin-authorname a' ).css({ 'font-family' : font, 'color' : color, 'font-size': size, 'line-height' : lineHeight.toString() + 'px' });
  192. },
  193. adjustAuthorWebsite: function() {
  194. var font = wp.SAB.Settings.getAttribute( 'sab_box_web_font' ),
  195. size = wp.SAB.Settings.getAttribute( 'sab_box_web_size' ),
  196. color = wp.SAB.Settings.getAttribute( 'sab_box_web_color' ),
  197. lineHeight = parseInt( size ) + 7;
  198. if ( '' == color ) {
  199. color = 'inherit';
  200. }
  201. if ( '' == font || 'None' == font ) {
  202. font = 'inherit';
  203. }else{
  204. this.loadGoogleFonts( font );
  205. }
  206. jQuery( '.saboxplugin-wrap .saboxplugin-web a' ).css({ 'font-family' : font, 'color' : color, 'font-size': size, 'line-height' : lineHeight.toString() + 'px' });
  207. },
  208. adjustAuthorDescription: function() {
  209. var font = wp.SAB.Settings.getAttribute( 'sab_box_desc_font' ),
  210. size = wp.SAB.Settings.getAttribute( 'sab_box_desc_size' ),
  211. color = wp.SAB.Settings.getAttribute( 'sab_box_author_p_color' ),
  212. link_color = wp.SAB.Settings.getAttribute( 'sab_box_author_a_color' ),
  213. style = wp.SAB.Settings.getAttribute( 'sab_desc_style' ),
  214. lineHeight = parseInt( size ) + 7;
  215. if ( '' == color ) {
  216. color = 'inherit';
  217. }
  218. if ( '' == link_color ) {
  219. link_color = 'inherit';
  220. }
  221. if ( '' == font || 'None' == font ) {
  222. font = 'inherit';
  223. }else{
  224. this.loadGoogleFonts( font );
  225. }
  226. if ( '0' == style ) {
  227. style = 'normal';
  228. }else{
  229. style = 'italic';
  230. }
  231. jQuery( '.saboxplugin-wrap .saboxplugin-desc p, .saboxplugin-wrap .saboxplugin-desc' ).css({ 'font-family' : font, 'color' : color, 'font-size': size, 'line-height' : lineHeight.toString() + 'px', 'font-style' : style });
  232. jQuery( '.saboxplugin-wrap .saboxplugin-desc a' ).css({ 'font-family' : font, 'color' : link_color, 'font-size': size, 'line-height' : lineHeight.toString() + 'px', 'font-style' : style });
  233. },
  234. adjustIconSize: function() {
  235. var size = this.model.get( 'sab_box_icon_size' ),
  236. size2x = parseInt( size ) * 2;
  237. jQuery( '.saboxplugin-wrap .saboxplugin-socials a.saboxplugin-icon-grey svg' ).css({ 'width' : size, 'height' : size });
  238. jQuery( '.saboxplugin-wrap .saboxplugin-socials a.saboxplugin-icon-color svg' ).css({ 'width' : size2x.toString() + 'px', 'height' : size2x.toString() + 'px' });
  239. },
  240. changeSocialBarBackground: function() {
  241. var color = this.model.get( 'sab_box_icons_back' );
  242. if ( '' == color ) {
  243. color = 'inherit';
  244. }
  245. jQuery( '.saboxplugin-wrap .saboxplugin-socials' ).css({ 'background-color' : color });
  246. },
  247. changeAuthorBoxBackground: function() {
  248. var color = this.model.get( 'sab_box_author_back' );
  249. if ( '' == color ) {
  250. color = 'inherit';
  251. }
  252. jQuery( '.saboxplugin-wrap' ).css({ 'background-color' : color });
  253. },
  254. changeSocialIconsColor: function() {
  255. var color = this.model.get( 'sab_box_icons_color' );
  256. if ( '' == color ) {
  257. color = 'inherit';
  258. }
  259. jQuery( '.saboxplugin-wrap .saboxplugin-socials a.saboxplugin-icon-grey svg' ).css({ 'color' : color });
  260. },
  261. updateModel: function( event ) {
  262. var value, setting;
  263. // Check if the target has a data-field. If not, it's not a model value we want to store
  264. if ( undefined === event.target.id ) {
  265. return;
  266. }
  267. // Update the model's value, depending on the input type
  268. if ( event.target.type == 'checkbox' ) {
  269. value = ( event.target.checked ? '1' : '0' );
  270. } else {
  271. value = event.target.value;
  272. }
  273. // Update the model
  274. this.model.set( event.target.id, value );
  275. },
  276. checkGoogleFonts: function() {
  277. var authorFont = this.model.getAttribute( 'sab_box_name_font' ),
  278. webFont = this.model.getAttribute( 'sab_box_web_font' ),
  279. descriptionFont = this.model.getAttribute( 'sab_box_desc_font' );
  280. if ( '' != authorFont && 'None' != authorFont ) {
  281. this.loadGoogleFonts( authorFont );
  282. }
  283. if ( '' != webFont && 'None' != webFont ) {
  284. this.loadGoogleFonts( webFont );
  285. }
  286. if ( '' != descriptionFont && 'None' != descriptionFont ) {
  287. this.loadGoogleFonts( descriptionFont );
  288. }
  289. },
  290. loadGoogleFonts: function( font ) {
  291. if ( ! wp.SAB.Fonts.includes( font ) ) {
  292. wp.SAB.Fonts.push( font );
  293. WebFont.load({
  294. google: {
  295. families: [ font ]
  296. }
  297. });
  298. }
  299. },
  300. });
  301. jQuery( document ).ready(function(){
  302. wp.SAB.Fonts = [];
  303. wp.SAB.Settings = new wp.SAB.models.Settings();
  304. });