main_builder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. AIRTIME = (function(AIRTIME) {
  2. var viewport,
  3. $lib,
  4. $libWrapper,
  5. $builder,
  6. $fs,
  7. widgetHeight,
  8. screenWidth,
  9. resizeTimeout,
  10. oBaseDatePickerSettings,
  11. oBaseTimePickerSettings,
  12. oRange,
  13. dateStartId = "#sb_date_start",
  14. timeStartId = "#sb_time_start",
  15. dateEndId = "#sb_date_end",
  16. timeEndId = "#sb_time_end",
  17. $toggleLib = $("<a id='sb_edit' class='btn btn-small' href='#' title='"+$.i18n._("Open library to add or remove content")+"'>"+$.i18n._("Add / Remove Content")+"</a>"),
  18. $libClose = $('<a />', {
  19. "class": "close-round",
  20. "href": "#",
  21. "id": "sb_lib_close"
  22. }),
  23. mod;
  24. if (AIRTIME.builderMain === undefined) {
  25. AIRTIME.builderMain = {};
  26. }
  27. mod = AIRTIME.builderMain;
  28. oBaseDatePickerSettings = {
  29. dateFormat: 'yy-mm-dd',
  30. //i18n_months, i18n_days_short are in common.js
  31. monthNames: i18n_months,
  32. dayNamesMin: i18n_days_short,
  33. onClick: function(sDate, oDatePicker) {
  34. $(this).datepicker( "setDate", sDate );
  35. },
  36. onClose: validateTimeRange
  37. };
  38. oBaseTimePickerSettings = {
  39. showPeriodLabels: false,
  40. showCloseButton: true,
  41. closeButtonText: $.i18n._("Done"),
  42. showLeadingZero: false,
  43. defaultTime: '0:00',
  44. hourText: $.i18n._("Hour"),
  45. minuteText: $.i18n._("Minute"),
  46. onClose: validateTimeRange
  47. };
  48. function setWidgetSize() {
  49. viewport = AIRTIME.utilities.findViewportDimensions();
  50. widgetHeight = viewport.height - 180;
  51. screenWidth = Math.floor(viewport.width - 40);
  52. var libTableHeight = widgetHeight - 175,
  53. builderTableHeight = widgetHeight - 95,
  54. oTable;
  55. if ($fs.is(':visible')) {
  56. builderTableHeight = builderTableHeight - 40;
  57. }
  58. //set the heights of the main widgets.
  59. $builder.height(widgetHeight)
  60. .find(".dataTables_scrolling")
  61. .css("max-height", builderTableHeight)
  62. .end()
  63. .width(screenWidth);
  64. $lib.height(widgetHeight)
  65. .find(".dataTables_scrolling")
  66. .css("max-height", libTableHeight)
  67. .end();
  68. if ($lib.filter(':visible').length > 0) {
  69. $lib.width(Math.floor(screenWidth * 0.48));
  70. $builder.width(Math.floor(screenWidth * 0.48))
  71. .find("#sb_edit")
  72. .remove()
  73. .end()
  74. .find("#sb_date_start")
  75. .css("margin-left", 0)
  76. .end();
  77. oTable = $('#show_builder_table').dataTable();
  78. //oTable.fnDraw();
  79. }
  80. }
  81. function validateTimeRange() {
  82. var oRange,
  83. inputs = $('.sb-timerange > input'),
  84. start, end;
  85. oRange = AIRTIME.utilities.fnGetScheduleRange(dateStartId, timeStartId, dateEndId, timeEndId);
  86. start = oRange.start;
  87. end = oRange.end;
  88. if (end >= start) {
  89. inputs.removeClass('error');
  90. }
  91. else {
  92. if (!inputs.hasClass('error')) {
  93. inputs.addClass('error');
  94. }
  95. }
  96. return {
  97. start: start,
  98. end: end,
  99. isValid: end >= start
  100. };
  101. }
  102. function showSearchSubmit() {
  103. var fn,
  104. op,
  105. oTable = $('#show_builder_table').dataTable(),
  106. check;
  107. check = validateTimeRange();
  108. if (check.isValid) {
  109. //reset timestamp value since input values could have changed.
  110. AIRTIME.showbuilder.resetTimestamp();
  111. fn = oTable.fnSettings().fnServerData;
  112. fn.start = check.start;
  113. fn.end = check.end;
  114. op = $("div.sb-advanced-options");
  115. if (op.is(":visible")) {
  116. if (fn.ops === undefined) {
  117. fn.ops = {};
  118. }
  119. fn.ops.showFilter = op.find("#sb_show_filter").val();
  120. fn.ops.myShows = op.find("#sb_my_shows").is(":checked") ? 1 : 0;
  121. }
  122. oTable.fnDraw();
  123. }
  124. }
  125. mod.onReady = function() {
  126. // define module vars.
  127. $lib = $("#library_content");
  128. $builder = $("#show_builder");
  129. $fs = $builder.find('fieldset');
  130. /*
  131. * Icon hover states for search.
  132. */
  133. $builder.on("mouseenter", ".sb-timerange .ui-button", function(ev) {
  134. $(this).addClass("ui-state-hover");
  135. });
  136. $builder.on("mouseleave", ".sb-timerange .ui-button", function(ev) {
  137. $(this).removeClass("ui-state-hover");
  138. });
  139. $builder.find(dateStartId)
  140. .datepicker(oBaseDatePickerSettings)
  141. .blur(validateTimeRange);
  142. $builder.find(timeStartId)
  143. .timepicker(oBaseTimePickerSettings)
  144. .blur(validateTimeRange);
  145. $builder.find(dateEndId)
  146. .datepicker(oBaseDatePickerSettings)
  147. .blur(validateTimeRange);
  148. $builder.find(timeEndId)
  149. .timepicker(oBaseTimePickerSettings)
  150. .blur(validateTimeRange);
  151. oRange = AIRTIME.utilities.fnGetScheduleRange(dateStartId, timeStartId,
  152. dateEndId, timeEndId);
  153. AIRTIME.showbuilder.fnServerData.start = oRange.start;
  154. AIRTIME.showbuilder.fnServerData.end = oRange.end;
  155. //the user might not have the library on the page (guest user)
  156. if (AIRTIME.library !== undefined) {
  157. AIRTIME.library.libraryInit();
  158. }
  159. AIRTIME.showbuilder.builderDataTable();
  160. setWidgetSize();
  161. $libWrapper = $lib.find("#library_display_wrapper");
  162. $libWrapper.prepend($libClose);
  163. $builder.find('.dataTables_scrolling').css("max-height",
  164. widgetHeight - 95);
  165. $builder.on("click", "#sb_submit", showSearchSubmit);
  166. $builder.on("click", "#sb_edit", function(ev) {
  167. var schedTable = $("#show_builder_table").dataTable();
  168. // reset timestamp to redraw the cursors.
  169. AIRTIME.showbuilder.resetTimestamp();
  170. $lib.show().width(Math.floor(screenWidth * 0.48));
  171. $builder.width(Math.floor(screenWidth * 0.48)).find("#sb_edit")
  172. .remove().end().find("#sb_date_start")
  173. .css("margin-left", 0).end();
  174. schedTable.fnDraw();
  175. $.ajax( {
  176. url : baseUrl+"usersettings/set-now-playing-screen-settings",
  177. type : "POST",
  178. data : {
  179. settings : {
  180. library : true
  181. },
  182. format : "json"
  183. },
  184. dataType : "json",
  185. success : function() {
  186. }
  187. });
  188. });
  189. $lib.on("click", "#sb_lib_close", function() {
  190. var schedTable = $("#show_builder_table").dataTable();
  191. $lib.hide();
  192. $builder.width(screenWidth).find(".sb-timerange").prepend(
  193. $toggleLib).find("#sb_date_start").css("margin-left", 30)
  194. .end().end();
  195. $toggleLib.removeClass("ui-state-hover");
  196. schedTable.fnDraw();
  197. $.ajax( {
  198. url : baseUrl+"usersettings/set-now-playing-screen-settings",
  199. type : "POST",
  200. data : {
  201. settings : {
  202. library : false
  203. },
  204. format : "json"
  205. },
  206. dataType : "json",
  207. success : function() {
  208. }
  209. });
  210. });
  211. $builder.find('legend').click(
  212. function(ev, item) {
  213. if ($fs.hasClass("closed")) {
  214. $fs.removeClass("closed");
  215. $builder.find('.dataTables_scrolling').css(
  216. "max-height", widgetHeight - 150);
  217. } else {
  218. $fs.addClass("closed");
  219. // set defaults for the options.
  220. $fs.find('select').val(0);
  221. $fs.find('input[type="checkbox"]').attr("checked",
  222. false);
  223. $builder.find('.dataTables_scrolling').css(
  224. "max-height", widgetHeight - 110);
  225. }
  226. });
  227. // set click event for all my shows checkbox.
  228. $builder.on("click", "#sb_my_shows", function(ev) {
  229. if ($(this).is(':checked')) {
  230. $(ev.delegateTarget).find('#sb_show_filter').val(0);
  231. }
  232. showSearchSubmit();
  233. });
  234. //set select event for choosing a show.
  235. $builder.on("change", '#sb_show_filter', function(ev) {
  236. if ($(this).val() !== 0) {
  237. $(ev.delegateTarget).find('#sb_my_shows')
  238. .attr("checked", false);
  239. }
  240. showSearchSubmit();
  241. });
  242. function checkScheduleUpdates() {
  243. var data = {}, oTable = $('#show_builder_table').dataTable(), fn = oTable
  244. .fnSettings().fnServerData, start = fn.start, end = fn.end;
  245. data["format"] = "json";
  246. data["start"] = start;
  247. data["end"] = end;
  248. data["timestamp"] = AIRTIME.showbuilder.getTimestamp();
  249. data["instances"] = AIRTIME.showbuilder.getShowInstances();
  250. if (fn.hasOwnProperty("ops")) {
  251. data["myShows"] = fn.ops.myShows;
  252. data["showFilter"] = fn.ops.showFilter;
  253. data["showInstanceFilter"] = fn.ops.showInstanceFilter;
  254. }
  255. $.ajax( {
  256. "dataType" : "json",
  257. "type" : "GET",
  258. "url" : baseUrl+"showbuilder/check-builder-feed",
  259. "data" : data,
  260. "success" : function(json) {
  261. if (json.update === true) {
  262. oTable.fnDraw();
  263. }
  264. setTimeout(checkScheduleUpdates, 5000);
  265. }
  266. });
  267. }
  268. //check if the timeline view needs updating.
  269. checkScheduleUpdates();
  270. };
  271. mod.onResize = function() {
  272. clearTimeout(resizeTimeout);
  273. resizeTimeout = setTimeout(setWidgetSize, 100);
  274. };
  275. return AIRTIME;
  276. } (AIRTIME || {}));
  277. $(document).ready(AIRTIME.builderMain.onReady);
  278. $(window).resize(AIRTIME.builderMain.onResize);