{"action":"created","comment":{"url":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments/28680105","id":28680105,"diff_hunk":"@@ -1,37 +1,270 @@\n var cdb = require('cartodb.js');\n-var Backbone = require('backbone');\n-var ImportsModel = require('../../imports_model');\n+var _ = require('underscore');\n+var ImportsModel = require('../../background_importer/imports_model');\n+var UploadModel = require('../../background_importer/upload_model');\n+var VisFetchModel = require('../../visualizations_fetch_model');\n+var LocalStorage = require('../../local_storage');\n+var CreateOnboarding = require('./create_onboarding');\n \n /**\n  *  This model will be on charge of create a new map\n  *  using user selected datasets, where they can be\n  *  already imported datasets or remote (and needed to import)\n- *  datasets.    \n- *\n+ *  datasets.\n  */\n-\n module.exports = cdb.core.Model.extend({\n \n   defaults: {\n     type: 'map',\n+    option: 'templates',\n     currentImport: null,\n     tableIdsArray: [],\n-    selectedDatasets: []\n+    listing: 'datasets' // [import, datasets, scratch]\n   },\n \n   _DEFAULT_MAP_NAME: 'Untitled Map',\n \n   initialize: function(attrs, opts) {\n     this.user = opts.user;\n+    this.upload = new UploadModel({\n+      create_vis: true\n+    }, {\n+      user: this.user\n+    });\n+    this.mapTemplate = new cdb.core.Model();\n+    this.vis = new cdb.admin.Visualization({ name: 'Untitled map' });\n+\n+    this.bind('change:mapTemplate', this._onTemplateChange, this);\n+    this.bind('change:option', this._onOptionChange, this);\n+    this.mapTemplate.bind('change', function() {\n+      this.trigger('change:mapTemplate', this);\n+    }, this);\n+    this.upload.bind('change', function() {\n+      this.trigger('change:upload', this);\n+    }, this);\n+\n+    this.collection = new cdb.admin.Visualizations(opts.selectedDatasets);\n+    this.collection.each(function(model) {\n+      model.set('selected', true);\n+    });\n+    this.visFetchModel = new VisFetchModel({\n+      content_type: 'datasets',\n+      library: this.showLibrary()\n+    });\n+    this.visFetchModel.bind('change', this._fetchCollection, this);\n+    this.bind('change:option', this.maybePrefetchDatasets, this);\n+    this.bind('change:listing', this.maybePrefetchDatasets, this);\n+    this.localStorage = new LocalStorage();\n+  },\n+\n+  // For create-content\n+  createOnboardingView: function() {\n+    if (!this.localStorage.get('onboarding-create-map')) {\n+      return new CreateOnboarding({\n+        localStorage: this.localStorage,\n+        model: this\n+      });\n+    }\n+  },\n+\n+  // For create-listing view\n+  showLibrary: function() {\n+    return false;\n+  },\n+\n+  // For create-listing view\n+  showDatasets: function() {\n+    return true;\n+  },\n+\n+  // For create-listing view\n+  createEmptyLabel: function() {\n+    return 'Create empty map';\n+  },\n+\n+  // For create-listing view\n+  isListingSomething: function() {\n+    return this.getOption() === 'listing';\n+  },\n+\n+  // For create-listing view\n+  canChangeSelectedState: function(datasetModel) {\n+    if (datasetModel.get('selected')) {\n+      return true;\n+    } else {\n+      return this._selectedItems().length < this.user.get('max_layers');\n+    }\n   },\n \n-  start: function() {\n+  // Get option state (it could be templates, preview or listing)\n+  getOption: function() {\n+    var option = this.get('option');\n+    var states = option.split('.');\n+\n+    if (states.length > 0) {\n+      return states[0];\n+    }\n+\n+    return '';\n+  },\n+\n+  // Get import state (it could be any of the possibilities of the import options, as in scratch, dropbox, etc...)\n+  // For create-footer view\n+  getImportState: function() {\n+    var option = this.get('option');\n+    var states = option.split('.');\n+\n+    if (states.length > 0 && states.length < 4 && states[0] === \"listing\" && states[1] === \"import\") {\n+      return states[2];\n+    }\n+\n+    return '';\n+  },\n+\n+  // For footer\n+  showTypeGuessingToggler: function() {\n+    return true;\n+  },\n+\n+  // For create-listing-import view\n+  setActiveImportPane: function(option) {\n+    if (option && this.get('listing') === \"import\" && this.getImportState() !== option) {\n+      this.set('option', 'listing.import.' + option);\n+    }\n+  },\n+\n+  // For create-footer view\n+  isMapType: function() {\n+    return true;\n+  },\n+\n+  // For create-listing-import view\n+  getUpload: function() {\n+    return this.upload.toJSON();\n+  },\n+\n+  // For create-footer view\n+  getSelectedDatasets: function() {\n+    return this._selectedItems();\n+  },\n+\n+  // For create-listing-import view\n+  setUpload: function(d) {\n+    if (d && !_.isEmpty(d)) {\n+      // Set upload properties except create_vis (defined at the beginning)\n+      this.upload.set(_.omit(d, 'create_vis'));\n+    } else {\n+      this.upload.clear();\n+    }\n+  },\n+  getMapTemplate: function() {\n+    return this.mapTemplate.toJSON();\n+  },\n+\n+  setMapTemplate: function(mdl) {\n+    if (mdl) {\n+      this.mapTemplate.set(mdl.toJSON());\n+    }\n+  },\n+\n+  createMap: function() {\n+    if (this._selectedItems().length === 0) {\n+      return;\n+    }\n+    this.set('option', 'loading');\n     this._checkCollection();\n   },\n \n+  // For create-from-scratch view\n+  titleForCreateFromScratch: function() {\n+    return 'Create a new map from scratch';\n+  },\n+\n+  // For create-from-scratch view\n+  explainWhatHappensAfterCreatedFromScratch: function() {\n+    return 'We will redirect you once it finishes';\n+  },\n+\n+  // For create-from-scratch view\n+  labelForCreateFromScratchButton: function() {\n+    return 'Create map';\n+  },\n+\n+  // For create-from-scratch view\n+  createFromScratch: function() {\n+    this.trigger('creatingDataset', 'dataset', this);\n+    this.set('option', 'loading');","path":"lib/assets/javascripts/cartodb/new_common/dialogs/create/create_map_model.js","position":203,"original_position":203,"commit_id":"019af898b2e185aab62e66af812da3c43efc3843","original_commit_id":"019af898b2e185aab62e66af812da3c43efc3843","user":{"login":"javisantana","id":30147,"avatar_url":"https://avatars.githubusercontent.com/u/30147?v=3","gravatar_id":"","url":"https://api.github.com/users/javisantana","html_url":"https://github.com/javisantana","followers_url":"https://api.github.com/users/javisantana/followers","following_url":"https://api.github.com/users/javisantana/following{/other_user}","gists_url":"https://api.github.com/users/javisantana/gists{/gist_id}","starred_url":"https://api.github.com/users/javisantana/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/javisantana/subscriptions","organizations_url":"https://api.github.com/users/javisantana/orgs","repos_url":"https://api.github.com/users/javisantana/repos","events_url":"https://api.github.com/users/javisantana/events{/privacy}","received_events_url":"https://api.github.com/users/javisantana/received_events","type":"User","site_admin":false},"body":"why do we use there a change in option and a custom trigger. Wouldn't be enough to change something in the model? Not that important but in my experience custom signals are something we should avoid ","created_at":"2015-04-20T11:06:58Z","updated_at":"2015-04-20T11:06:58Z","html_url":"https://github.com/CartoDB/cartodb/pull/3277#discussion_r28680105","pull_request_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277","_links":{"self":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments/28680105"},"html":{"href":"https://github.com/CartoDB/cartodb/pull/3277#discussion_r28680105"},"pull_request":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277"}}},"pull_request":{"url":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277","id":33649250,"html_url":"https://github.com/CartoDB/cartodb/pull/3277","diff_url":"https://github.com/CartoDB/cartodb/pull/3277.diff","patch_url":"https://github.com/CartoDB/cartodb/pull/3277.patch","issue_url":"https://api.github.com/repos/CartoDB/cartodb/issues/3277","number":3277,"state":"open","locked":false,"title":"New add layer modal","user":{"login":"viddo","id":978461,"avatar_url":"https://avatars.githubusercontent.com/u/978461?v=3","gravatar_id":"","url":"https://api.github.com/users/viddo","html_url":"https://github.com/viddo","followers_url":"https://api.github.com/users/viddo/followers","following_url":"https://api.github.com/users/viddo/following{/other_user}","gists_url":"https://api.github.com/users/viddo/gists{/gist_id}","starred_url":"https://api.github.com/users/viddo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/viddo/subscriptions","organizations_url":"https://api.github.com/users/viddo/orgs","repos_url":"https://api.github.com/users/viddo/repos","events_url":"https://api.github.com/users/viddo/events{/privacy}","received_events_url":"https://api.github.com/users/viddo/received_events","type":"User","site_admin":false},"body":"Fixes #3161 since this modal is rather big it will only support adding a single layer in this first iteration.\r\n\r\nThis PR became rather big, but it was necessary to accommodate existing code to work with this new use-case context.\r\n\r\nBasically, the reused part is this: ![screen shot 2015-04-20 at 11 22 00](https://cloud.githubusercontent.com/assets/978461/7227305/03b613ec-e750-11e4-9091-6833112307d1.png) and it's underlying views/models. The header and footer differed so much it made no sense to reuse them.\r\n\r\n- While working on this I observed that most of the **views do too much**. I had to spend a lot time to understand what was going on, and **extract non-view logic** that do not belong in a view **to appropriate models**. In many cases some logic are only relevant for a certain use-case, why I used view-models to place that logic, instead of resorting to a bunch of if-ifelse-else cases which only would have complicated things even further.\r\n- Background importer process is another part of the system that was necessary to change for this feature, followed same approach as explained above there too.\r\n- All code related to the background-importer was moved to `new_common/background_importer` to keep things more tidy and now being shared for both editor and dashboard.\r\n\r\nI'll add inline comments with the code changes, to further explain details in-context.","created_at":"2015-04-20T09:39:30Z","updated_at":"2015-04-20T11:06:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"21eaa346ed3fe5e5dc35f5742e2d5d774c500953","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277/commits","review_comments_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277/comments","review_comment_url":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CartoDB/cartodb/issues/3277/comments","statuses_url":"https://api.github.com/repos/CartoDB/cartodb/statuses/019af898b2e185aab62e66af812da3c43efc3843","head":{"label":"CartoDB:3161-add-layer-modal","ref":"3161-add-layer-modal","sha":"019af898b2e185aab62e66af812da3c43efc3843","user":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"repo":{"id":4909355,"name":"cartodb","full_name":"CartoDB/cartodb","owner":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CartoDB/cartodb","description":"Geospatial database for the cloud","fork":false,"url":"https://api.github.com/repos/CartoDB/cartodb","forks_url":"https://api.github.com/repos/CartoDB/cartodb/forks","keys_url":"https://api.github.com/repos/CartoDB/cartodb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CartoDB/cartodb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CartoDB/cartodb/teams","hooks_url":"https://api.github.com/repos/CartoDB/cartodb/hooks","issue_events_url":"https://api.github.com/repos/CartoDB/cartodb/issues/events{/number}","events_url":"https://api.github.com/repos/CartoDB/cartodb/events","assignees_url":"https://api.github.com/repos/CartoDB/cartodb/assignees{/user}","branches_url":"https://api.github.com/repos/CartoDB/cartodb/branches{/branch}","tags_url":"https://api.github.com/repos/CartoDB/cartodb/tags","blobs_url":"https://api.github.com/repos/CartoDB/cartodb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CartoDB/cartodb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CartoDB/cartodb/git/refs{/sha}","trees_url":"https://api.github.com/repos/CartoDB/cartodb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CartoDB/cartodb/statuses/{sha}","languages_url":"https://api.github.com/repos/CartoDB/cartodb/languages","stargazers_url":"https://api.github.com/repos/CartoDB/cartodb/stargazers","contributors_url":"https://api.github.com/repos/CartoDB/cartodb/contributors","subscribers_url":"https://api.github.com/repos/CartoDB/cartodb/subscribers","subscription_url":"https://api.github.com/repos/CartoDB/cartodb/subscription","commits_url":"https://api.github.com/repos/CartoDB/cartodb/commits{/sha}","git_commits_url":"https://api.github.com/repos/CartoDB/cartodb/git/commits{/sha}","comments_url":"https://api.github.com/repos/CartoDB/cartodb/comments{/number}","issue_comment_url":"https://api.github.com/repos/CartoDB/cartodb/issues/comments{/number}","contents_url":"https://api.github.com/repos/CartoDB/cartodb/contents/{+path}","compare_url":"https://api.github.com/repos/CartoDB/cartodb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CartoDB/cartodb/merges","archive_url":"https://api.github.com/repos/CartoDB/cartodb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CartoDB/cartodb/downloads","issues_url":"https://api.github.com/repos/CartoDB/cartodb/issues{/number}","pulls_url":"https://api.github.com/repos/CartoDB/cartodb/pulls{/number}","milestones_url":"https://api.github.com/repos/CartoDB/cartodb/milestones{/number}","notifications_url":"https://api.github.com/repos/CartoDB/cartodb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CartoDB/cartodb/labels{/name}","releases_url":"https://api.github.com/repos/CartoDB/cartodb/releases{/id}","created_at":"2012-07-05T15:23:45Z","updated_at":"2015-04-20T10:33:23Z","pushed_at":"2015-04-20T10:56:10Z","git_url":"git://github.com/CartoDB/cartodb.git","ssh_url":"git@github.com:CartoDB/cartodb.git","clone_url":"https://github.com/CartoDB/cartodb.git","svn_url":"https://github.com/CartoDB/cartodb","homepage":"www.cartodb.com","size":478119,"stargazers_count":779,"watchers_count":779,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":178,"mirror_url":null,"open_issues_count":500,"forks":178,"open_issues":500,"watchers":779,"default_branch":"master"}},"base":{"label":"CartoDB:master","ref":"master","sha":"051125e726b292b83b922b0a7d0fbafdd1316979","user":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"repo":{"id":4909355,"name":"cartodb","full_name":"CartoDB/cartodb","owner":{"login":"CartoDB","id":1799254,"avatar_url":"https://avatars.githubusercontent.com/u/1799254?v=3","gravatar_id":"","url":"https://api.github.com/users/CartoDB","html_url":"https://github.com/CartoDB","followers_url":"https://api.github.com/users/CartoDB/followers","following_url":"https://api.github.com/users/CartoDB/following{/other_user}","gists_url":"https://api.github.com/users/CartoDB/gists{/gist_id}","starred_url":"https://api.github.com/users/CartoDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CartoDB/subscriptions","organizations_url":"https://api.github.com/users/CartoDB/orgs","repos_url":"https://api.github.com/users/CartoDB/repos","events_url":"https://api.github.com/users/CartoDB/events{/privacy}","received_events_url":"https://api.github.com/users/CartoDB/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CartoDB/cartodb","description":"Geospatial database for the cloud","fork":false,"url":"https://api.github.com/repos/CartoDB/cartodb","forks_url":"https://api.github.com/repos/CartoDB/cartodb/forks","keys_url":"https://api.github.com/repos/CartoDB/cartodb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CartoDB/cartodb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CartoDB/cartodb/teams","hooks_url":"https://api.github.com/repos/CartoDB/cartodb/hooks","issue_events_url":"https://api.github.com/repos/CartoDB/cartodb/issues/events{/number}","events_url":"https://api.github.com/repos/CartoDB/cartodb/events","assignees_url":"https://api.github.com/repos/CartoDB/cartodb/assignees{/user}","branches_url":"https://api.github.com/repos/CartoDB/cartodb/branches{/branch}","tags_url":"https://api.github.com/repos/CartoDB/cartodb/tags","blobs_url":"https://api.github.com/repos/CartoDB/cartodb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CartoDB/cartodb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CartoDB/cartodb/git/refs{/sha}","trees_url":"https://api.github.com/repos/CartoDB/cartodb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CartoDB/cartodb/statuses/{sha}","languages_url":"https://api.github.com/repos/CartoDB/cartodb/languages","stargazers_url":"https://api.github.com/repos/CartoDB/cartodb/stargazers","contributors_url":"https://api.github.com/repos/CartoDB/cartodb/contributors","subscribers_url":"https://api.github.com/repos/CartoDB/cartodb/subscribers","subscription_url":"https://api.github.com/repos/CartoDB/cartodb/subscription","commits_url":"https://api.github.com/repos/CartoDB/cartodb/commits{/sha}","git_commits_url":"https://api.github.com/repos/CartoDB/cartodb/git/commits{/sha}","comments_url":"https://api.github.com/repos/CartoDB/cartodb/comments{/number}","issue_comment_url":"https://api.github.com/repos/CartoDB/cartodb/issues/comments{/number}","contents_url":"https://api.github.com/repos/CartoDB/cartodb/contents/{+path}","compare_url":"https://api.github.com/repos/CartoDB/cartodb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CartoDB/cartodb/merges","archive_url":"https://api.github.com/repos/CartoDB/cartodb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CartoDB/cartodb/downloads","issues_url":"https://api.github.com/repos/CartoDB/cartodb/issues{/number}","pulls_url":"https://api.github.com/repos/CartoDB/cartodb/pulls{/number}","milestones_url":"https://api.github.com/repos/CartoDB/cartodb/milestones{/number}","notifications_url":"https://api.github.com/repos/CartoDB/cartodb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CartoDB/cartodb/labels{/name}","releases_url":"https://api.github.com/repos/CartoDB/cartodb/releases{/id}","created_at":"2012-07-05T15:23:45Z","updated_at":"2015-04-20T10:33:23Z","pushed_at":"2015-04-20T10:56:10Z","git_url":"git://github.com/CartoDB/cartodb.git","ssh_url":"git@github.com:CartoDB/cartodb.git","clone_url":"https://github.com/CartoDB/cartodb.git","svn_url":"https://github.com/CartoDB/cartodb","homepage":"www.cartodb.com","size":478119,"stargazers_count":779,"watchers_count":779,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":178,"mirror_url":null,"open_issues_count":500,"forks":178,"open_issues":500,"watchers":779,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277"},"html":{"href":"https://github.com/CartoDB/cartodb/pull/3277"},"issue":{"href":"https://api.github.com/repos/CartoDB/cartodb/issues/3277"},"comments":{"href":"https://api.github.com/repos/CartoDB/cartodb/issues/3277/comments"},"review_comments":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277/comments"},"review_comment":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CartoDB/cartodb/pulls/3277/commits"},"statuses":{"href":"https://api.github.com/repos/CartoDB/cartodb/statuses/019af898b2e185aab62e66af812da3c43efc3843"}}}}