window.SKT = function(selector) { this.site_title = "English National Opera"; this.site_url = "https:\/\/www.eno.org"; var View = function(selector) { var obj = jQuery(selector); var context = obj.prop('tagName'); if(obj.attr('id')) { context += '#' + obj.attr('id'); } if(obj.attr('class')) { context += '.' + obj.attr('class'); } return { updatable: function() { obj.find('a').on('click', function(e) { var self = jQuery(this); var href = self.attr('href'); var target = self.attr('target'); if(target) { return; } if(href.indexOf('?') > -1) { href += '&'; } else { href += '?'; } obj.trigger('ajax.loading'); href += 'skt-context=' + escape(context); e.preventDefault(); jQuery.ajax( { url: href, success: function(html) { var dom = jQuery(html); var target = dom.find(context); var view = new View(target.get(0)); obj.replaceWith(target).trigger('ajax.loaded'); view.updatable(); target.trigger('ajax.loaded'); } } ); } ); }, validatable: function() { obj.find('.skt-field').each( function() { var field = jQuery(this); var parent = field.parents('.skt-field'); var errorsDiv = jQuery('
').attr( 'data-for', '#' + field.attr('id') ); if(!parent || !parent.length) { field.after(errorsDiv); } field.attr('data-validatable', 'true'); field.on('change', function() { field.parent().find( 'div[data-for="#' + field.attr('id') + '"]' ).html(''); } ); } ); obj.closest('form').submit( function() { var canSubmit = true; var firstErrored = null; obj.find('.skt-field[data-validatable]').each( function() { var field = jQuery(this); var errors; var errorsDiv = field.parent().find( 'div[data-for="#' + field.attr('id') + '"]' ).html(''); field.removeData('validation-errors'); field.trigger('validate'); errors = field.data('validation-errors'); if(typeof(errors) != 'undefined' && errors) { canSubmit = false; for(var i = 0; i < errors.length; i ++) { errorsDiv.append( jQuery('
').addClass( 'skt-admin-error' ).text( errors[i] ) ); } if(!firstErrored) { firstErrored = field; } } } ); if(!canSubmit) { if(firstErrored) { firstErrored.focus(); } return false; } } ); } }; }; if(typeof(selector) == 'string') { return new View( document.querySelector(selector) ); } };