Commit cc8cc2ae authored by 神楽坂玲奈's avatar 神楽坂玲奈

search

parent 8dc364d9
...@@ -8,7 +8,6 @@ class Card extends Spine.Model ...@@ -8,7 +8,6 @@ class Card extends Spine.Model
@configure 'Card', 'id', 'name', 'card_type', 'type', 'attribute', 'level', 'atk', 'def', 'description' @configure 'Card', 'id', 'name', 'card_type', 'type', 'attribute', 'level', 'atk', 'def', 'description'
@extend Spine.Model.Local @extend Spine.Model.Local
@extend Spine.Events @extend Spine.Events
@hasMany 'card_usages', CardUsage
@url: "http://my-card.in/cards" @url: "http://my-card.in/cards"
@locale_url: "http://my-card.in/cards_#{locale}" @locale_url: "http://my-card.in/cards_#{locale}"
image_url: -> image_url: ->
...@@ -50,14 +49,14 @@ class Card extends Spine.Model ...@@ -50,14 +49,14 @@ class Card extends Spine.Model
cards_id = [] cards_id = []
for lang in langs for lang in langs
try try
result.push Card.find lang._id result.push Card.find(lang._id)
catch e catch e
cards_id.push lang._id cards_id.push lang._id
if cards_id.length if cards_id.length
$.getJSON "#{@url}?q=#{JSON.stringify({_id: { $in: cards_id}})}", (cards) => $.getJSON "#{@url}?q=#{JSON.stringify({_id: { $in: cards_id}})}", (cards) =>
@load cards, langs @load cards, langs
for card in cards for card in cards
result.push Card.find card._id result.push Card.find(card._id)
callback(result) callback(result)
else else
callback(result) callback(result)
...@@ -176,7 +175,6 @@ class Deck extends Spine.Model ...@@ -176,7 +175,6 @@ class Deck extends Spine.Model
else else
throw '无效卡组' throw '无效卡组'
card_usages.push {id: "#{result.cid}_#{side}_#{last_id}", card_id: last_id, side: side, count: count} if last_id card_usages.push {id: "#{result.cid}_#{side}_#{last_id}", card_id: last_id, side: side, count: count} if last_id
alert card_usages
result.card_usages card_usages result.card_usages card_usages
result result
...@@ -190,7 +188,7 @@ class Deck extends Spine.Model ...@@ -190,7 +188,7 @@ class Deck extends Spine.Model
"http://my-card.in" + @location_ydk() "http://my-card.in" + @location_ydk()
add: (card_usage)-> add: (card_usage)->
if !card_usage.card_id #card if !card_usage.card_id #card
card_usage = @card_usages().findByAttribute('card_id', card.id) || new CardUsage(card_id: card.id, deck_id: deck.id, main: true, count: 0) card_usage = @card_usages().findByAttribute('card_id', card.id) || new CardUsage(card_id: card_usage.id, deck_id: @id, main: true, count: 0)
count = 0 count = 0
for c in @card_usages().findAllByAttribute('card_id', card_usage.card_id) #TODO: alias for c in @card_usages().findAllByAttribute('card_id', card_usage.card_id) #TODO: alias
count += c.count count += c.count
...@@ -199,7 +197,7 @@ class Deck extends Spine.Model ...@@ -199,7 +197,7 @@ class Deck extends Spine.Model
card_usage.save() card_usage.save()
minus: (card_usage)-> minus: (card_usage)->
if !card_usage.card_id #card if !card_usage.card_id #card
card_usage = @card_usages().findByAttribute('card_id', card.id) card_usage = @card_usages().findByAttribute('card_id', card_usage.id)
return if !card_usage return if !card_usage
card_usage.count-- card_usage.count--
if card_usage.count if card_usage.count
...@@ -308,7 +306,7 @@ class DecksController extends Spine.Controller ...@@ -308,7 +306,7 @@ class DecksController extends Spine.Controller
else else
$('#deck_url_ydk').attr 'href', @deck().url_ydk() $('#deck_url_ydk').attr 'href', @deck().url_ydk()
@tab_control: -> tab_control: ->
$(".bottom_area div").click -> $(".bottom_area div").click ->
$(this).addClass("bottom_button_active").removeClass("bottom_button") $(this).addClass("bottom_button_active").removeClass("bottom_button")
$(this).siblings().addClass("bottom_button").removeClass("bottom_button_active") $(this).siblings().addClass("bottom_button").removeClass("bottom_button_active")
...@@ -322,16 +320,15 @@ class DecksController extends Spine.Controller ...@@ -322,16 +320,15 @@ class DecksController extends Spine.Controller
{btn: false}}) {btn: false}})
; ;
show: (e) -> show: (e) ->
card = $(e.target).tmplItem().data.card() card = $(e.target).tmplItem().data
@show_card(card) card = card.card() if card.card_id
show_card: (card)->
$('#card').removeClass(Card.card_types.join(' ')) $('#card').removeClass(Card.card_types.join(' '))
active_page_index = $('.bottom_area div').index $(".bottom_button_active") active_page_index = $('.bottom_area div').index $(".bottom_button_active")
$('#card').html $("#card_template").tmpl(card) $('#card').html $("#card_template").tmpl(card)
$('#card').addClass(card.card_type.join(' ')) $('#card').addClass(card.card_type.join(' '))
$('.card_frame .frame_element').eq(active_page_index).addClass('card_frame_focus') $('.card_frame .frame_element').eq(active_page_index).addClass('card_frame_focus')
$('.bottom_area div').eq(active_page_index).addClass('bottom_button_active').removeClass("bottom_button") $('.bottom_area div').eq(active_page_index).addClass('bottom_button_active').removeClass("bottom_button")
DecksController.tab_control() @tab_control()
add: (e)-> add: (e)->
@deck().add $(e.target).tmplItem().data @deck().add $(e.target).tmplItem().data
minus: (e)-> minus: (e)->
...@@ -340,22 +337,25 @@ class DecksController extends Spine.Controller ...@@ -340,22 +337,25 @@ class DecksController extends Spine.Controller
class CardsController extends Spine.Controller class CardsController extends Spine.Controller
events: events:
'mouseover .card_search_result': 'show', 'mouseover .search_card': 'show',
'click .card_search_result': 'add', 'click .search_card': 'add',
'contextmenu .card_search_result': 'minus' 'contextmenu .search_card': 'minus'
add: (e)-> add: (e)->
decks.deck().add($(this).tmplItem().data) decks.deck().add($(e.target).tmplItem().data)
minus: (e)-> minus: (e)->
decks.deck().minus($(this).tmplItem().data) e.preventDefault()
decks.deck().minus($(e.target).tmplItem().data)
show: (e)-> show: (e)->
decks.show_card($(this).tmplItem().data) decks.show(e)
template: ->
$('#search_cards_' + if $('.operate_area').hasClass('text') then 'text' else 'graphic' + '_template')
search: (name)-> search: (name)->
Card.fetch_by_name name, (cards)=> Card.fetch_by_name name, (cards)=>
@html $('#cards_search_result_template').tmpl cards @html @template().tmpl cards
decks = new DecksController(el: $("#deck")) decks = new DecksController(el: $("#deck"))
cards = new CardsController(el: $("#cards_search")) cards = new CardsController(el: $("#search_cards"))
$(document).ready -> $(document).ready ->
decks.load_from_url() decks.load_from_url()
...@@ -400,18 +400,18 @@ $(document).ready -> ...@@ -400,18 +400,18 @@ $(document).ready ->
$('#deck_load').change -> $('#deck_load').change ->
decks.upload(@files) decks.upload(@files)
window.addEventListener 'popstate', (ev)-> $(window).bind 'popstate', (ev)->
if ev.state if ev.state
deck.refresh ev.state, false deck.refresh ev.state, false
$('.main_div').bind 'dragover', (ev)-> $('.main_div').bind 'dragover', (ev)->
$("#drop_upload_dialog").dialog('open') ev.preventDefault();
false #$("#drop_upload_dialog").dialog('open')
$("#drop_upload_dialog").bind 'drop', (ev)-> $('.main_div').bind 'drop', (ev)->
ev.preventDefault();
$("#drop_upload_dialog").dialog('close') $("#drop_upload_dialog").dialog('close')
decks.upload event.dataTransfer.files decks.upload event.dataTransfer.files
false
$(".rename_ope").click -> $(".rename_ope").click ->
$(".text,.graphic").toggleClass("graphic text") $(".text,.graphic").toggleClass("graphic text")
......
...@@ -32,8 +32,6 @@ ...@@ -32,8 +32,6 @@
Card.extend(Spine.Events); Card.extend(Spine.Events);
Card.hasMany('card_usages', CardUsage);
Card.url = "http://my-card.in/cards"; Card.url = "http://my-card.in/cards";
Card.locale_url = "http://my-card.in/cards_" + locale; Card.locale_url = "http://my-card.in/cards_" + locale;
...@@ -415,7 +413,6 @@ ...@@ -415,7 +413,6 @@
count: count count: count
}); });
} }
alert(card_usages);
result.card_usages(card_usages); result.card_usages(card_usages);
return result; return result;
}; };
...@@ -440,8 +437,8 @@ ...@@ -440,8 +437,8 @@
var c, count, _i, _len, _ref; var c, count, _i, _len, _ref;
if (!card_usage.card_id) { if (!card_usage.card_id) {
card_usage = this.card_usages().findByAttribute('card_id', card.id) || new CardUsage({ card_usage = this.card_usages().findByAttribute('card_id', card.id) || new CardUsage({
card_id: card.id, card_id: card_usage.id,
deck_id: deck.id, deck_id: this.id,
main: true, main: true,
count: 0 count: 0
}); });
...@@ -460,7 +457,7 @@ ...@@ -460,7 +457,7 @@
Deck.prototype.minus = function(card_usage) { Deck.prototype.minus = function(card_usage) {
if (!card_usage.card_id) { if (!card_usage.card_id) {
card_usage = this.card_usages().findByAttribute('card_id', card.id); card_usage = this.card_usages().findByAttribute('card_id', card_usage.id);
} }
if (!card_usage) { if (!card_usage) {
return; return;
...@@ -689,7 +686,7 @@ ...@@ -689,7 +686,7 @@
} }
}; };
DecksController.tab_control = function() { DecksController.prototype.tab_control = function() {
$(".bottom_area div").click(function() { $(".bottom_area div").click(function() {
var $dangqian; var $dangqian;
$(this).addClass("bottom_button_active").removeClass("bottom_button"); $(this).addClass("bottom_button_active").removeClass("bottom_button");
...@@ -707,20 +704,18 @@ ...@@ -707,20 +704,18 @@
}; };
DecksController.prototype.show = function(e) { DecksController.prototype.show = function(e) {
var card; var active_page_index, card;
card = $(e.target).tmplItem().data.card(); card = $(e.target).tmplItem().data;
return this.show_card(card); if (card.card_id) {
}; card = card.card();
}
DecksController.prototype.show_card = function(card) {
var active_page_index;
$('#card').removeClass(Card.card_types.join(' ')); $('#card').removeClass(Card.card_types.join(' '));
active_page_index = $('.bottom_area div').index($(".bottom_button_active")); active_page_index = $('.bottom_area div').index($(".bottom_button_active"));
$('#card').html($("#card_template").tmpl(card)); $('#card').html($("#card_template").tmpl(card));
$('#card').addClass(card.card_type.join(' ')); $('#card').addClass(card.card_type.join(' '));
$('.card_frame .frame_element').eq(active_page_index).addClass('card_frame_focus'); $('.card_frame .frame_element').eq(active_page_index).addClass('card_frame_focus');
$('.bottom_area div').eq(active_page_index).addClass('bottom_button_active').removeClass("bottom_button"); $('.bottom_area div').eq(active_page_index).addClass('bottom_button_active').removeClass("bottom_button");
return DecksController.tab_control(); return this.tab_control();
}; };
DecksController.prototype.add = function(e) { DecksController.prototype.add = function(e) {
...@@ -745,27 +740,32 @@ ...@@ -745,27 +740,32 @@
} }
CardsController.prototype.events = { CardsController.prototype.events = {
'mouseover .card_search_result': 'show', 'mouseover .search_card': 'show',
'click .card_search_result': 'add', 'click .search_card': 'add',
'contextmenu .card_search_result': 'minus' 'contextmenu .search_card': 'minus'
}; };
CardsController.prototype.add = function(e) { CardsController.prototype.add = function(e) {
return decks.deck().add($(this).tmplItem().data); return decks.deck().add($(e.target).tmplItem().data);
}; };
CardsController.prototype.minus = function(e) { CardsController.prototype.minus = function(e) {
return decks.deck().minus($(this).tmplItem().data); e.preventDefault();
return decks.deck().minus($(e.target).tmplItem().data);
}; };
CardsController.prototype.show = function(e) { CardsController.prototype.show = function(e) {
return decks.show_card($(this).tmplItem().data); return decks.show(e);
};
CardsController.prototype.template = function() {
return $('#search_cards_' + ($('.operate_area').hasClass('text') ? 'text' : 'graphic' + '_template'));
}; };
CardsController.prototype.search = function(name) { CardsController.prototype.search = function(name) {
var _this = this; var _this = this;
return Card.fetch_by_name(name, function(cards) { return Card.fetch_by_name(name, function(cards) {
return _this.html($('#cards_search_result_template').tmpl(cards)); return _this.html(_this.template().tmpl(cards));
}); });
}; };
...@@ -778,7 +778,7 @@ ...@@ -778,7 +778,7 @@
}); });
cards = new CardsController({ cards = new CardsController({
el: $("#cards_search") el: $("#search_cards")
}); });
$(document).ready(function() { $(document).ready(function() {
...@@ -825,19 +825,18 @@ ...@@ -825,19 +825,18 @@
$('#deck_load').change(function() { $('#deck_load').change(function() {
return decks.upload(this.files); return decks.upload(this.files);
}); });
window.addEventListener('popstate', function(ev) { $(window).bind('popstate', function(ev) {
if (ev.state) { if (ev.state) {
return deck.refresh(ev.state, false); return deck.refresh(ev.state, false);
} }
}); });
$('.main_div').bind('dragover', function(ev) { $('.main_div').bind('dragover', function(ev) {
$("#drop_upload_dialog").dialog('open'); return ev.preventDefault();
return false;
}); });
$("#drop_upload_dialog").bind('drop', function(ev) { $('.main_div').bind('drop', function(ev) {
ev.preventDefault();
$("#drop_upload_dialog").dialog('close'); $("#drop_upload_dialog").dialog('close');
decks.upload(event.dataTransfer.files); return decks.upload(event.dataTransfer.files);
return false;
}); });
$(".rename_ope").click(function() { $(".rename_ope").click(function() {
$(".text,.graphic").toggleClass("graphic text"); $(".text,.graphic").toggleClass("graphic text");
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment