class CheckAuth # simple javascript template, # i hope I'm right. # ok, I was wrong :( @defaults: onStart: -> $('#login_holder').parent().addClass('loading') $('.menu_header .menu_order').removeClass('signed') onSuccess: (result, user)-> $('.menu_header .menu_order').addClass('signed') onFinish: (result, user={})-> $('#login_holder').parent().removeClass('loading') onFail: null processData: null @renderAuth: (attrs={}, user={})-> if attrs.processData && $.isFunction(attrs.processData) data = attrs.processData.call(this, user) else data = name: user.name notify: user.notifications_count hasnotify: (user.notifications_count && user.notifications_count > 0) signed: !!user.name unsigned: !user.name $('[check-auth-position]').each (index, tmpl)-> _tmpl = $(tmpl) target = $(_tmpl.attr('check-auth-position')) if target.length > 0 rslt = Mustache.render(_tmpl.html(), data) target.html(rslt) @fillFeedbackForm: (data)-> form = $(".feedback_form").first() if form[0] name = form.find("[name='feedback[name]'], [name='testimonial[name]']") email = form.find("[name='feedback[email]'], [name='testimonial[email]']") if name[0] && name.val().length <= 0 name.val(data['name']) name.trigger('change') if email[0] && email.val().length <= 0 email.val(data['email']) email.trigger('change') @autoFillForm: (data)-> @fillFeedbackForm(data) @check: (attrs)-> attrs = $.extend(true, @defaults, attrs) attrs.onStart.call(this) if $.isFunction(attrs.onStart) $.ajax url: "/account/session.json" dataType: "JSON" type: "GET" success: (data) => if data this.renderAuth(attrs, data.attributes) CheckAuth.autoFillForm(data.attributes) attrs.onSuccess.call(this, data.attributes) if $.isFunction(attrs.onSuccess) else this.renderAuth(attrs) attrs.onFail.call(this) if $.isFunction(attrs.onFail) attrs.onFinish.call(this, data && data.attributes) if $.isFunction(attrs.onFinish) window.CheckAuth = CheckAuth