form-components.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. var FormComponents = function () {
  2. var handleWysihtml5 = function () {
  3. if (!jQuery().wysihtml5) {
  4. return;
  5. }
  6. if ($('.wysihtml5').size() > 0) {
  7. $('.wysihtml5').wysihtml5({
  8. "stylesheets": ["assets/plugins/bootstrap-wysihtml5/wysiwyg-color.css"]
  9. });
  10. }
  11. }
  12. var resetWysihtml5 = function () {
  13. if (!jQuery().wysihtml5) {
  14. return;
  15. }
  16. if ($('.wysihtml5').size() > 0) {
  17. $('.wysihtml5').wysihtml5({
  18. "stylesheets": ["assets/plugins/bootstrap-wysihtml5/wysiwyg-color.css"]
  19. });
  20. }
  21. }
  22. var handleToggleButtons = function () {
  23. if (!jQuery().toggleButtons) {
  24. return;
  25. }
  26. $('.basic-toggle-button').toggleButtons();
  27. $('.text-toggle-button').toggleButtons({
  28. width: 200,
  29. label: {
  30. enabled: "Lorem Ipsum",
  31. disabled: "Dolor Sit"
  32. }
  33. });
  34. $('.danger-toggle-button').toggleButtons({
  35. style: {
  36. // Accepted values ["primary", "danger", "info", "success", "warning"] or nothing
  37. enabled: "danger",
  38. disabled: "info"
  39. }
  40. });
  41. $('.info-toggle-button').toggleButtons({
  42. style: {
  43. enabled: "info",
  44. disabled: ""
  45. }
  46. });
  47. $('.success-toggle-button').toggleButtons({
  48. style: {
  49. enabled: "success",
  50. disabled: "info"
  51. }
  52. });
  53. $('.warning-toggle-button').toggleButtons({
  54. style: {
  55. enabled: "warning",
  56. disabled: "info"
  57. }
  58. });
  59. $('.height-toggle-button').toggleButtons({
  60. height: 100,
  61. font: {
  62. 'line-height': '100px',
  63. 'font-size': '20px',
  64. 'font-style': 'italic'
  65. }
  66. });
  67. }
  68. var handleTagsInput = function () {
  69. if (!jQuery().tagsInput) {
  70. return;
  71. }
  72. $('#tags_1').tagsInput({
  73. width: 'auto',
  74. 'onAddTag': function () {
  75. //alert(1);
  76. },
  77. });
  78. $('#tags_2').tagsInput({
  79. width: 240
  80. });
  81. }
  82. var handlejQueryUIDatePickers = function () {
  83. $( ".ui-date-picker" ).datepicker();
  84. }
  85. var handleDatePickers = function () {
  86. if (jQuery().datepicker) {
  87. $('.date-picker').datepicker({
  88. rtl : App.isRTL()
  89. });
  90. }
  91. }
  92. var handleTimePickers = function () {
  93. if (jQuery().timepicker) {
  94. $('.timepicker-default').timepicker();
  95. $('.timepicker-24').timepicker({
  96. minuteStep: 1,
  97. showSeconds: true,
  98. showMeridian: false
  99. });
  100. }
  101. }
  102. var handleDateRangePickers = function () {
  103. if (!jQuery().daterangepicker) {
  104. return;
  105. }
  106. $('.date-range').daterangepicker(
  107. {
  108. opens: (App.isRTL() ? 'left' : 'right'),
  109. format: 'MM/dd/yyyy',
  110. separator: ' to ',
  111. startDate: Date.today().add({
  112. days: -29
  113. }),
  114. endDate: Date.today(),
  115. minDate: '01/01/2012',
  116. maxDate: '12/31/2014',
  117. }
  118. );
  119. $('#form-date-range').daterangepicker({
  120. ranges: {
  121. 'Today': ['today', 'today'],
  122. 'Yesterday': ['yesterday', 'yesterday'],
  123. 'Last 7 Days': [Date.today().add({
  124. days: -6
  125. }), 'today'],
  126. 'Last 29 Days': [Date.today().add({
  127. days: -29
  128. }), 'today'],
  129. 'This Month': [Date.today().moveToFirstDayOfMonth(), Date.today().moveToLastDayOfMonth()],
  130. 'Last Month': [Date.today().moveToFirstDayOfMonth().add({
  131. months: -1
  132. }), Date.today().moveToFirstDayOfMonth().add({
  133. days: -1
  134. })]
  135. },
  136. opens: (App.isRTL() ? 'left' : 'right'),
  137. format: 'MM/dd/yyyy',
  138. separator: ' to ',
  139. startDate: Date.today().add({
  140. days: -29
  141. }),
  142. endDate: Date.today(),
  143. minDate: '01/01/2012',
  144. maxDate: '12/31/2014',
  145. locale: {
  146. applyLabel: 'Submit',
  147. fromLabel: 'From',
  148. toLabel: 'To',
  149. customRangeLabel: 'Custom Range',
  150. daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  151. monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  152. firstDay: 1
  153. },
  154. showWeekNumbers: true,
  155. buttonClasses: ['btn-danger']
  156. },
  157. function (start, end) {
  158. $('#form-date-range span').html(start.toString('MMMM d, yyyy') + ' - ' + end.toString('MMMM d, yyyy'));
  159. });
  160. $('#form-date-range span').html(Date.today().add({
  161. days: -29
  162. }).toString('MMMM d, yyyy') + ' - ' + Date.today().toString('MMMM d, yyyy'));
  163. //modal version:
  164. $('#form-date-range-modal').daterangepicker({
  165. ranges: {
  166. 'Today': ['today', 'today'],
  167. 'Yesterday': ['yesterday', 'yesterday'],
  168. 'Last 7 Days': [Date.today().add({
  169. days: -6
  170. }), 'today'],
  171. 'Last 29 Days': [Date.today().add({
  172. days: -29
  173. }), 'today'],
  174. 'This Month': [Date.today().moveToFirstDayOfMonth(), Date.today().moveToLastDayOfMonth()],
  175. 'Last Month': [Date.today().moveToFirstDayOfMonth().add({
  176. months: -1
  177. }), Date.today().moveToFirstDayOfMonth().add({
  178. days: -1
  179. })]
  180. },
  181. opens: (App.isRTL() ? 'left' : 'right'),
  182. format: 'MM/dd/yyyy',
  183. separator: ' to ',
  184. startDate: Date.today().add({
  185. days: -29
  186. }),
  187. endDate: Date.today(),
  188. minDate: '01/01/2012',
  189. maxDate: '12/31/2014',
  190. locale: {
  191. applyLabel: 'Submit',
  192. fromLabel: 'From',
  193. toLabel: 'To',
  194. customRangeLabel: 'Custom Range',
  195. daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  196. monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  197. firstDay: 1
  198. },
  199. showWeekNumbers: true,
  200. buttonClasses: ['btn-danger']
  201. },
  202. function (start, end) {
  203. $('#form-date-range-modal span').html(start.toString('MMMM d, yyyy') + ' - ' + end.toString('MMMM d, yyyy'));
  204. });
  205. $('#form-date-range-modal span').html(Date.today().add({
  206. days: -29
  207. }).toString('MMMM d, yyyy') + ' - ' + Date.today().toString('MMMM d, yyyy'));
  208. }
  209. var handleDatetimePicker = function () {
  210. $(".form_datetime").datetimepicker({
  211. format: "dd MM yyyy - hh:ii",
  212. pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left")
  213. });
  214. $(".form_advance_datetime").datetimepicker({
  215. format: "dd MM yyyy - hh:ii",
  216. autoclose: true,
  217. todayBtn: true,
  218. startDate: "2013-02-14 10:00",
  219. pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left"),
  220. minuteStep: 10
  221. });
  222. $(".form_meridian_datetime").datetimepicker({
  223. format: "dd MM yyyy - HH:ii P",
  224. showMeridian: true,
  225. autoclose: true,
  226. pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left"),
  227. todayBtn: true
  228. });
  229. }
  230. var handleClockfaceTimePickers = function () {
  231. if (!jQuery().clockface) {
  232. return;
  233. }
  234. $('.clockface_1').clockface();
  235. $('#clockface_2').clockface({
  236. format: 'HH:mm',
  237. trigger: 'manual'
  238. });
  239. $('#clockface_2_toggle').click(function (e) {
  240. e.stopPropagation();
  241. $('#clockface_2').clockface('toggle');
  242. });
  243. $('#clockface_2_modal').clockface({
  244. format: 'HH:mm',
  245. trigger: 'manual'
  246. });
  247. $('#clockface_2_modal_toggle').click(function (e) {
  248. e.stopPropagation();
  249. $('#clockface_2_modal').clockface('toggle');
  250. });
  251. $('.clockface_3').clockface({
  252. format: 'H:mm'
  253. }).clockface('show', '14:30');
  254. }
  255. var handleColorPicker = function () {
  256. if (!jQuery().colorpicker) {
  257. return;
  258. }
  259. $('.colorpicker-default').colorpicker({
  260. format: 'hex'
  261. });
  262. $('.colorpicker-rgba').colorpicker();
  263. }
  264. var handleSelec2 = function () {
  265. $('#select2_sample1').select2({
  266. placeholder: "Select an option",
  267. allowClear: true
  268. });
  269. $('#select2_sample2').select2({
  270. placeholder: "Select a State",
  271. allowClear: true
  272. });
  273. $("#select2_sample3").select2({
  274. allowClear: true,
  275. minimumInputLength: 1,
  276. query: function (query) {
  277. var data = {
  278. results: []
  279. }, i, j, s;
  280. for (i = 1; i < 5; i++) {
  281. s = "";
  282. for (j = 0; j < i; j++) {
  283. s = s + query.term;
  284. }
  285. data.results.push({
  286. id: query.term + i,
  287. text: s
  288. });
  289. }
  290. query.callback(data);
  291. }
  292. });
  293. function format(state) {
  294. if (!state.id) return state.text; // optgroup
  295. return "<img class='flag' src='assets/img/flags/" + state.id.toLowerCase() + ".png'/>&nbsp;&nbsp;" + state.text;
  296. }
  297. $("#select2_sample4").select2({
  298. allowClear: true,
  299. formatResult: format,
  300. formatSelection: format,
  301. escapeMarkup: function (m) {
  302. return m;
  303. }
  304. });
  305. $("#select2_sample5").select2({
  306. tags: ["red", "green", "blue", "yellow", "pink"]
  307. });
  308. function movieFormatResult(movie) {
  309. var markup = "<table class='movie-result'><tr>";
  310. if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
  311. markup += "<td valign='top'><img src='" + movie.posters.thumbnail + "'/></td>";
  312. }
  313. markup += "<td valign='top'><h5>" + movie.title + "</h5>";
  314. if (movie.critics_consensus !== undefined) {
  315. markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
  316. } else if (movie.synopsis !== undefined) {
  317. markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
  318. }
  319. markup += "</td></tr></table>"
  320. return markup;
  321. }
  322. function movieFormatSelection(movie) {
  323. return movie.title;
  324. }
  325. $("#select2_sample6").select2({
  326. placeholder: "Search for a movie",
  327. minimumInputLength: 1,
  328. ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
  329. url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
  330. dataType: 'jsonp',
  331. data: function (term, page) {
  332. return {
  333. q: term, // search term
  334. page_limit: 10,
  335. apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
  336. };
  337. },
  338. results: function (data, page) { // parse the results into the format expected by Select2.
  339. // since we are using custom formatting functions we do not need to alter remote JSON data
  340. return {
  341. results: data.movies
  342. };
  343. }
  344. },
  345. initSelection: function (element, callback) {
  346. // the input tag has a value attribute preloaded that points to a preselected movie's id
  347. // this function resolves that id attribute to an object that select2 can render
  348. // using its formatResult renderer - that way the movie name is shown preselected
  349. var id = $(element).val();
  350. if (id !== "") {
  351. $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/" + id + ".json", {
  352. data: {
  353. apikey: "ju6z9mjyajq2djue3gbvv26t"
  354. },
  355. dataType: "jsonp"
  356. }).done(function (data) {
  357. callback(data);
  358. });
  359. }
  360. },
  361. formatResult: movieFormatResult, // omitted for brevity, see the source of this page
  362. formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
  363. dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
  364. escapeMarkup: function (m) {
  365. return m;
  366. } // we do not want to escape markup since we are displaying html in results
  367. });
  368. }
  369. var handleMultiSelect = function () {
  370. $('#my_multi_select1').multiSelect();
  371. $('#my_multi_select2').multiSelect({
  372. selectableOptgroup: true
  373. });
  374. }
  375. var handleInputMasks = function () {
  376. $.extend($.inputmask.defaults, {
  377. 'autounmask': true
  378. });
  379. $("#mask_date").inputmask("d/m/y", {autoUnmask: true}); //direct mask
  380. $("#mask_date1").inputmask("d/m/y",{ "placeholder": "*"}); //change the placeholder
  381. $("#mask_date2").inputmask("d/m/y",{ "placeholder": "dd/mm/yyyy" }); //multi-char placeholder
  382. $("#mask_phone").inputmask("mask", {"mask": "(999) 999-9999"}); //specifying fn & options
  383. $("#mask_tin").inputmask({"mask": "99-9999999"}); //specifying options only
  384. $("#mask_number").inputmask({ "mask": "9", "repeat": 10, "greedy": false }); // ~ mask "9" or mask "99" or ... mask "9999999999"
  385. $("#mask_decimal").inputmask('decimal', { rightAlignNumerics: false }); //disables the right alignment of the decimal input
  386. $("#mask_currency").inputmask('€ 999.999.999,99', { numericInput: true }); //123456 => € ___.__1.234,56
  387. $("#mask_currency2").inputmask('€ 999,999,999.99', { numericInput: true, rightAlignNumerics: false, greedy: false}); //123456 => € ___.__1.234,56
  388. $("#mask_ssn").inputmask("999-99-9999", {placeholder:" ", clearMaskOnLostFocus: true }); //default
  389. }
  390. var handleIPAddressInput = function () {
  391. $('#input_ipv4').ipAddress();
  392. $('#input_ipv6').ipAddress({v:6});
  393. }
  394. return {
  395. //main function to initiate the module
  396. init: function () {
  397. handleWysihtml5();
  398. handleToggleButtons();
  399. handleTagsInput();
  400. handlejQueryUIDatePickers();
  401. handleDatePickers();
  402. handleTimePickers();
  403. handleDatetimePicker();
  404. handleDateRangePickers();
  405. handleClockfaceTimePickers();
  406. handleColorPicker();
  407. handleSelec2();
  408. handleInputMasks();
  409. handleIPAddressInput();
  410. handleMultiSelect();
  411. App.addResponsiveHandler(function(){
  412. resetWysihtml5();
  413. })
  414. }
  415. };
  416. }();