/*************************************************************************** * * Cargo Namespace * **************************************************************************/ var Cargo = { }; // Create a default objects Cargo.o = { }; // Default object defenitions (for backbone init) Cargo.o.model = { }; // Default object defenitions (for backbone init) Cargo.o.collection = { }; // Default object defenitions (for backbone init) Cargo.o.view = { }; // Default object defenitions (for backbone init) Cargo.Router = { }; // All main router methods Cargo.Template = { }; // Template controllers Cargo.Helper = { }; // Helper methods Cargo.API = { }; // API communication Cargo.Event = { }; // Backbone events (History, close, etc) Cargo.Collection = { }; // Collections base Cargo.Collection.Page = { }; // Project collection pages (for pagination in memory) Cargo.Collection.NavPage = { }; // Navigation collection pages (for pagination in memory) Cargo.Model = { }; // Models base Cargo.View = { }; // Views base /** * Action namespace */ var Action = window.Action || { }; /*************************************************************************** * * Cargo core functionality * This contains all core functionality native to all Cargo websites * For the most part, these methods are reproductions of the existing * functionality from site.package * **************************************************************************/ /* - Slideshow : _cargo/_core/slideshow.js - Handlebars : _cargo/_core/handlebars.js - Fullscreen : freshbox jQuery plugin in this file - Lightbox : freshbox jQuery plugin in this file - Keyboard nav : this file - Video player : _cargo/_core/video.js - Log : this file */ Cargo.Core = { /** * Make a safe logging method */ log : function(args) { if (typeof console == 'undefined' || !window.console) { return; } console.log(args); }, /** * Keyboard shortcuts * Add and Remove * Add Usage: * Cargo.Core.KeyboardShortcut.Add("Shift+F", 70, function(){ }); * Remove Usage: * Cargo.Core.KeyboardShortcut.Remove("Shift+F"); */ KeyboardShortcut : { Add : function(key, keycode, fn) { var data = { 'type' : 'keydown', 'disable_in_input' : true, 'keycode' : keycode }; shortcut.add(key, fn, data); }, Remove : function(key) { shortcut.remove(key); }, /* * Base set of keyboard shortcuts */ InitDefaults : function() { this.Add("J", 74, function() { Action.Project.Next() }); this.Add("K", 75, function() { Action.Project.Prev() }); this.Add("R", 82, function() { Action.Project.Random() }); this.Add("X", 88, function() { Action.Project.Index() }); this.Add("I", 73, function() { Action.Project.Index() }); this.Add("Shift+F", 70, function() { if ( ! $("body").hasClass("rail_active") ) { Cargo.app.Rail.Events.trigger("show_rail_following"); } else { Cargo.app.Rail.Events.trigger("rail:hide"); } }); this.Add("Shift+G", 71, function() { Cargo.Helper.GoToUrl("images"); }); this.Add("Shift+A", 65, function() { Cargo.Helper.GoToUrl("admin"); }); this.Add("Shift+E", 69, function() { if(Cargo.Model.Site.GetIsEditor() && Cargo.Model.Project.GetId()) { Cargo.Helper.GoToUrl("admin/"+Cargo.Model.Project.GetId()); } }); this.Add("Shift+H", 72, function() { Cargo.Helper.GoToUrl("admin/html"); }); this.Add("Shift+C", 67, function() { Cargo.Core.loadInspector3(); }); this.Add("Shift+D", 68, function() { Cargo.Helper.GoToUrl("admin/designs"); }); this.Add("Shift+X", 88, function() { Cargo.Helper.GoToUrl(""); }); this.Add("Shift+I", 73, function() { loadInspector(); }); /*this.Add("Ctrl+Shift+I", 73, function() { loadInspector(); });*/ this.Add("Esc", 27, function() { $("a.follow_popup_cancel").click(); if ( ! $("#freshbox").length > 0 ) { Cargo.app.Rail.Events.trigger("rail:hide"); } }); } }, /** * Replace the old loading animations with retnified versions * * Note: This sits in a timeout because it won't fire correctly * on doc.ready */ ReplaceLoadingAnims : { init : function(specificSelection) { // First, check for existing spinners $(".retinaSpinner").each(function() { if (window.devicePixelRatio > 1 ){ $(this).addClass('rotating'); } }); setTimeout(function() { if(Cargo.Helper && !Cargo.Helper.isOldIE()){ var spinnerEl = $('
'); for(var i = 0; i < 8; i++){ spinnerEl.append( '
' + '
' + '
'); } if(typeof specificSelection == "undefined" && $('img[src$="/loadingAnim.gif"]').length > 0){ $('img[src$="/loadingAnim.gif"]').each(function(){ Cargo.Core.ReplaceLoadingAnims.replace(this, spinnerEl); }); } else if(typeof specificSelection != "undefined") { specificSelection.each(function(){ Cargo.Core.ReplaceLoadingAnims.replace(this, spinnerEl); }); } Cargo.Event.trigger("loading_anims_replaced"); } }, 100); }, replace : function(which, spinnerEl) { var thisSpinner = spinnerEl.clone(); thisSpinner.attr('class', 'retinaSpinner ' + $(which).attr('class')); thisSpinner.attr('id', $(which).attr('id')); thisSpinner.attr('style', $(which).attr('style')); thisSpinner.attr('data-loading', $(which).attr('data-loading')); if (window.devicePixelRatio > 1 ){ thisSpinner.addClass('rotating'); } // replace the old GIF $(which).replaceWith(thisSpinner); } }, /** * This method is called by events.js on project_load_complete * It loads the template, and loads the view into Memory * The view for the freshbox handles the click events */ CheckAndInitFreshbox : function() { // Remove the fullscreen from offline mode if( Cargo.API.Config.offline ) { return false; } /* Check for a fullscreen button perpare it */ if( $("#fullscreen").length > 0 || Cargo.Model.DisplayOptions.GetLightboxView() ) { /* Prevent multiple instantiation */ if(typeof Cargo.View.Fullscreen == "undefined") { /* Add the template to the stack */ Cargo.Template.Add( "Freshbox" ); /* Make sure we have loaded the freshbox */ Cargo.Template.Load( "Freshbox", Cargo.API.GetFreshboxPath() ); /* Initialize the fullscreen view */ Cargo.View.Fullscreen = new Cargo.o.view.Freshbox({ el : "body", model : Cargo.Model.Project }); } } }, /** * This finds the search data-view and loads the search view * if one is not already loaded */ FindAndLoadSearch : function(first_load) { if(typeof Cargo.View.Search != "undefined" && !first_load) { return false; } if($("[data-view='search']").length > 0) { Cargo.View.Search = new Cargo.o.view.Search({ el : "[data-view='search']" }); } }, /** * This method is meant to reseed the project data, rather then refreshing the whole page * This is called from the admin frameset upon project ordering and toggle */ ReseedProjectModels : function() { // Reset the page and offset count Cargo.API.Config.current_offset = 0; Cargo.API.Config.current_page = 0; // Get the URL of the API path Cargo.Collection.Navigation.url = Cargo.API.GetNavigationCargoPath( 0 ); Cargo.Collection.Projects.url = Cargo.API.GetProjectsPath( 0 ); // Fetch the data Cargo.Collection.Projects.fetch({reset: true, complete: function() { Cargo.Event.Common(); Cargo.Event.trigger("reseed_project_complete"); }}); Cargo.Collection.Navigation.fetch({reset: true, complete: function() { Cargo.Event.trigger("reseed_navigation_complete"); }}); }, loadInspector3 : function(){ if(window.editor !== undefined){ return false; } Cargo.Core.Inspector3.load({ onInit: function(inspector){ function addClickBehavior(){ // replace all click events handled by Backbone with dblClick events. _.each(Cargo.View, function(view){ // remove all events bound to this view view.undelegateEvents(); _.each(view.events, function(method, key){ var match = key.match(delegateEventSplitter); if(match !== null) { // found a click event. Add a dblclick in place. if(match[1] === "click") { view.events['dblclick ' + match[2]] = method; delete view.events[key]; } } }); // re-attached all events including the modified ones. view.delegateEvents(); }); $("a:not([rel], #splash)").unbind("click").off('dblclick').on("dblclick", function() { var href = this.getAttribute('href'); if(href !== '#' && href !== '' && href.indexOf('javascript:') === -1){ var cargoURL = (Cargo.hasOwnProperty('Helper') ? Cargo.Helper.GetUrl() : Cargo.Config.GetCookieUrl()), customDomain = inspector.options.coreWindow.location.hostname.search(/cargocollective\.com/i) === -1, sameDomain = (this.hostname.search(/cargocollective\.com/i) !== -1 ? this.pathname.split('/')[1] === cargoURL : this.hostname === inspector.options.coreWindow.location.hostname), subLocation = this.pathname.split('/')[(customDomain ? 1 : 2)], forbiddenSubLocation = (subLocation === undefined ? false : subLocation.search(/^(following|admin|images)$/i) !== -1); //console.log(cargoURL, customDomain, subLocation, sameDomain, forbiddenSubLocation); if(sameDomain && !forbiddenSubLocation) { inspector.options.coreDocument.location.href = this.getAttribute('href'); } else { var newTab = inspector.options.coreWindow.open(href, '_blank'); newTab.focus(); } } }); $('img').unbind('click'); if(Cargo.Core.Slideshow.hasOwnProperty('SlideshowObjects')){ $.each(Cargo.Core.Slideshow.SlideshowObjects, function(key, val){ val.elements.$thumbnails.on('dblclick', function(){ val.hideThumbnails(); val.showContainer(); }); }) } } var $ = inspector.options.coreWindow.$, Cargo = inspector.options.coreWindow.Cargo, delegateEventSplitter = /^(\S+)\s*(.*)$/; setTimeout(function(){ Cargo.app.Rail.Events.on("toolset:loaded", function() { $("#toolset, #toolset *, #toolset_menu, #toolset_menu *, #design_panel, #design_panel *").unbind().off(); $("#toolset_follow_pair a, #toolset_follow a").unbind(); $("#toolset").fadeTo('fast', 0.3).css('pointer-events', 'none'); }); $("#toolset, #toolset *, #toolset_menu, #toolset_menu *, #design_panel, #design_panel *").unbind().off(); $("#toolset_follow_pair a, #toolset_follow a").unbind(); $("#toolset").fadeTo('fast', 0.3).css('pointer-events', 'none'); }, 500); $("#toolset").fadeTo('fast', 0.3).css('pointer-events', 'none'); Cargo.Event.on("pagination_complete", function( new_page ) { addClickBehavior(); }); Cargo.Event.on("project_load_complete", function( new_page ) { addClickBehavior(); }); $('.slideshow *').unbind().off(); addClickBehavior(); } }); }, /** * Loads the inspector */ LoadInspector : function() { this.loadInspector3(); /* if($("#inspector").length > 0) { // inspector is loaded } else { var dependencies = [ "/_jsapps/inspector/2.0/interceptions.js", "/_jsapps/inspector/2.0/jquery-ui.1.10.3.min.js", "/_jsapps/inspector/codemirror-2.15/codemirror-package.js", "/_jsapps/inspector/2.0/inspector.css", "/_jsapps/inspector/jquery.mini.colors.js" ]; $.loadFiles(dependencies).done(function() { $.post("/dispatch/admin/loadInspector/" + Cargo.Helper.GetUrl(), {}, function(data) { if (data.error == "") { var jdata = $.parseJSON(data.jdata); // load custom interceptions if present. $.ajax({ url: '/_jsapps/inspector/custom_interceptions/' + Cargo.Helper.GetDesignName().toLowerCase() + '-interceptions.js', dataType: 'script' }).always(function(d){ // load the actual inspector $.loadFiles("/_jsapps/inspector/2.0/inspector-2.0.js").done(function() { // Make sure they didn't double click if( $("#inspector").length <= 0 ) { CSS.Sheet.ParseAndSet(jdata.css_data); // Load the CSS data Inspector.CustomHTML.SetData(jdata.html_data); // Load the HTML data $("body").append(data.html); // append the inspector Inspector.init(jdata.inspector_position); // Initialize } }); }); } else if (data.error == "Refresh") { document.location.href = document.location + "?inspector"; } else { alert("Log in to use the Inspector"); } }, "json"); }); } */ }, HiRes : { active : false, Queue : { STATE_LOADING: 0, STATE_COMPLETE: 1, STATE_ERROR: -1, ID: 'hi-res-queue', FILE_EXTENSION: /(?:\.([^.]+))?$/, FILE_NAME: /(?:\/([^\/]+))?$/, element: null, init: function() { $('body').append('