Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. JSFormWizard: (function() {
  2. /**
  3. * This class is responsible for constructing a form from an object of
  4. * context variables and values. It knows how to map types of variables
  5. * to their respective form fields and how to show selected checkboxes
  6. * and values.
  7. *
  8. * authored by: Dayne Jones
  9. * April 2017
  10. */
  11. getColorPicker = function(name, context, selectedColor) {
  12. var colors = ['#FFFFFF', '#64B3DF', '#ACD373', '#EFD26C', '#FD7F54', '#E95050', '#766ACF', '#EC85CC', '#000000'];
  13. var color_picker_string = '';
  14. var customSelected = false;
  15.  
  16. if (selectedColor && colors.indexOf(selectedColor) === -1) {
  17. customSelected = true;
  18. }
  19.  
  20. var getIsChecked = function(i) {
  21. // determines whether an input should be checked
  22. return (colors[i] === selectedColor ? 'checked' : '');
  23. };
  24.  
  25. for (var i=0; i<colors.length; i++) {
  26. color_picker_string += '<label class="color-picker">' +
  27. '<input type="checkbox" data-type="color" name="' + name + '_color' +
  28. '" class="custom" value="' + colors[i] + '"' + getIsChecked(i) + '><span></span></label>';
  29. }
  30.  
  31. var getCustomSelectedColor = function() {
  32. // returns the custom color only if a custom color is selected
  33. return (customSelected ? selectedColor : '#000');
  34. };
  35.  
  36. var getCustomSelected = function() {
  37. // determines whether a custom color is selected
  38. return (customSelected ? 'checked' : '');
  39. };
  40.  
  41. color_picker_string += '<label class="color-picker">' +
  42. '<input type="checkbox" data-type="color" name="' + name + '_color' +
  43. '" class="custom custom-color" value="' + getCustomSelectedColor() + '"' +
  44. getCustomSelected() + '><span></span></label>' +
  45. '<div class="popover-box popover-arrow left">' +
  46. '<input type="text" data-type="color" class="new" value="#"></div></label>';
  47.  
  48. return color_picker_string;
  49. };
  50.  
  51. getTextField = function(name, context) {
  52. return '<fieldset class="text-fieldset">' +
  53. getTextFieldInner(name, context) +
  54. '</fieldset>';
  55. };
  56.  
  57. getFontOptions = function(value) {
  58. var fontOptions = {
  59. 'Arial, sans-serif': 'Arial',
  60. 'Comic Sans MS, cursive, sans-serif': 'Comic Sans',
  61. 'Courier New, Courier, monospace': 'Courier',
  62. 'Georgia, serif': 'Georgia',
  63. 'Impact, Charcoal, sans-serif': 'Impact',
  64. 'Lucida Sans Unicode, Lucida Grande, sans-serif': 'Lucida',
  65. 'Palatino Linotype, Palatino, serif': 'Palantino',
  66. 'Tahoma, Geneva, sans-serif': 'Tahoma',
  67. 'Times New Roman, Times, serif': 'Times New Roman',
  68. 'Trebuchet MS, Helvetica, sans-serif': 'Trebuchet',
  69. 'Verdana, Geneva, sans-serif': 'Verdana',
  70. 'inherit': 'Inherit From Your Page'
  71. };
  72.  
  73. var fontString = '';
  74. if (!value) {
  75. value = 'inherit';
  76. }
  77. for (var font in fontOptions) {
  78. if (fontOptions.hasOwnProperty(font)) {
  79. fontString = fontString + '<option ' + (value === font ? 'selected' : '' ) + ' value="' + font + '">' + fontOptions[font] + '</option>';
  80. }
  81. };
  82.  
  83. return fontString;
  84. };
  85.  
  86. getTextFieldInner = function(name, context) {
  87. var fontSize = context.style ? context.style['font-size'].replace('px', '') : '';
  88. var fontFamily = context.style ? context.style['font-family'] : '';
  89. var fontWeight = context.style ? context.style['font-weight'] : '';
  90. var fontStyle = context.style ? context.style['font-style'] : '';
  91. var textDecoration = context.style ? context.style['text-decoration'] : '';
  92. var selectedColor = context.style ? context.style.color : '';
  93. var displayName = getDisplayName(name);
  94. return '<h5 for="' + name + '">' + displayName + '</h5>' +
  95. '<input type="text" data-type="text" name="' + name + '" value="' + context.value + '">' +
  96. '<h5>Text Color</h5>' +
  97. getColorPicker(name, context, selectedColor) +
  98. '<h5 for="' + name + '_fontSize">Font Size</h5>' +
  99. '<input data-type="fontSize" type="number" min="1" max="100" name="' + name + '_fontSize" value="' + fontSize + '">' +
  100. '<h5 for="' + name + '_fontFamily">Font Family</h5>' +
  101. '<select data-type="fontFamily" name="' + name + '_fontFamily" value="' + fontFamily + '">' +
  102. getFontOptions(fontFamily) +
  103. '</select>' +
  104. '<h5>Font Style</h5>' +
  105. '<input type="checkbox" data-type="fontWeight" id="' + name + '_fontWeight"' + (fontWeight ? 'checked' : '' ) + ' name="' + name + '_fontWeight"' + (fontWeight ? 'checked' : '' ) + '>' +
  106. '<label class="font-stylelbl style-bold" for="' + name + '_fontWeight">B</label>' +
  107. '<input data-type="fontStyle" type="checkbox" id="' + name + '_fontStyle"' + (fontStyle ? 'checked' : '' ) + ' name="' + name + '_fontStyle"' + (fontStyle ? 'checked' : '' ) + '>' +
  108. '<label class="font-stylelbl style-italic" for="' + name + '_fontStyle">I</label>' +
  109. '<input type="checkbox" data-type="textDecoration" id="' + name + '_textDecoration"' + (textDecoration ? 'checked' : '' ) + ' name="' + name + '_textDecoration"' + (textDecoration ? 'checked' : '' ) + '>' +
  110. '<label class="font-stylelbl style-underline" for="' + name + '_textDecoration">U</label>';
  111. };
  112.  
  113. getButtonHrefField = function(name, context) {
  114. var button_id = name.replace('button_href_', '');
  115. return '<fieldset data-button-id="' + button_id + '" class="button-href-fieldset" id="id_' + name + '">' +
  116. '<h5>Link URL</h5>' +
  117. '<input type="text" data-type="text" name="' + name + '" value="' + context.value + '">' +
  118. '</fieldset>';
  119. };
  120.  
  121. getButtonClassField = function(name, context) {
  122. var displayName = getDisplayName(name);
  123. var button_id = name.replace('button_class_', '');
  124. var borderRadius = context.style && context.style['border-radius'] ? context.style['border-radius'].replace('px', '') : '0';
  125. var selectedColor = context.style ? context.style['background-color'] : '';
  126. return '<fieldset data-button-id="' + button_id + '" class="button-class-fieldset" id="id_' + name + '">' +
  127. '<input type="hidden" name="' + name + '" value="' + name.replace('_class', '') + '">' +
  128. '<h5>Rounded Corners</h5>' +
  129. '<input data-type="borderRadius" id="' + name + '_borderRadius" type="number"' +
  130. 'name="' + name + '_borderRadius" min="0" max="500" value="' + borderRadius + '" />' +
  131. '<h5>Background Color</h5>' +
  132. getColorPicker(name, context, selectedColor) +
  133. '</fieldset>';
  134. }
  135.  
  136. getButtonField = function(name, context) {
  137. return '<fieldset class="button-fieldset">' +
  138. getTextFieldInner(name, context) +
  139. '</fieldset>';
  140. };
  141.  
  142. getImageField = function(name, context) {
  143. var displayName = getDisplayName(name);
  144. var formAction = CONFIG.api_url + 'behavioral_targeting/campaign/' + Abandonment.campaign.id + '/upload_image';
  145.  
  146. return '<fieldset class="image-fieldset">' +
  147. '<h5 for="' + name + '">' + displayName + '</h5>' +
  148. '<input id="id_' + name + '_file" accept="image/*" class="hidden ajax-file-input" data-site-id="' + Api.getSiteId() + '"' +
  149. 'data-action="' + formAction + '" name="' + name + '_file" type="file">' +
  150. '<img class="ajax-image-tag" src="' + context.value + '">' +
  151. '<input class="ajax-picture-url" data-type="image" placeholder="Image URL" id="id_' + name + '" name="' + name + '" value="' + context.value + '" type="text">' +
  152. '<button data-input-id="id_' + name + '_file" class="btn-primary btn-large black ajax-image-upload">Upload Image</button>' +
  153. '</fieldset>';
  154. };
  155.  
  156. getVideoField = function(name, context) {
  157. var displayName = getDisplayName(name);
  158. return '<fieldset class="video-fieldset">' +
  159. '<h5 for="' + name + '">' + displayName + ' (youtube ID only)</h5>' +
  160. '<input type="text" data-type="video" name="' + name + '" placeholder="YouTube URL" value="' + context.value + '">' +
  161. '</fieldset>';
  162. };
  163.  
  164. getDisplayName = function(name) {
  165. return name.slice(0, 1).toUpperCase() + name.slice(1).split('_')[0] + ' value';
  166. };
  167.  
  168. getFormFields = function(extra_context, editor_view) {
  169. var fields = [],
  170. variable;
  171. for (variable in extra_context) {
  172. var field;
  173.  
  174. if (extra_context.hasOwnProperty(variable)) {
  175. if (variable.indexOf('text') !== -1) {
  176. field = getTextField(variable, extra_context[variable]);
  177. } else if (variable.indexOf('button_class') !== -1) {
  178. field = getButtonClassField(variable, extra_context[variable]);
  179. } else if (variable.indexOf('button_href') !== -1) {
  180. field = getButtonHrefField(variable, extra_context[variable]);
  181. } else if (variable.indexOf('button') !== -1) {
  182. field = getButtonField(variable, extra_context[variable]);
  183. } else if (variable.indexOf('image') !== -1) {
  184. field = getImageField(variable, extra_context[variable]);
  185. } else if (variable.indexOf('video') !== -1) {
  186. field = getVideoField(variable, extra_context[variable]);
  187. }
  188. fields.push(field);
  189. }
  190. }
  191.  
  192. return fields;
  193. };
  194.  
  195. return {
  196. getFormFields: getFormFields
  197. }
  198. }()),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement