{"action":"created","comment":{"url":"https://api.github.com/repos/dimagi/Vellum/pulls/comments/24339834","id":24339834,"diff_hunk":"@@ -598,27 +598,55 @@ define([\n     }\n \n     function parseControlTree (form, controlsTree) {\n-        function eachFunc(cEl, parentMug) {\n+        function eachFunc(cEl, parentMug, index) {\n             var $cEl = $(cEl),\n-                mug = parseControlElement(form, $cEl, parentMug);\n+                mug = parseControlElement(form, $cEl, parentMug),\n+                tree = form.tree,\n+                node = tree.getNodeFromMug(mug);\n+\n+            if (!node) {\n+                mug.options.isControlOnly = true;\n+                form.tree.insertMug(mug, 'into', parentMug);\n+                node = tree.getNodeFromMug(mug);\n+            }\n \n-            form.controlTree.insertMug(mug, 'into', parentMug);\n             if (mug.options.isControlOnly) {\n                 // HACK fix abstraction broken by direct tree insert\n                 form.mugMap[mug.ufid] = mug;\n             }\n \n-            if (mug.__className === \"ReadOnly\") {\n-                return;\n+            // preserve the control order \n+            var siblings = node.parent.getChildren(),\n+                realIndex = index,\n+                numControlStructs = 0,\n+                numChildren = siblings.length;\n+\n+            for (var i = 0; i < numChildren; i++) {\n+                if (siblings[i].value.options.isDataOnly) {\n+                    realIndex++;\n+                } else {\n+                    numControlStructs++;\n+                }\n+\n+                if (numControlStructs > index) {\n+                    break;\n+                }\n             }\n-            if (mug.options.controlNodeChildren) {\n+\n+            form.tree.insertMug(mug, 'index', parentMug, realIndex);\n+\n+            if (mug.__className !== \"ReadOnly\" && mug.options.controlNodeChildren) {\n+                var index1 = 0;","path":"src/parser.js","position":null,"original_position":57,"commit_id":"ebb57eadbcf5868062a974408db22e001b3dd925","original_commit_id":"a2ce4e9a57c386b4771fe7b82c9822c03fa62336","user":{"login":"emord","id":1471773,"avatar_url":"https://avatars.githubusercontent.com/u/1471773?v=3","gravatar_id":"","url":"https://api.github.com/users/emord","html_url":"https://github.com/emord","followers_url":"https://api.github.com/users/emord/followers","following_url":"https://api.github.com/users/emord/following{/other_user}","gists_url":"https://api.github.com/users/emord/gists{/gist_id}","starred_url":"https://api.github.com/users/emord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emord/subscriptions","organizations_url":"https://api.github.com/users/emord/orgs","repos_url":"https://api.github.com/users/emord/repos","events_url":"https://api.github.com/users/emord/events{/privacy}","received_events_url":"https://api.github.com/users/emord/received_events","type":"User","site_admin":false},"body":"Variable names are hard","created_at":"2015-02-09T16:15:47Z","updated_at":"2015-02-09T16:15:47Z","html_url":"https://github.com/dimagi/Vellum/pull/323#discussion_r24339834","pull_request_url":"https://api.github.com/repos/dimagi/Vellum/pulls/323","_links":{"self":{"href":"https://api.github.com/repos/dimagi/Vellum/pulls/comments/24339834"},"html":{"href":"https://github.com/dimagi/Vellum/pull/323#discussion_r24339834"},"pull_request":{"href":"https://api.github.com/repos/dimagi/Vellum/pulls/323"}}},"pull_request":{"url":"https://api.github.com/repos/dimagi/Vellum/pulls/323","id":28760568,"html_url":"https://github.com/dimagi/Vellum/pull/323","diff_url":"https://github.com/dimagi/Vellum/pull/323.diff","patch_url":"https://github.com/dimagi/Vellum/pull/323.patch","issue_url":"https://api.github.com/repos/dimagi/Vellum/issues/323","number":323,"state":"open","locked":false,"title":"Only one tree","user":{"login":"emord","id":1471773,"avatar_url":"https://avatars.githubusercontent.com/u/1471773?v=3","gravatar_id":"","url":"https://api.github.com/users/emord","html_url":"https://github.com/emord","followers_url":"https://api.github.com/users/emord/followers","following_url":"https://api.github.com/users/emord/following{/other_user}","gists_url":"https://api.github.com/users/emord/gists{/gist_id}","starred_url":"https://api.github.com/users/emord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/emord/subscriptions","organizations_url":"https://api.github.com/users/emord/orgs","repos_url":"https://api.github.com/users/emord/repos","events_url":"https://api.github.com/users/emord/events{/privacy}","received_events_url":"https://api.github.com/users/emord/received_events","type":"User","site_admin":false},"body":"No longer have to keep track of both the data and control tree.\r\n\r\nThe control tree is the master and we use the flags isControlOnly and isDataOnly whenever we need to discern between those.","created_at":"2015-02-05T20:56:20Z","updated_at":"2015-02-09T16:15:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2b6084766ff9eebd6419dd2d77ad89eaf6d85262","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dimagi/Vellum/pulls/323/commits","review_comments_url":"https://api.github.com/repos/dimagi/Vellum/pulls/323/comments","review_comment_url":"https://api.github.com/repos/dimagi/Vellum/pulls/comments/{number}","comments_url":"https://api.github.com/repos/dimagi/Vellum/issues/323/comments","statuses_url":"https://api.github.com/repos/dimagi/Vellum/statuses/ebb57eadbcf5868062a974408db22e001b3dd925","head":{"label":"dimagi:only-one-tree","ref":"only-one-tree","sha":"ebb57eadbcf5868062a974408db22e001b3dd925","user":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"repo":{"id":1859793,"name":"Vellum","full_name":"dimagi/Vellum","owner":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dimagi/Vellum","description":"An XForms form designer written purely in Javascript","fork":false,"url":"https://api.github.com/repos/dimagi/Vellum","forks_url":"https://api.github.com/repos/dimagi/Vellum/forks","keys_url":"https://api.github.com/repos/dimagi/Vellum/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dimagi/Vellum/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dimagi/Vellum/teams","hooks_url":"https://api.github.com/repos/dimagi/Vellum/hooks","issue_events_url":"https://api.github.com/repos/dimagi/Vellum/issues/events{/number}","events_url":"https://api.github.com/repos/dimagi/Vellum/events","assignees_url":"https://api.github.com/repos/dimagi/Vellum/assignees{/user}","branches_url":"https://api.github.com/repos/dimagi/Vellum/branches{/branch}","tags_url":"https://api.github.com/repos/dimagi/Vellum/tags","blobs_url":"https://api.github.com/repos/dimagi/Vellum/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dimagi/Vellum/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dimagi/Vellum/git/refs{/sha}","trees_url":"https://api.github.com/repos/dimagi/Vellum/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dimagi/Vellum/statuses/{sha}","languages_url":"https://api.github.com/repos/dimagi/Vellum/languages","stargazers_url":"https://api.github.com/repos/dimagi/Vellum/stargazers","contributors_url":"https://api.github.com/repos/dimagi/Vellum/contributors","subscribers_url":"https://api.github.com/repos/dimagi/Vellum/subscribers","subscription_url":"https://api.github.com/repos/dimagi/Vellum/subscription","commits_url":"https://api.github.com/repos/dimagi/Vellum/commits{/sha}","git_commits_url":"https://api.github.com/repos/dimagi/Vellum/git/commits{/sha}","comments_url":"https://api.github.com/repos/dimagi/Vellum/comments{/number}","issue_comment_url":"https://api.github.com/repos/dimagi/Vellum/issues/comments/{number}","contents_url":"https://api.github.com/repos/dimagi/Vellum/contents/{+path}","compare_url":"https://api.github.com/repos/dimagi/Vellum/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dimagi/Vellum/merges","archive_url":"https://api.github.com/repos/dimagi/Vellum/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dimagi/Vellum/downloads","issues_url":"https://api.github.com/repos/dimagi/Vellum/issues{/number}","pulls_url":"https://api.github.com/repos/dimagi/Vellum/pulls{/number}","milestones_url":"https://api.github.com/repos/dimagi/Vellum/milestones{/number}","notifications_url":"https://api.github.com/repos/dimagi/Vellum/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dimagi/Vellum/labels{/name}","releases_url":"https://api.github.com/repos/dimagi/Vellum/releases{/id}","created_at":"2011-06-07T13:03:34Z","updated_at":"2015-02-05T17:38:10Z","pushed_at":"2015-02-09T16:14:53Z","git_url":"git://github.com/dimagi/Vellum.git","ssh_url":"git@github.com:dimagi/Vellum.git","clone_url":"https://github.com/dimagi/Vellum.git","svn_url":"https://github.com/dimagi/Vellum","homepage":"","size":13058,"stargazers_count":23,"watchers_count":23,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":2,"forks":16,"open_issues":2,"watchers":23,"default_branch":"master"}},"base":{"label":"dimagi:master","ref":"master","sha":"392101694612b0dad8c541c2331d299e30bfdf15","user":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"repo":{"id":1859793,"name":"Vellum","full_name":"dimagi/Vellum","owner":{"login":"dimagi","id":94514,"avatar_url":"https://avatars.githubusercontent.com/u/94514?v=3","gravatar_id":"","url":"https://api.github.com/users/dimagi","html_url":"https://github.com/dimagi","followers_url":"https://api.github.com/users/dimagi/followers","following_url":"https://api.github.com/users/dimagi/following{/other_user}","gists_url":"https://api.github.com/users/dimagi/gists{/gist_id}","starred_url":"https://api.github.com/users/dimagi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimagi/subscriptions","organizations_url":"https://api.github.com/users/dimagi/orgs","repos_url":"https://api.github.com/users/dimagi/repos","events_url":"https://api.github.com/users/dimagi/events{/privacy}","received_events_url":"https://api.github.com/users/dimagi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dimagi/Vellum","description":"An XForms form designer written purely in Javascript","fork":false,"url":"https://api.github.com/repos/dimagi/Vellum","forks_url":"https://api.github.com/repos/dimagi/Vellum/forks","keys_url":"https://api.github.com/repos/dimagi/Vellum/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dimagi/Vellum/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dimagi/Vellum/teams","hooks_url":"https://api.github.com/repos/dimagi/Vellum/hooks","issue_events_url":"https://api.github.com/repos/dimagi/Vellum/issues/events{/number}","events_url":"https://api.github.com/repos/dimagi/Vellum/events","assignees_url":"https://api.github.com/repos/dimagi/Vellum/assignees{/user}","branches_url":"https://api.github.com/repos/dimagi/Vellum/branches{/branch}","tags_url":"https://api.github.com/repos/dimagi/Vellum/tags","blobs_url":"https://api.github.com/repos/dimagi/Vellum/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dimagi/Vellum/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dimagi/Vellum/git/refs{/sha}","trees_url":"https://api.github.com/repos/dimagi/Vellum/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dimagi/Vellum/statuses/{sha}","languages_url":"https://api.github.com/repos/dimagi/Vellum/languages","stargazers_url":"https://api.github.com/repos/dimagi/Vellum/stargazers","contributors_url":"https://api.github.com/repos/dimagi/Vellum/contributors","subscribers_url":"https://api.github.com/repos/dimagi/Vellum/subscribers","subscription_url":"https://api.github.com/repos/dimagi/Vellum/subscription","commits_url":"https://api.github.com/repos/dimagi/Vellum/commits{/sha}","git_commits_url":"https://api.github.com/repos/dimagi/Vellum/git/commits{/sha}","comments_url":"https://api.github.com/repos/dimagi/Vellum/comments{/number}","issue_comment_url":"https://api.github.com/repos/dimagi/Vellum/issues/comments/{number}","contents_url":"https://api.github.com/repos/dimagi/Vellum/contents/{+path}","compare_url":"https://api.github.com/repos/dimagi/Vellum/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dimagi/Vellum/merges","archive_url":"https://api.github.com/repos/dimagi/Vellum/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dimagi/Vellum/downloads","issues_url":"https://api.github.com/repos/dimagi/Vellum/issues{/number}","pulls_url":"https://api.github.com/repos/dimagi/Vellum/pulls{/number}","milestones_url":"https://api.github.com/repos/dimagi/Vellum/milestones{/number}","notifications_url":"https://api.github.com/repos/dimagi/Vellum/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dimagi/Vellum/labels{/name}","releases_url":"https://api.github.com/repos/dimagi/Vellum/releases{/id}","created_at":"2011-06-07T13:03:34Z","updated_at":"2015-02-05T17:38:10Z","pushed_at":"2015-02-09T16:14:53Z","git_url":"git://github.com/dimagi/Vellum.git","ssh_url":"git@github.com:dimagi/Vellum.git","clone_url":"https://github.com/dimagi/Vellum.git","svn_url":"https://github.com/dimagi/Vellum","homepage":"","size":13058,"stargazers_count":23,"watchers_count":23,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":2,"forks":16,"open_issues":2,"watchers":23,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dimagi/Vellum/pulls/323"},"html":{"href":"https://github.com/dimagi/Vellum/pull/323"},"issue":{"href":"https://api.github.com/repos/dimagi/Vellum/issues/323"},"comments":{"href":"https://api.github.com/repos/dimagi/Vellum/issues/323/comments"},"review_comments":{"href":"https://api.github.com/repos/dimagi/Vellum/pulls/323/comments"},"review_comment":{"href":"https://api.github.com/repos/dimagi/Vellum/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/dimagi/Vellum/pulls/323/commits"},"statuses":{"href":"https://api.github.com/repos/dimagi/Vellum/statuses/ebb57eadbcf5868062a974408db22e001b3dd925"}}}}