library.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. var AIRTIME = (function(AIRTIME) {
  2. var mod,
  3. libraryInit,
  4. oTable,
  5. $libContent,
  6. $libTable,
  7. LIB_SELECTED_CLASS = "lib-selected",
  8. chosenItems = {},
  9. visibleChosenItems = {};
  10. // we need to know whether the criteria value is string or
  11. // numeric in order to provide a single textbox or range textboxes
  12. // in the advanced search
  13. // s => string
  14. // n => numberic
  15. var libraryColumnTypes = {
  16. 0 : "",
  17. "album_title" : "s",
  18. "artist_name" : "s",
  19. "bit_rate" : "n",
  20. "bpm" : "n",
  21. "comments" : "s",
  22. "composer" : "s",
  23. "conductor" : "s",
  24. "copyright" : "s",
  25. "cuein" : "n",
  26. "cueout" : "n",
  27. "utime" : "n",
  28. "mtime" : "n",
  29. "lptime" : "n",
  30. "disc_number" : "n",
  31. "encoded_by" : "s",
  32. "genre" : "s",
  33. "isrc_number" : "s",
  34. "label" : "s",
  35. "language" : "s",
  36. "length" : "n",
  37. "lyricist" : "s",
  38. "mime" : "s",
  39. "mood" : "s",
  40. "name" : "s",
  41. "orchestra" : "s",
  42. "rating" : "n",
  43. "sample_rate" : "n",
  44. "track_title" : "s",
  45. "track_num" : "n",
  46. "year" : "n",
  47. "owner_id" : "s",
  48. "info_url" : "s",
  49. "replay_gain" : "n"
  50. };
  51. if (AIRTIME.library === undefined) {
  52. AIRTIME.library = {};
  53. }
  54. mod = AIRTIME.library;
  55. mod.getChosenItemsLength = function(){
  56. var cItem,
  57. selected,
  58. $trs;
  59. // Get visible items and check if any chosenItems are visible
  60. $trs = $libTable.find("tbody input:checkbox").parents("tr");
  61. $trs.each(function(i){
  62. for (cItem in chosenItems) {
  63. if (cItem === $(this).attr("id")) {
  64. visibleChosenItems[cItem] = $(this).data('aData');
  65. }
  66. }
  67. });
  68. selected = Object.keys(visibleChosenItems).length;
  69. visibleChosenItems = {};
  70. return selected;
  71. };
  72. mod.getChosenAudioFilesLength = function(){
  73. // var files = Object.keys(chosenItems),
  74. var files,
  75. $trs,
  76. cItem,
  77. i, length,
  78. count = 0,
  79. reAudio=/^(au|st|pl|bl)/ ;
  80. // Get visible items and check if any chosenItems are visible
  81. $trs = $libTable.find("tbody input:checkbox").parents("tr");
  82. $trs.each(function(i){
  83. for (cItem in chosenItems) {
  84. if (cItem === $(this).attr("id")) {
  85. visibleChosenItems[cItem] = $(this).data('aData');
  86. }
  87. }
  88. });
  89. files = Object.keys(visibleChosenItems);
  90. for (i = 0, length = files.length; i < length; i++) {
  91. if (files[i].search(reAudio) !== -1) {
  92. count++;
  93. }
  94. }
  95. visibleChosenItems = {};
  96. return count;
  97. };
  98. mod.changeAddButtonText = function($button, btnText) {
  99. $button.text(btnText);
  100. }
  101. mod.createToolbarButtons = function() {
  102. $menu = $("<div class='btn-toolbar' />");
  103. $menu
  104. .append("<div class='btn-group'>" +
  105. "<button class='btn btn-small dropdown-toggle' data-toggle='dropdown'>" +
  106. $.i18n._("Select")+" <span class='caret'></span>" +
  107. "</button>" +
  108. "<ul class='dropdown-menu'>" +
  109. "<li id='sb-select-page'><a href='#'>"+$.i18n._("Select this page")+"</a></li>" +
  110. "<li id='sb-dselect-page'><a href='#'>"+$.i18n._("Deselect this page")+"</a></li>" +
  111. "<li id='sb-dselect-all'><a href='#'>"+$.i18n._("Deselect all")+"</a></li>" +
  112. "</ul>" +
  113. "</div>")
  114. .append("<div class='btn-group'>" +
  115. "<button class='btn btn-small' id='library-plus'>" +
  116. "<i class='icon-white icon-plus'></i>" +
  117. "<span id='lib-plus-text'></span>" +
  118. "</button>" +
  119. "</div>")
  120. .append("<div class='btn-group'>" +
  121. "<button class='btn btn-small' id='sb-trash'>" +
  122. "<i class='icon-white icon-trash'></i>" +
  123. "</button>" +
  124. "</div>");
  125. }
  126. mod.createToolbarDropDown = function() {
  127. $('#sb-select-page').click(function(){mod.selectCurrentPage();});
  128. $('#sb-dselect-page').click(function(){mod.deselectCurrentPage();});
  129. $('#sb-dselect-all').click(function(){mod.selectNone();});
  130. };
  131. mod.checkDeleteButton = function() {
  132. var selected = mod.getChosenItemsLength(),
  133. check = false;
  134. if (selected !== 0) {
  135. check = true;
  136. }
  137. if (check === true) {
  138. AIRTIME.button.enableButton("btn-group #sb-trash", false);
  139. }
  140. else {
  141. AIRTIME.button.disableButton("btn-group #sb-trash", false);
  142. }
  143. };
  144. mod.checkToolBarIcons = function() {
  145. AIRTIME.library.checkAddButton();
  146. AIRTIME.library.checkDeleteButton();
  147. };
  148. mod.getSelectedData = function() {
  149. var id,
  150. data = [],
  151. cItem,
  152. $trs;
  153. $.fn.reverse = [].reverse;
  154. // Get visible items and check if any chosenItems are visible
  155. $trs = $libTable.find("tbody input:checkbox").parents("tr").reverse();
  156. $trs.each(function(i){
  157. for (cItem in chosenItems) {
  158. if (cItem === $(this).attr("id")) {
  159. visibleChosenItems[cItem] = $(this).data('aData');
  160. }
  161. }
  162. });
  163. for (id in visibleChosenItems) {
  164. if (visibleChosenItems.hasOwnProperty(id)) {
  165. data.push(visibleChosenItems[id]);
  166. }
  167. }
  168. visibleChosenItems = {};
  169. return data;
  170. };
  171. mod.redrawChosen = function() {
  172. var ids = Object.keys(chosenItems),
  173. i, length,
  174. $el;
  175. for (i = 0, length = ids.length; i < length; i++) {
  176. $el = $libTable.find("#"+ids[i]);
  177. if ($el.length !== 0) {
  178. mod.highlightItem($el);
  179. }
  180. }
  181. };
  182. mod.isChosenItem = function($el) {
  183. var id = $el.attr("id"),
  184. item = chosenItems[id];
  185. return item !== undefined ? true : false;
  186. };
  187. mod.addToChosen = function($el) {
  188. var id = $el.attr("id");
  189. chosenItems[id] = $el.data('aData');
  190. };
  191. mod.removeFromChosen = function($el) {
  192. var id = $el.attr("id");
  193. // used to not keep dragged items selected.
  194. if (!$el.hasClass(LIB_SELECTED_CLASS)) {
  195. delete chosenItems[id];
  196. }
  197. };
  198. mod.highlightItem = function($el) {
  199. var $input = $el.find("input");
  200. $input.attr("checked", true);
  201. $el.addClass(LIB_SELECTED_CLASS);
  202. };
  203. mod.unHighlightItem = function($el) {
  204. var $input = $el.find("input");
  205. $input.attr("checked", false);
  206. $el.removeClass(LIB_SELECTED_CLASS);
  207. };
  208. mod.selectItem = function($el) {
  209. mod.highlightItem($el);
  210. mod.addToChosen($el);
  211. mod.checkToolBarIcons();
  212. };
  213. mod.deselectItem = function($el) {
  214. mod.unHighlightItem($el);
  215. mod.removeFromChosen($el);
  216. mod.checkToolBarIcons();
  217. };
  218. /*
  219. * selects all items which the user can currently see. (behaviour taken from
  220. * gmail)
  221. *
  222. * by default the items are selected in reverse order so we need to reverse
  223. * it back
  224. */
  225. mod.selectCurrentPage = function() {
  226. $.fn.reverse = [].reverse;
  227. var $inputs = $libTable.find("tbody input:checkbox"),
  228. $trs = $inputs.parents("tr").reverse();
  229. $inputs.attr("checked", true);
  230. $trs.addClass(LIB_SELECTED_CLASS);
  231. $trs.each(function(i, el){
  232. $el = $(this);
  233. mod.addToChosen($el);
  234. });
  235. mod.checkToolBarIcons();
  236. };
  237. /*
  238. * deselects all items that the user can currently see. (behaviour taken
  239. * from gmail)
  240. */
  241. mod.deselectCurrentPage = function() {
  242. var $inputs = $libTable.find("tbody input:checkbox"),
  243. $trs = $inputs.parents("tr"),
  244. id;
  245. $inputs.attr("checked", false);
  246. $trs.removeClass(LIB_SELECTED_CLASS);
  247. $trs.each(function(i, el){
  248. $el = $(this);
  249. id = $el.attr("id");
  250. delete chosenItems[id];
  251. });
  252. mod.checkToolBarIcons();
  253. };
  254. mod.selectNone = function() {
  255. var $inputs = $libTable.find("tbody input:checkbox"),
  256. $trs = $inputs.parents("tr");
  257. $inputs.attr("checked", false);
  258. $trs.removeClass(LIB_SELECTED_CLASS);
  259. chosenItems = {};
  260. mod.checkToolBarIcons();
  261. };
  262. mod.fnDeleteItems = function(aMedia) {
  263. //Prevent the user from spamming the delete button while the AJAX request is in progress
  264. AIRTIME.button.disableButton("btn-group #sb-trash", false);
  265. //Hack to immediately show the "Processing" div in DataTables to give the user some sort of feedback.
  266. $(".dataTables_processing").css('visibility','visible');
  267. $.post(baseUrl+"library/delete",
  268. {"format": "json", "media": aMedia},
  269. function(json){
  270. if (json.message !== undefined) {
  271. alert(json.message);
  272. }
  273. chosenItems = {};
  274. oTable.fnStandingRedraw();
  275. //Re-enable the delete button
  276. AIRTIME.button.enableButton("btn-group #sb-trash", false);
  277. });
  278. };
  279. mod.fnDeleteSelectedItems = function() {
  280. if (confirm($.i18n._('Are you sure you want to delete the selected item(s)?'))) {
  281. var aData = AIRTIME.library.getSelectedData(),
  282. item,
  283. temp,
  284. aMedia = [],
  285. currentObjId = $("#side_playlist").find("#obj_id").val(),
  286. currentObjType = $("#side_playlist").find("#obj_type").val(),
  287. closeObj = false;
  288. // process selected files/playlists.
  289. for (item in aData) {
  290. temp = aData[item];
  291. if (temp !== null && temp.hasOwnProperty('id') ) {
  292. aMedia.push({"id": temp.id, "type": temp.ftype});
  293. if ( (temp.id == currentObjId && temp.ftype === currentObjType) ||
  294. temp.id == currentObjId && temp.ftype === "stream" && currentObjType === "webstream") {
  295. closeObj = true;
  296. }
  297. }
  298. }
  299. AIRTIME.library.fnDeleteItems(aMedia);
  300. // close the object (playlist/block/webstream)
  301. // on the right side if it was just deleted
  302. // from the library
  303. if (closeObj) {
  304. $.post(baseUrl+"playlist/close-playlist",
  305. {"format": "json", "type": currentObjType},
  306. function(json) {
  307. $("#side_playlist").empty().append(json.html);
  308. });
  309. }
  310. }
  311. };
  312. libraryInit = function() {
  313. $libContent = $("#library_content");
  314. /*
  315. * Icon hover states in the toolbar.
  316. */
  317. $libContent.on("mouseenter", ".fg-toolbar ul li", function(ev) {
  318. $el = $(this);
  319. if (!$el.hasClass("ui-state-disabled")) {
  320. $el.addClass("ui-state-hover");
  321. }
  322. });
  323. $libContent.on("mouseleave", ".fg-toolbar ul li", function(ev) {
  324. $el = $(this);
  325. if (!$el.hasClass("ui-state-disabled")) {
  326. $el.removeClass("ui-state-hover");
  327. }
  328. });
  329. var colReorderMap = new Array();
  330. $libTable = $libContent.find("table");
  331. function getTableHeight() {
  332. return $libContent.height() - 175;
  333. }
  334. function setColumnFilter(oTable){
  335. // TODO : remove this dirty hack once js is refactored
  336. if (!oTable.fnSettings()) { return ; }
  337. var aoCols = oTable.fnSettings().aoColumns;
  338. var colsForAdvancedSearch = new Array();
  339. var advanceSearchDiv = $("div#advanced_search");
  340. advanceSearchDiv.empty();
  341. $.each(aoCols, function(i,ele){
  342. if (ele.bSearchable) {
  343. var currentColId = ele._ColReorder_iOrigCol;
  344. var inputClass = 'filter_column filter_number_text';
  345. var labelStyle = "style='margin-right:35px;'";
  346. if (libraryColumnTypes[ele.mDataProp] != "s") {
  347. inputClass = 'filterColumn filter_number_range';
  348. labelStyle = "";
  349. }
  350. if (ele.bVisible) {
  351. advanceSearchDiv.append(
  352. "<div id='advanced_search_col_"+currentColId+"' class='control-group'>" +
  353. "<label class='control-label'"+labelStyle+">"+ele.sTitle+" : </label>" +
  354. "<div id='"+ele.mDataProp+"' class='controls "+inputClass+"'></div>" +
  355. "</div>");
  356. } else {
  357. advanceSearchDiv.append(
  358. "<div id='advanced_search_col_"+currentColId+"' class='control-group' style='display:none;'>" +
  359. "<label class='control-label'"+labelStyle+">"+ele.sTitle+"</label>" +
  360. "<div id='"+ele.mDataProp+"' class='controls "+inputClass+"'></div>" +
  361. "</div>");
  362. }
  363. if (libraryColumnTypes[ele.mDataProp] == "s") {
  364. var obj = { sSelector: "#"+ele.mDataProp }
  365. } else {
  366. var obj = { sSelector: "#"+ele.mDataProp, type: "number-range" }
  367. }
  368. colsForAdvancedSearch.push(obj);
  369. } else {
  370. colsForAdvancedSearch.push(null);
  371. }
  372. });
  373. oTable.columnFilter({
  374. aoColumns: colsForAdvancedSearch,
  375. bUseColVis: true,
  376. sPlaceHolder: "head:before"
  377. }
  378. );
  379. }
  380. function setFilterElement(iColumn, bVisible){
  381. var actualId = colReorderMap[iColumn];
  382. var selector = "div#advanced_search_col_"+actualId;
  383. var $el = $(selector);
  384. if (bVisible) {
  385. $el.show();
  386. } else {
  387. $el.hide();
  388. }
  389. //resize to prevent double scroll bars.
  390. var $fs = $el.parents("fieldset"),
  391. tableHeight = getTableHeight(),
  392. searchHeight = $fs.height();
  393. $libContent.find(".dataTables_scrolling").css("max-height", tableHeight - searchHeight);
  394. }
  395. oTable = $libTable.dataTable( {
  396. // put hidden columns at the top to insure they can never be visible
  397. // on the table through column reordering.
  398. //IMPORTANT: WHEN ADDING A NEW COLUMN PLEASE CONSULT WITH THE WIKI
  399. // https://wiki.sourcefabric.org/display/CC/Adding+a+new+library+datatable+column
  400. "aoColumns": [
  401. /* ftype */ { "sTitle" : "" , "mDataProp" : "ftype" , "bSearchable" : false , "bVisible" : false } ,
  402. /* Checkbox */ { "sTitle" : "" , "mDataProp" : "checkbox" , "bSortable" : false , "bSearchable" : false , "sWidth" : "25px" , "sClass" : "library_checkbox" } ,
  403. /* Type */ { "sTitle" : "" , "mDataProp" : "image" , "bSearchable" : false , "sWidth" : "25px" , "sClass" : "library_type" , "iDataSort" : 0 } ,
  404. /* Is Scheduled */ { "sTitle" : $.i18n._("Scheduled") , "mDataProp" : "is_scheduled" , "bSearchable" : false , "sWidth" : "90px" , "sClass" : "library_is_scheduled"} ,
  405. /* Is Playlist */ { "sTitle" : $.i18n._("Playlist / Block") , "mDataProp" : "is_playlist" , "bSearchable" : false , "sWidth" : "110px" , "sClass" : "library_is_playlist"} ,
  406. /* Title */ { "sTitle" : $.i18n._("Title") , "mDataProp" : "track_title" , "sClass" : "library_title" , "sWidth" : "170px" } ,
  407. /* Creator */ { "sTitle" : $.i18n._("Creator") , "mDataProp" : "artist_name" , "sClass" : "library_creator" , "sWidth" : "160px" } ,
  408. /* Album */ { "sTitle" : $.i18n._("Album") , "mDataProp" : "album_title" , "sClass" : "library_album" , "sWidth" : "150px" } ,
  409. /* Bit Rate */ { "sTitle" : $.i18n._("Bit Rate") , "mDataProp" : "bit_rate" , "bVisible" : false , "sClass" : "library_bitrate" , "sWidth" : "80px" },
  410. /* BPM */ { "sTitle" : $.i18n._("BPM") , "mDataProp" : "bpm" , "bVisible" : false , "sClass" : "library_bpm" , "sWidth" : "50px" },
  411. /* Composer */ { "sTitle" : $.i18n._("Composer") , "mDataProp" : "composer" , "bVisible" : false , "sClass" : "library_composer" , "sWidth" : "150px" },
  412. /* Conductor */ { "sTitle" : $.i18n._("Conductor") , "mDataProp" : "conductor" , "bVisible" : false , "sClass" : "library_conductor" , "sWidth" : "125px" },
  413. /* Copyright */ { "sTitle" : $.i18n._("Copyright") , "mDataProp" : "copyright" , "bVisible" : false , "sClass" : "library_copyright" , "sWidth" : "125px" },
  414. /* Cue In */ { "sTitle" : $.i18n._("Cue In") , "mDataProp" : "cuein" , "bVisible" : false , "sClass" : "library_length" , "sWidth" : "80px" },
  415. /* Cue Out */ { "sTitle" : $.i18n._("Cue Out") , "mDataProp" : "cueout" , "bVisible" : false , "sClass" : "library_length" , "sWidth" : "80px" },
  416. /* Encoded */ { "sTitle" : $.i18n._("Encoded By") , "mDataProp" : "encoded_by" , "bVisible" : false , "sClass" : "library_encoded" , "sWidth" : "150px" },
  417. /* Genre */ { "sTitle" : $.i18n._("Genre") , "mDataProp" : "genre" , "bVisible" : false , "sClass" : "library_genre" , "sWidth" : "100px" },
  418. /* ISRC Number */ { "sTitle" : $.i18n._("ISRC") , "mDataProp" : "isrc_number" , "bVisible" : false , "sClass" : "library_isrc" , "sWidth" : "150px" },
  419. /* Label */ { "sTitle" : $.i18n._("Label") , "mDataProp" : "label" , "bVisible" : false , "sClass" : "library_label" , "sWidth" : "125px" },
  420. /* Language */ { "sTitle" : $.i18n._("Language") , "mDataProp" : "language" , "bVisible" : false , "sClass" : "library_language" , "sWidth" : "125px" },
  421. /* Last Modified */ { "sTitle" : $.i18n._("Last Modified") , "mDataProp" : "mtime" , "bVisible" : false , "sClass" : "library_modified_time" , "sWidth" : "125px" },
  422. /* Last Played */ { "sTitle" : $.i18n._("Last Played") , "mDataProp" : "lptime" , "bVisible" : false , "sClass" : "library_modified_time" , "sWidth" : "125px" },
  423. /* Length */ { "sTitle" : $.i18n._("Length") , "mDataProp" : "length" , "sClass" : "library_length" , "sWidth" : "80px" } ,
  424. /* Mime */ { "sTitle" : $.i18n._("Mime") , "mDataProp" : "mime" , "bVisible" : false , "sClass" : "library_mime" , "sWidth" : "80px" },
  425. /* Mood */ { "sTitle" : $.i18n._("Mood") , "mDataProp" : "mood" , "bVisible" : false , "sClass" : "library_mood" , "sWidth" : "70px" },
  426. /* Owner */ { "sTitle" : $.i18n._("Owner") , "mDataProp" : "owner_id" , "bVisible" : false , "sClass" : "library_language" , "sWidth" : "125px" },
  427. /* Replay Gain */ { "sTitle" : $.i18n._("Replay Gain") , "mDataProp" : "replay_gain" , "bVisible" : false , "sClass" : "library_replay_gain" , "sWidth" : "80px" },
  428. /* Sample Rate */ { "sTitle" : $.i18n._("Sample Rate") , "mDataProp" : "sample_rate" , "bVisible" : false , "sClass" : "library_sr" , "sWidth" : "80px" },
  429. /* Track Number */ { "sTitle" : $.i18n._("Track Number") , "mDataProp" : "track_number" , "bVisible" : false , "sClass" : "library_track" , "sWidth" : "65px" },
  430. /* Upload Time */ { "sTitle" : $.i18n._("Uploaded") , "mDataProp" : "utime" , "sClass" : "library_upload_time" , "sWidth" : "125px" } ,
  431. /* Website */ { "sTitle" : $.i18n._("Website") , "mDataProp" : "info_url" , "bVisible" : false , "sClass" : "library_url" , "sWidth" : "150px" },
  432. /* Year */ { "sTitle" : $.i18n._("Year") , "mDataProp" : "year" , "bVisible" : false , "sClass" : "library_year" , "sWidth" : "60px" }
  433. ],
  434. "bProcessing": true,
  435. "bServerSide": true,
  436. "aLengthMenu": [[5, 10, 15, 20, 25, 50, 100], [5, 10, 15, 20, 25, 50, 100]],
  437. "bStateSave": true,
  438. "fnStateSaveParams": function (oSettings, oData) {
  439. // remove oData components we don't want to save.
  440. delete oData.oSearch;
  441. delete oData.aoSearchCols;
  442. },
  443. "fnStateSave": function (oSettings, oData) {
  444. localStorage.setItem('datatables-library', JSON.stringify(oData));
  445. $.ajax({
  446. url: baseUrl+"usersettings/set-library-datatable",
  447. type: "POST",
  448. data: {settings : oData, format: "json"},
  449. dataType: "json"
  450. });
  451. colReorderMap = oData.ColReorder;
  452. },
  453. "fnStateLoad": function fnLibStateLoad(oSettings) {
  454. var settings = localStorage.getItem('datatables-library');
  455. try {
  456. return JSON.parse(settings);
  457. } catch (e) {
  458. return null;
  459. }
  460. },
  461. "fnStateLoadParams": function (oSettings, oData) {
  462. var i,
  463. length,
  464. a = oData.abVisCols;
  465. if (a) {
  466. // putting serialized data back into the correct js type to make
  467. // sure everything works properly.
  468. for (i = 0, length = a.length; i < length; i++) {
  469. if (typeof(a[i]) === "string") {
  470. a[i] = (a[i] === "true") ? true : false;
  471. }
  472. }
  473. }
  474. a = oData.ColReorder;
  475. if (a) {
  476. for (i = 0, length = a.length; i < length; i++) {
  477. if (typeof(a[i]) === "string") {
  478. a[i] = parseInt(a[i], 10);
  479. }
  480. }
  481. }
  482. oData.iEnd = parseInt(oData.iEnd, 10);
  483. oData.iLength = parseInt(oData.iLength, 10);
  484. oData.iStart = parseInt(oData.iStart, 10);
  485. oData.iCreate = parseInt(oData.iCreate, 10);
  486. },
  487. "sAjaxSource": baseUrl+"Library/contents-feed",
  488. "sAjaxDataProp": "files",
  489. "fnServerData": function ( sSource, aoData, fnCallback ) {
  490. /*
  491. * The real validation check is done in
  492. * dataTables.columnFilter.js We also need to check it here
  493. * because datatable is redrawn everytime an action is performed
  494. * in the Library page. In order for datatable to redraw the
  495. * advanced search fields MUST all be valid.
  496. */
  497. var advSearchFields = $("div#advanced_search").children(':visible');
  498. var advSearchValid = validateAdvancedSearch(advSearchFields);
  499. var type;
  500. aoData.push( { name: "format", value: "json"} );
  501. aoData.push( { name: "advSearch", value: advSearchValid} );
  502. // push whether to search files/playlists or all.
  503. type = $("#library_display_type").find("select").val();
  504. type = (type === undefined) ? 0 : type;
  505. aoData.push( { name: "type", value: type} );
  506. $.ajax( {
  507. "dataType": 'json',
  508. "type": "POST",
  509. "url": sSource,
  510. "data": aoData,
  511. "success": fnCallback
  512. } );
  513. },
  514. "fnRowCallback": AIRTIME.library.fnRowCallback,
  515. "fnCreatedRow": function( nRow, aData, iDataIndex ) {
  516. //add soundcloud icon
  517. if (aData.soundcloud_id !== undefined) {
  518. if (aData.soundcloud_id === "-2") {
  519. $(nRow).find("td.library_title").append('<span class="small-icon progress"/>');
  520. } else if (aData.soundcloud_id === "-3") {
  521. $(nRow).find("td.library_title").append('<span class="small-icon sc-error"/>');
  522. } else if (aData.soundcloud_id !== null) {
  523. $(nRow).find("td.library_title").append('<span class="small-icon soundcloud"/>');
  524. }
  525. }
  526. // add checkbox
  527. $(nRow).find('td.library_checkbox').html("<input type='checkbox' name='cb_"+aData.id+"'>");
  528. // add audio preview image/button
  529. if (aData.ftype === "audioclip") {
  530. $(nRow).find('td.library_type').html('<img title="'+$.i18n._("Track preview")+'" src="'+baseUrl+'css/images/icon_audioclip.png">');
  531. } else if (aData.ftype === "playlist") {
  532. $(nRow).find('td.library_type').html('<img title="'+$.i18n._("Playlist preview")+'" src="'+baseUrl+'css/images/icon_playlist.png">');
  533. } else if (aData.ftype === "block") {
  534. $(nRow).find('td.library_type').html('<img title="'+$.i18n._("Smart Block")+'" src="'+baseUrl+'css/images/icon_smart-block.png">');
  535. } else if (aData.ftype === "stream") {
  536. $(nRow).find('td.library_type').html('<img title="'+$.i18n._("Webstream preview")+'" src="'+baseUrl+'css/images/icon_webstream.png">');
  537. }
  538. if (aData.is_scheduled) {
  539. $(nRow).find("td.library_is_scheduled").html('<span class="small-icon is_scheduled"></span>');
  540. } else if (!aData.is_scheduled) {
  541. $(nRow).find("td.library_is_scheduled").html('');
  542. }
  543. if (aData.is_playlist) {
  544. $(nRow).find("td.library_is_playlist").html('<span class="small-icon is_playlist"></span>');
  545. } else if (!aData.is_playlist) {
  546. $(nRow).find("td.library_is_playlist").html('');
  547. }
  548. // add the play function to the library_type td
  549. $(nRow).find('td.library_type').click(function(){
  550. if (aData.ftype === 'playlist' && aData.length !== '0.0'){
  551. open_playlist_preview(aData.audioFile, 0);
  552. } else if (aData.ftype === 'audioclip') {
  553. if (isAudioSupported(aData.mime)) {
  554. open_audio_preview(aData.ftype, aData.audioFile, aData.track_title, aData.artist_name);
  555. }
  556. } else if (aData.ftype == 'stream') {
  557. if (isAudioSupported(aData.mime)) {
  558. open_audio_preview(aData.ftype, aData.audioFile, aData.track_title, aData.artist_name);
  559. }
  560. } else if (aData.ftype == 'block' && aData.bl_type == 'static') {
  561. open_block_preview(aData.audioFile, 0);
  562. }
  563. return false;
  564. });
  565. alreadyclicked=false;
  566. // call the context menu so we can prevent the event from
  567. // propagating.
  568. $(nRow).find('td:not(.library_checkbox, .library_type)').click(function(e){
  569. var el=$(this);
  570. if (alreadyclicked)
  571. {
  572. alreadyclicked=false; // reset
  573. clearTimeout(alreadyclickedTimeout); // prevent this
  574. // from
  575. // happening
  576. // do what needs to happen on double click.
  577. $tr = $(el).parent();
  578. data = $tr.data("aData");
  579. AIRTIME.library.dblClickAdd(data, data.ftype);
  580. }
  581. else
  582. {
  583. alreadyclicked=true;
  584. alreadyclickedTimeout=setTimeout(function(){
  585. alreadyclicked=false; // reset when it happens
  586. // do what needs to happen on single click.
  587. // use el instead of $(this) because $(this) is
  588. // no longer the element
  589. el.contextMenu({x: e.pageX, y: e.pageY});
  590. },300); // <-- dblclick tolerance here
  591. }
  592. return false;
  593. });
  594. /*$(nRow).find(".media-item-in-use").qtip({
  595. content: {
  596. text: aData.status_msg
  597. },
  598. hide: {
  599. delay: 500,
  600. fixed: true
  601. },
  602. style: {
  603. border: {
  604. width: 0,
  605. radius: 4
  606. },
  607. classes: "ui-tooltip-dark ui-tooltip-rounded"
  608. },
  609. position: {
  610. my: "left bottom",
  611. at: "right center"
  612. },
  613. });*/
  614. // add a tool tip to appear when the user clicks on the type
  615. // icon.
  616. $(nRow).find("td:not(.library_checkbox, .library_type)").qtip({
  617. content: {
  618. text: $.i18n._("Loading..."),
  619. title: {
  620. text: aData.track_title
  621. },
  622. ajax: {
  623. url: baseUrl+"Library/get-file-metadata",
  624. type: "get",
  625. data: ({format: "html", id : aData.id, type: aData.ftype}),
  626. success: function(data, status) {
  627. this.set('content.text', data);
  628. }
  629. }
  630. },
  631. position: {
  632. target: 'event',
  633. adjust: {
  634. resize: true,
  635. method: "flip flip"
  636. },
  637. my: 'left center',
  638. at: 'right center',
  639. viewport: $(window), // Keep the tooltip on-screen at
  640. // all times
  641. effect: false // Disable positioning animation
  642. },
  643. style: {
  644. classes: "ui-tooltip-dark file-md-long"
  645. },
  646. show: 'mousedown',
  647. events: {
  648. show: function(event, api) {
  649. // Only show the tooltip if it was a right-click
  650. if(event.originalEvent.button !== 2) {
  651. event.preventDefault();
  652. }
  653. }
  654. },
  655. hide: {event:'mouseout', delay: 50, fixed:true}
  656. });
  657. },
  658. // remove any selected nodes before the draw.
  659. "fnPreDrawCallback": function( oSettings ) {
  660. // make sure any dragging helpers are removed or else they'll be
  661. // stranded on the screen.
  662. $("#draggingContainer").remove();
  663. },
  664. "fnDrawCallback": AIRTIME.library.fnDrawCallback,
  665. "aaSorting": [[5, 'asc']],
  666. "sPaginationType": "full_numbers",
  667. "bJQueryUI": true,
  668. "bAutoWidth": false,
  669. "oLanguage": datatables_dict,
  670. // R = ColReorder, C = ColVis
  671. "sDom": 'Rl<"#library_display_type">f<"dt-process-rel"r><"H"<"library_toolbar"C>><"dataTables_scrolling"t><"F"ip>',
  672. "oColVis": {
  673. "sAlign": "right",
  674. "aiExclude": [0, 1, 2],
  675. "sSize": "css",
  676. "fnStateChange": setFilterElement,
  677. "buttonText": $.i18n._("Show / hide columns")
  678. },
  679. "oColReorder": {
  680. "iFixedColumns": 3
  681. }
  682. });
  683. setColumnFilter(oTable);
  684. oTable.fnSetFilteringDelay(350);
  685. var simpleSearchText;
  686. $libContent.on("click", "legend", function(){
  687. $simpleSearch = $libContent.find("#library_display_filter label");
  688. var $fs = $(this).parents("fieldset"),
  689. searchHeight,
  690. tableHeight = getTableHeight(),
  691. height;
  692. if ($fs.hasClass("closed")) {
  693. $fs.removeClass("closed");
  694. searchHeight = $fs.height();
  695. //keep value of simple search for when user switches back to it
  696. simpleSearchText = $simpleSearch.find('input').val();
  697. // clear the simple search text field and reset datatable
  698. $(".dataTables_filter input").val("").keyup();
  699. $simpleSearch.addClass("sp-invisible");
  700. //resize the library table to avoid a double scroll bar. CC-4504
  701. height = tableHeight - searchHeight;
  702. $libContent.find(".dataTables_scrolling").css("max-height", height);
  703. }
  704. else {
  705. // clear the advanced search fields
  706. var divs = $("div#advanced_search").children(':visible');
  707. $.each(divs, function(i, div){
  708. fields = $(div).children().find('input');
  709. $.each(fields, function(i, field){
  710. if ($(field).val() !== "") {
  711. $(field).val("");
  712. // we need to reset the results when removing
  713. // an advanced search field
  714. $(field).keyup();
  715. }
  716. });
  717. });
  718. //reset datatable with previous simple search results (if any)
  719. $(".dataTables_filter input").val(simpleSearchText).keyup();
  720. $simpleSearch.removeClass("sp-invisible");
  721. $fs.addClass("closed");
  722. //resize the library table to avoid a double scroll bar. CC-4504
  723. $libContent.find(".dataTables_scrolling").css("max-height", tableHeight);
  724. }
  725. });
  726. var tableHeight = getTableHeight();
  727. $libContent.find(".dataTables_scrolling").css("max-height", tableHeight);
  728. AIRTIME.library.setupLibraryToolbar(oTable);
  729. $("#library_display_type")
  730. .addClass("dataTables_type")
  731. .append('<select name="library_display_type" />')
  732. .find("select")
  733. .append('<option value="0">'+$.i18n._("All")+'</option>')
  734. .append('<option value="1">'+$.i18n._("Files")+'</option>')
  735. .append('<option value="2">'+$.i18n._("Playlists")+'</option>')
  736. .append('<option value="3">'+$.i18n._("Smart Blocks")+'</option>')
  737. .append('<option value="4">'+$.i18n._("Web Streams")+'</option>')
  738. .end()
  739. .change(function(ev){
  740. oTable.fnDraw();
  741. });
  742. $libTable.find("tbody").on("click", "input[type=checkbox]", function(ev) {
  743. var $cb = $(this),
  744. $prev,
  745. $tr = $cb.parents("tr"),
  746. $trs;
  747. if ($cb.is(":checked")) {
  748. if (ev.shiftKey) {
  749. $prev = $libTable.find("tbody").find("tr."+LIB_SELECTED_CLASS).eq(-1);
  750. $trs = $prev.nextUntil($tr);
  751. $trs.each(function(i, el){
  752. mod.selectItem($(el));
  753. });
  754. }
  755. mod.selectItem($tr);
  756. }
  757. else {
  758. mod.deselectItem($tr);
  759. }
  760. });
  761. checkImportStatus();
  762. checkLibrarySCUploadStatus();
  763. addQtipToSCIcons();
  764. // begin context menu initialization.
  765. $.contextMenu({
  766. selector: '#library_display td:not(.library_checkbox)',
  767. trigger: "left",
  768. ignoreRightClick: true,
  769. build: function($el, e) {
  770. var data, screen, items, callback, $tr;
  771. $tr = $el.parent();
  772. data = $tr.data("aData");
  773. screen = $tr.data("screen");
  774. function processMenuItems(oItems) {
  775. // define an add to playlist callback.
  776. if (oItems.pl_add !== undefined) {
  777. var aItems = [];
  778. callback = function() {
  779. aItems.push(new Array(data.id, data.ftype));
  780. AIRTIME.playlist.fnAddItems(aItems, undefined, 'after');
  781. };
  782. oItems.pl_add.callback = callback;
  783. }
  784. // define an edit callback.
  785. if (oItems.edit !== undefined) {
  786. if (data.ftype === "audioclip") {
  787. callback = function() {
  788. $.get(oItems.edit.url, {format: "json"}, function(json){
  789. buildEditMetadataDialog(json);
  790. });
  791. };
  792. } else if (data.ftype === "playlist" || data.ftype === "block") {
  793. callback = function() {
  794. var url = baseUrl+'Playlist/edit';
  795. AIRTIME.playlist.fnEdit(data.id, data.ftype, url);
  796. AIRTIME.playlist.validatePlaylistElements();
  797. };
  798. } else if (data.ftype === "stream") {
  799. callback = function() {
  800. var url = baseUrl+'Webstream/edit';
  801. AIRTIME.playlist.fnEdit(data.id, data.ftype, url);
  802. }
  803. } else {
  804. throw new Exception($.i18n._("Unknown type: ") + data.ftype);
  805. }
  806. oItems.edit.callback = callback;
  807. }
  808. // define a play callback.
  809. if (oItems.play !== undefined) {
  810. if (oItems.play.mime !== undefined) {
  811. if (!isAudioSupported(oItems.play.mime)) {
  812. oItems.play.disabled = true;
  813. }
  814. }
  815. callback = function() {
  816. if (data.ftype === 'playlist' && data.length !== '0.0'){
  817. playlistIndex = $(this).parent().attr('id').substring(3); // remove
  818. // the
  819. // pl_
  820. open_playlist_preview(playlistIndex, 0);
  821. } else if (data.ftype === 'audioclip' || data.ftype === 'stream') {
  822. open_audio_preview(data.ftype, data.audioFile, data.track_title, data.artist_name);
  823. } else if (data.ftype === 'block') {
  824. blockIndex = $(this).parent().attr('id').substring(3); // remove
  825. // the
  826. // pl_
  827. open_block_preview(blockIndex, 0);
  828. }
  829. };
  830. oItems.play.callback = callback;
  831. }
  832. // define a delete callback.
  833. if (oItems.del !== undefined) {
  834. // delete through the playlist controller, will reset
  835. // playlist screen if this is the currently edited
  836. // playlist.
  837. if ((data.ftype === "playlist" || data.ftype === "block") && screen === "playlist") {
  838. callback = function() {
  839. aMedia = [];
  840. aMedia.push({"id": data.id, "type": data.ftype});
  841. if (confirm($.i18n._('Are you sure you want to delete the selected item?'))) {
  842. AIRTIME.library.fnDeleteItems(aMedia);
  843. }
  844. };
  845. }
  846. else {
  847. callback = function() {
  848. var media = [];
  849. if (confirm($.i18n._('Are you sure you want to delete the selected item?'))) {
  850. media.push({"id": data.id, "type": data.ftype});
  851. $.post(oItems.del.url, {format: "json", media: media }, function(json){
  852. var oTable;
  853. if (json.message) {
  854. alert(json.message);
  855. }
  856. oTable = $("#library_display").dataTable();
  857. oTable.fnDeleteRow( $tr[0] );
  858. });
  859. }
  860. };
  861. }
  862. oItems.del.callback = callback;
  863. }
  864. // define a download callback.
  865. if (oItems.download !== undefined) {
  866. callback = function() {
  867. document.location.href = oItems.download.url;
  868. };
  869. oItems.download.callback = callback;
  870. }
  871. // add callbacks for Soundcloud menu items.
  872. if (oItems.soundcloud !== undefined) {
  873. var soundcloud = oItems.soundcloud.items;
  874. // define an upload to soundcloud callback.
  875. if (soundcloud.upload !== undefined) {
  876. callback = function() {
  877. $.post(soundcloud.upload.url, function(){
  878. addProgressIcon(data.id);
  879. });
  880. };
  881. soundcloud.upload.callback = callback;
  882. }
  883. // define a view on soundcloud callback
  884. if (soundcloud.view !== undefined) {
  885. callback = function() {
  886. window.open(soundcloud.view.url);
  887. };
  888. soundcloud.view.callback = callback;
  889. }
  890. }
  891. // add callbacks for duplicate menu items.
  892. if (oItems.duplicate !== undefined) {
  893. var url = oItems.duplicate.url;
  894. callback = function() {
  895. $.post(url, {format: "json", id: data.id }, function(json){
  896. oTable.fnStandingRedraw();
  897. });
  898. };
  899. oItems.duplicate.callback = callback;
  900. }
  901. // remove 'Add to smart block' option if the current
  902. // block is dynamic
  903. if ($('input:radio[name=sp_type]:checked').val() === "1") {
  904. delete oItems.pl_add;
  905. }
  906. items = oItems;
  907. }
  908. request = $.ajax({
  909. url: baseUrl+"library/context-menu",
  910. type: "GET",
  911. data: {id : data.id, type: data.ftype, format: "json", "screen": screen},
  912. dataType: "json",
  913. async: false,
  914. success: function(json){
  915. processMenuItems(json.items);
  916. }
  917. });
  918. return {
  919. items: items
  920. };
  921. }
  922. });
  923. };
  924. mod.libraryInit = libraryInit;
  925. return AIRTIME;
  926. }(AIRTIME || {}));
  927. function buildEditMetadataDialog (json){
  928. var dialog = $(json.dialog);
  929. dialog.dialog({
  930. autoOpen: false,
  931. title: $.i18n._("Edit Metadata"),
  932. width: 460,
  933. height: 660,
  934. modal: true,
  935. close: closeDialogLibrary
  936. });
  937. dialog.dialog('open');
  938. }
  939. function closeDialogLibrary(event, ui) {
  940. $(this).remove();
  941. }
  942. function checkImportStatus() {
  943. $.getJSON(baseUrl+'Preference/is-import-in-progress', function(data){
  944. var $div = $('#import_status');
  945. var table = $('#library_display').dataTable();
  946. if (data == true){
  947. $div.show();
  948. }
  949. else{
  950. if ($div.is(':visible')) {
  951. table.fnStandingRedraw();
  952. }
  953. $div.hide();
  954. }
  955. setTimeout(checkImportStatus, 5000);
  956. });
  957. }
  958. function addProgressIcon(id) {
  959. var tr = $("#au_"+id),
  960. span;
  961. span = tr.find("td.library_title").find("span");
  962. if (span.length > 0){
  963. span.removeClass()
  964. .addClass("small-icon progress");
  965. }
  966. else{
  967. tr.find("td.library_title")
  968. .append('<span class="small-icon progress"></span>');
  969. }
  970. }
  971. function checkLibrarySCUploadStatus(){
  972. var url = baseUrl+'Library/get-upload-to-soundcloud-status',
  973. span,
  974. id;
  975. function checkSCUploadStatusCallback(json) {
  976. if (json.sc_id > 0) {
  977. span.removeClass("progress").addClass("soundcloud");
  978. }
  979. else if (json.sc_id == "-3") {
  980. span.removeClass("progress").addClass("sc-error");
  981. }
  982. }
  983. function checkSCUploadStatusRequest() {
  984. span = $(this);
  985. id = span.parents("tr").data("aData").id;
  986. $.post(url, {format: "json", id: id, type:"file"}, checkSCUploadStatusCallback);
  987. }
  988. $("#library_display span.progress").each(checkSCUploadStatusRequest);
  989. setTimeout(checkLibrarySCUploadStatus, 5000);
  990. }
  991. function addQtipToSCIcons() {
  992. $("#content")
  993. .on('mouseover', ".progress, .soundcloud, .sc-error", function() {
  994. var aData = $(this).parents("tr").data("aData"),
  995. id = aData.id,
  996. sc_id = aData.soundcloud_id;
  997. if ($(this).hasClass("progress")){
  998. $(this).qtip({
  999. content: {
  1000. text: $.i18n._("Uploading in progress...")
  1001. },
  1002. position:{
  1003. adjust: {
  1004. resize: true,
  1005. method: "flip flip"
  1006. },
  1007. at: "right center",
  1008. my: "left top",
  1009. viewport: $(window)
  1010. },
  1011. style: {
  1012. classes: "ui-tooltip-dark file-md-long"
  1013. },
  1014. show: {
  1015. ready: true // Needed to make it show on first mouseover event
  1016. }
  1017. });
  1018. }
  1019. else if ($(this).hasClass("soundcloud")){
  1020. $(this).qtip({
  1021. content: {
  1022. text: $.i18n._("The soundcloud id for this file is: ") + sc_id
  1023. },
  1024. position:{
  1025. adjust: {
  1026. resize: true,
  1027. method: "flip flip"
  1028. },
  1029. at: "right center",
  1030. my: "left top",
  1031. viewport: $(window)
  1032. },
  1033. style: {
  1034. classes: "ui-tooltip-dark file-md-long"
  1035. },
  1036. show: {
  1037. ready: true // Needed to make it show on first mouseover event
  1038. }
  1039. });
  1040. }
  1041. else if ($(this).hasClass("sc-error")) {
  1042. $(this).qtip({
  1043. content: {
  1044. text: $.i18n._("Retreiving data from the server..."),
  1045. ajax: {
  1046. url: baseUrl+"Library/get-upload-to-soundcloud-status",
  1047. type: "post",
  1048. data: ({format: "json", id : id, type: "file"}),
  1049. success: function(json, status){
  1050. this.set('content.text', $.i18n._("There was an error while uploading to soundcloud.")+"<br>"+
  1051. $.i18n._("Error code: ")+json.error_code+
  1052. "<br>"+$.i18n._("Error msg: ")+json.error_msg+"<br>");
  1053. }
  1054. }
  1055. },
  1056. position:{
  1057. adjust: {
  1058. resize: true,
  1059. method: "flip flip"
  1060. },
  1061. at: "right center",
  1062. my: "left top",
  1063. viewport: $(window)
  1064. },
  1065. style: {
  1066. classes: "ui-tooltip-dark file-md-long"
  1067. },
  1068. show: {
  1069. ready: true // Needed to make it show on first mouseover event
  1070. }
  1071. });
  1072. }
  1073. });
  1074. }
  1075. /*
  1076. * This function is called from dataTables.columnFilter.js
  1077. */
  1078. function validateAdvancedSearch(divs) {
  1079. var valid,
  1080. allValid = true,
  1081. fieldName,
  1082. fields,
  1083. searchTerm = Array(),
  1084. searchTermType,
  1085. regExpr,
  1086. timeRegEx = "\\d{2}[:]([0-5]){1}([0-9]){1}[:]([0-5]){1}([0-9]){1}([.]\\d{1,6})?",
  1087. dateRegEx = "\\d{4}[-]\\d{2}[-]\\d{2}?",
  1088. integerRegEx = "^\\d+$",
  1089. numericRegEx = "^\\d+[.]?\\d*$";
  1090. searchTerm[0] = "";
  1091. searchTerm[1] = "";
  1092. $.each(divs, function(i, div){
  1093. fieldName = $(div).children(':nth-child(2)').attr('id');
  1094. fields = $(div).children().find('input');
  1095. searchTermType = validationTypes[fieldName];
  1096. valid = true;
  1097. $.each(fields, function(i, field){
  1098. searchTerm[i] = $(field).val();
  1099. if (searchTerm[i] !== "") {
  1100. if (searchTermType === "l") {
  1101. regExpr = new RegExp("^" +timeRegEx+ "$");
  1102. } else if (searchTermType === "t") {
  1103. var pieces = searchTerm[i].split(" ");
  1104. if (pieces.length === 2) {
  1105. regExpr = new RegExp("^" +dateRegEx+ " " +timeRegEx+ "$");
  1106. } else if (pieces.length === 1) {
  1107. regExpr = new RegExp("^" +dateRegEx+ "$");
  1108. }
  1109. } else if (searchTermType === "i") {
  1110. regExpr = new RegExp(integerRegEx);
  1111. } else if (searchTermType === "n") {
  1112. regExpr = new RegExp(numericRegEx);
  1113. if (searchTerm[i].charAt(0) === "-") {
  1114. searchTerm[i] = searchTerm[i].substr(1);
  1115. }
  1116. }
  1117. // string fields do not need validation
  1118. if (searchTermType !== "s") {
  1119. valid = regExpr.test(searchTerm[i]);
  1120. if (!valid) allValid = false;
  1121. }
  1122. addRemoveValidationIcons(valid, $(field), searchTermType);
  1123. /*
  1124. * Empty fields should not have valid/invalid indicator Range values
  1125. * are considered valid even if only the 'From' value is provided.
  1126. * Therefore, if the 'To' value is empty but the 'From' value is not
  1127. * empty we need to keep the validation icon on screen.
  1128. */
  1129. } else if (searchTerm[0] === "" && searchTerm[1] !== "" ||
  1130. searchTerm[0] === "" && searchTerm[1] === ""){
  1131. if ($(field).closest('div').children(':last-child').hasClass('checked-icon') ||
  1132. $(field).closest('div').children(':last-child').hasClass('not-available-icon')) {
  1133. $(field).closest('div').children(':last-child').remove();
  1134. }
  1135. }
  1136. if (!valid) {
  1137. return false;
  1138. }
  1139. });
  1140. });
  1141. return allValid;
  1142. }
  1143. function addRemoveValidationIcons(valid, field, searchTermType) {
  1144. var title = '';
  1145. if (searchTermType === 'i') {
  1146. title = $.i18n._('Input must be a positive number');
  1147. } else if (searchTermType === 'n') {
  1148. title = $.i18n._('Input must be a number');
  1149. } else if (searchTermType === 't') {
  1150. title = $.i18n._('Input must be in the format: yyyy-mm-dd');
  1151. } else if (searchTermType === 'l') {
  1152. title = $.i18n._('Input must be in the format: hh:mm:ss.t');
  1153. }
  1154. var validIndicator = " <span class='checked-icon sp-checked-icon'></span>",
  1155. invalidIndicator = " <span title='"+title+"' class='not-available-icon sp-checked-icon'></span>";
  1156. if (valid) {
  1157. if (!field.closest('div').children(':last-child').hasClass('checked-icon')) {
  1158. // remove invalid icon before adding valid icon
  1159. if (field.closest('div').children(':last-child').hasClass('not-available-icon')) {
  1160. field.closest('div').children(':last-child').remove();
  1161. }
  1162. field.closest('div').append(validIndicator);
  1163. }
  1164. } else {
  1165. if (!field.closest('div').children(':last-child').hasClass('not-available-icon')) {
  1166. // remove valid icon before adding invalid icon
  1167. if (field.closest('div').children(':last-child').hasClass('checked-icon')) {
  1168. field.closest('div').children(':last-child').remove();
  1169. }
  1170. field.closest('div').append(invalidIndicator);
  1171. }
  1172. }
  1173. }
  1174. /*
  1175. * Validation types: s => string i => integer n => numeric (positive/negative,
  1176. * whole/decimals) t => timestamp l => length
  1177. */
  1178. var validationTypes = {
  1179. "album_title" : "s",
  1180. "artist_name" : "s",
  1181. "bit_rate" : "i",
  1182. "bpm" : "i",
  1183. "comments" : "s",
  1184. "composer" : "s",
  1185. "conductor" : "s",
  1186. "copyright" : "s",
  1187. "cuein" : "l",
  1188. "cueout" : "l",
  1189. "encoded_by" : "s",
  1190. "utime" : "t",
  1191. "mtime" : "t",
  1192. "lptime" : "t",
  1193. "disc_number" : "i",
  1194. "genre" : "s",
  1195. "isrc_number" : "s",
  1196. "label" : "s",
  1197. "language" : "s",
  1198. "length" : "l",
  1199. "lyricist" : "s",
  1200. "mood" : "s",
  1201. "mime" : "s",
  1202. "name" : "s",
  1203. "orchestra" : "s",
  1204. "owner_id" : "s",
  1205. "rating" : "i",
  1206. "replay_gain" : "n",
  1207. "sample_rate" : "n",
  1208. "track_title" : "s",
  1209. "track_number" : "i",
  1210. "info_url" : "s",
  1211. "year" : "i"
  1212. };
  1213. $(document).ready(function() {
  1214. $('#editmdsave').live("click", function() {
  1215. var file_id = $('#file_id').val(),
  1216. data = $("#edit-md-dialog form").serializeArray();
  1217. $.post(baseUrl+'library/edit-file-md', {format: "json", id: file_id, data: data}, function() {
  1218. $("#edit-md-dialog").dialog().remove();
  1219. // don't redraw the library table if we are on calendar page
  1220. // we would be on calendar if viewing recorded file metadata
  1221. if ($("#schedule_calendar").length === 0) {
  1222. oTable.fnStandingRedraw();
  1223. }
  1224. });
  1225. });
  1226. $('#editmdcancel').live("click", function() {
  1227. $("#edit-md-dialog").dialog().remove();
  1228. });
  1229. $('#edit-md-dialog').live("keyup", function(event) {
  1230. if (event.keyCode === 13) {
  1231. $('#editmdsave').click();
  1232. }
  1233. });
  1234. });