From 14c69fe3f3537342d8487c37054298dd876a65f9 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Thu, 19 Jan 2012 12:58:47 +0000 Subject: [PATCH] Working on extracting card filter. --- .../src/main/webapp/js/cardFilter.js | 329 ++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 gemp-lotr/gemp-lotr-web/src/main/webapp/js/cardFilter.js diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/cardFilter.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/cardFilter.js new file mode 100644 index 000000000..bce670ac9 --- /dev/null +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/cardFilter.js @@ -0,0 +1,329 @@ +var CardFilter = Class.extend({ + communication: null, + clearCollectionFunc: null, + addCardFunc: null, + finishCollectionFunc: null, + + collectionType: null, + + filter: null, + start: 0, + count: 18, + + previousPageBut: null, + nextPageBut: null, + countSlider: null, + + setSelect: null, + nameInput: null, + sortSelect: null, + + init: function(url, elem, clearCollectionFunc, addCardFunc, finishCollectionFunc) { + this.communication = new GempLotrCommunication(url, + function(xhr, ajaxOptions, thrownError) { + }); + + this.clearCollectionFunc = clearCollectionFunc; + this.addCardFunc = addCardFunc; + this.finishCollectionFunc = finishCollectionFunc; + + this.filter = "cardType:-THE_ONE_RING"; + + this.buildUi(elem); + }, + + setCollectionType: function(collectionType) { + this.collectionType = collectionType; + this.start = 0; + this.getCollection(); + }, + + enableDetailFilters: function(enable) { + $("#culture1").buttonset("option", "disabled", !enable); + $("#culture2").buttonset("option", "disabled", !enable); + $("#cardType").prop("disabled", !enable); + $("#keyword").prop("disabled", !enable); + }, + + buildUi: function(elem) { + var that = this; + + var pageDiv = $("
"); + + this.previousPageBut = $("").button({ + text: false, + icons: { + primary: "ui-icon-circle-triangle-w" + }, + disabled: true + }).click( + function() { + that.disableNavigation(); + that.start -= that.count; + that.getCollection(); + }); + + this.nextPageBut = $("").button({ + text: false, + icons: { + primary: "ui-icon-circle-triangle-e" + }, + disabled: true + }).click( + function() { + that.disableNavigation(); + that.start += that.count; + that.getCollection(); + }); + + this.countSlider = $("
").slider({ + value:18, + min: 4, + max: 40, + step: 1, + disabled: true, + slide: function(event, ui) { + that.start = 0; + that.count = ui.value; + that.getCollection(); + } + }); + + pageDiv.append(this.previousPageBut); + pageDiv.append(this.nextPageBut); + pageDiv.append(this.countSlider); + + elem.append(pageDiv); + + var fullFilterDiv = $("
"); + this.setSelect = $(""); + this.nameInput = $(""); + this.sortSelect = $(""); + + fullFilterDiv.append(this.setSelect); + fullFilterDiv.append(this.nameInput); + fullFilterDiv.append(this.sortSelect); + + elem.append(fullFilterDiv); + + var filterDiv = $("
"); + + filterDiv.append("
" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "
"); + filterDiv.append("
" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "
"); + + var combos = $("
"); + + combos.append(" "); + combos.append(" "); + filterDiv.append(combos); + + elem.append(filterDiv); + + $("#culture1").buttonset(); + $("#culture2").buttonset(); + + var fullFilterChanged = function() { + that.start = 0; + that.getCollection(); + return true; + }; + + this.setSelect.change(fullFilterChanged); + this.nameInput.change(fullFilterChanged); + this.sortSelect.change(fullFilterChanged); + + var filterOut = function() { + that.filter = that.calculateNormalFilter(); + that.start = 0; + that.getCollection(); + return true; + }; + + $("#cardType").change(filterOut); + $("#keyword").change(filterOut); + + $("#labelDWARVEN,#labelELVEN,#labelGANDALF,#labelGONDOR,#labelROHAN,#labelSHIRE,#labelGOLLUM,#labelDUNLAND,#labelISENGARD,#labelMEN,#labelMORIA,#labelORC,#labelRAIDER,#labelSAURON,#labelURUK_HAI,#labelWRAITH").click(filterOut); + }, + + disableNavigation: function() { + this.previousPageBut.button("option", "disabled", true); + this.nextPageBut.button("option", "disabled", true); + this.countSlider.button("option", "disabled", true); + }, + + calculateNormalFilter: function() { + var cultures = new Array(); + $("label", $("#culture1")).each( + function() { + if ($(this).hasClass("ui-state-active")) + cultures.push($(this).prop("id").substring(5)); + }); + $("label", $("#culture2")).each( + function() { + if ($(this).hasClass("ui-state-active")) + cultures.push($(this).prop("id").substring(5)); + }); + + var cardType = $("#cardType option:selected").prop("value"); + if (cardType == "") + cardType = "cardType:-SITE,THE_ONE_RING"; + else + cardType = "cardType:" + cardType; + + var keyword = $("#keyword option:selected").prop("value"); + if (keyword != "") + keyword = " keyword:" + keyword; + + if (cultures.length > 0) + return cardType + " culture:" + cultures + keyword; + else + return cardType + keyword; + }, + + calculateFullFilterPostfix: function() { + var setNo = $("option:selected", this.setSelect).prop("value"); + if (setNo != "") + setNo = " set:" + setNo; + + var sort = $("option:selected", this.sortSelect).prop("value"); + if (sort != "") + sort = " sort:" + sort; + + var cardName = this.nameInput.val(); + if (cardName == "Card name") + cardName = ""; + else { + var cardNameElems = cardName.split(" "); + cardName = ""; + for (var i = 0; i < cardNameElems.length; i++) + cardName += " name:" + cardNameElems[i]; + } + + return setNo + sort + cardName; + }, + + getCollection: function() { + var that = this; + this.comm.getCollection(this.collectionType, this.filter + this.calculateFullFilterPostfix(), this.start, this.count, function(xml) { + that.displayCollection(xml); + }); + }, + + displayCollection: function(xml) { + log(xml); + var root = xml.documentElement; + if (root.tagName == "collection") { + this.clearCollectionFunc(); + + var packs = root.getElementsByTagName("pack"); + for (var i = 0; i < packs.length; i++) { + var packElem = packs[i]; + var blueprintId = packElem.getAttribute("blueprintId"); + var count = packElem.getAttribute("count"); + this.addCardFunc("pack", blueprintId, count, null, packElem.getAttribute("contents")); + } + + var cards = root.getElementsByTagName("card"); + for (var i = 0; i < cards.length; i++) { + var cardElem = cards[i]; + var blueprintId = cardElem.getAttribute("blueprintId"); + var count = cardElem.getAttribute("count"); + this.addCardFunc("card", blueprintId, count, cardElem.getAttribute("side"), null); + } + + this.finishCollectionFunc(); + + $("#previousPage").button("option", "disabled", this.start == 0); + var cnt = parseInt(root.getAttribute("count")); + $("#nextPage").button("option", "disabled", (this.start + this.count) >= cnt); + $("#countSlider").slider("option", "disabled", false); + } + } +}); \ No newline at end of file