| [5] | 1 | /* |
|---|
| 2 | ************************************************************************************************************************************* |
|---|
| 3 | 30 december : |
|---|
| 4 | added global js var rememberMe (array) |
|---|
| 5 | added functions updateRememberMe() and checkRememberMe() |
|---|
| 6 | added onclick events to categories/entries boxes |
|---|
| 7 | 08 january : |
|---|
| 8 | created init() function and added resetRelatedEntries() function in order to be able to re-call script on 'reset' click |
|---|
| 9 | ************************************************************************************************************************************* |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | var field1 = document.editForm.cboRelatedEntriesCats; |
|---|
| 13 | var field2 = document.editForm.cboRelatedEntries; |
|---|
| 14 | |
|---|
| 15 | var preselected = 0; |
|---|
| 16 | |
|---|
| 17 | function init() { |
|---|
| 18 | |
|---|
| 19 | field1.options.length = 0; |
|---|
| 20 | field2.options.length = 0; |
|---|
| 21 | |
|---|
| 22 | // populate the categories on initial page load |
|---|
| 23 | for (var i=0; i<categoryArray.length; i++) { |
|---|
| 24 | field1.options[field1.options.length] = new Option(categoryArray[i], categoryArray[i]); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | // pre-select categories for this post if it already has related entries stored |
|---|
| 28 | for (var i=0; i<rememberCats.length; i++) { |
|---|
| 29 | for (var j=0; j<field1.options.length; j++) { |
|---|
| 30 | if (rememberCats[i] == field1.options[j].value) { |
|---|
| 31 | field1.options[j].selected = true; |
|---|
| 32 | preselected++; |
|---|
| 33 | break; |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | if (preselected > 0) { |
|---|
| 39 | doPopulateEntries(0); |
|---|
| 40 | checkRememberEntries(); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | init(); |
|---|
| 45 | |
|---|
| 46 | // -------------------------------------------------------------------------------------------- // |
|---|
| 47 | |
|---|
| 48 | function updateRememberEntries() { |
|---|
| 49 | rememberEntries.length = 0; |
|---|
| 50 | for (var i=0; i<field2.options.length; i++) { |
|---|
| 51 | if (field2.options[i].selected == true) { |
|---|
| 52 | rememberEntries[rememberEntries.length] = field2.options[i].value; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | function checkRememberEntries() { |
|---|
| 58 | // loop over the rememberEntries array |
|---|
| 59 | for (var i=0; i<rememberEntries.length; i++) { |
|---|
| 60 | // loop over the options in the Entries box |
|---|
| 61 | for (var j=0; j<field2.options.length; j++) { |
|---|
| 62 | if (rememberEntries[i] == field2.options[j].value) { |
|---|
| 63 | field2.options[j].selected = true; |
|---|
| 64 | break; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | function doPopulateEntries(sortBy) { |
|---|
| 71 | // populates the entries with the applicable options based on the selected categories |
|---|
| 72 | |
|---|
| 73 | if (field1.selectedIndex == -1) { |
|---|
| 74 | field2.options.length = 0; // added on 08 january 2005 to remove all entry options if no categories are selected - cjg |
|---|
| 75 | return false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if (sortBy == 0) { |
|---|
| 79 | if (document.getElementById('sortLinkDate').style.color == "rgb(128, 128, 128)") { |
|---|
| 80 | sortBy = "sortByTitle"; |
|---|
| 81 | } else { |
|---|
| 82 | sortBy = "sortByDate"; |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if (sortBy == "sortByTitle") { |
|---|
| 87 | document.getElementById('sortLinkDate').style.color = "rgb(128, 128, 128)"; |
|---|
| 88 | document.getElementById('sortLinkTitle').style.color = "rgb(0, 0, 255)"; |
|---|
| 89 | } else { |
|---|
| 90 | document.getElementById('sortLinkTitle').style.color = "rgb(128, 128, 128)"; |
|---|
| 91 | document.getElementById('sortLinkDate').style.color = "rgb(0, 0, 255)"; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | var entryArray = new Array(); // to populate the options |
|---|
| 95 | field2.options.length = null; // clear out the options |
|---|
| 96 | |
|---|
| 97 | for (var i=0; i<field1.options.length; i++) { |
|---|
| 98 | if (field1.options[i].selected) { |
|---|
| 99 | for (var j=0; j<categoryArray[field1.options[i].text].length; j++) { |
|---|
| 100 | apos = entryArray.length; |
|---|
| 101 | entryArray[apos] = new Array(categoryArray[field1.options[i].text][j].ID, categoryArray[field1.options[i].text][j].title + ' (' + categoryArray[field1.options[i].text][j].posted + ')', categoryArray[field1.options[i].text][j].posted); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // dedupe |
|---|
| 107 | entryArrayDeDuped = new Array(); |
|---|
| 108 | |
|---|
| 109 | // seed the deduped array with the first element from the entryArray |
|---|
| 110 | entryArrayDeDuped[0] = new Array(); |
|---|
| 111 | entryArrayDeDuped[0][0] = entryArray[0][0]; // id |
|---|
| 112 | entryArrayDeDuped[0][1] = entryArray[0][1]; // title |
|---|
| 113 | entryArrayDeDuped[0][2] = entryArray[0][2]; // date |
|---|
| 114 | |
|---|
| 115 | for (var i=1; i<entryArray.length; i++) { |
|---|
| 116 | |
|---|
| 117 | found = 0; |
|---|
| 118 | for (var j=0; j<entryArrayDeDuped.length; j++) { |
|---|
| 119 | if (entryArray[i][0] == entryArrayDeDuped[j][0]) { |
|---|
| 120 | found = 1; |
|---|
| 121 | break; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if (found == 0) { |
|---|
| 126 | thispos = entryArrayDeDuped.length; |
|---|
| 127 | entryArrayDeDuped[thispos] = new Array(); |
|---|
| 128 | entryArrayDeDuped[thispos][0] = entryArray[i][0]; |
|---|
| 129 | entryArrayDeDuped[thispos][1] = entryArray[i][1]; |
|---|
| 130 | |
|---|
| 131 | entryArrayDeDuped[thispos][2] = entryArray[i][2]; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | // sort the deduped array (w00t!) |
|---|
| 136 | function sortByDate(a,b) { |
|---|
| 137 | if (a[2]>b[2]) { |
|---|
| 138 | return 1; |
|---|
| 139 | } else if (a[2]<b[2]) { |
|---|
| 140 | return -1; |
|---|
| 141 | } else { |
|---|
| 142 | return 0; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | function sortByTitle(a,b) { |
|---|
| 147 | if (a[1]>b[1]) { |
|---|
| 148 | return 1; |
|---|
| 149 | } else if (a[1]<b[1]) { |
|---|
| 150 | return -1; |
|---|
| 151 | } else { |
|---|
| 152 | return 0; |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | if (sortBy == "sortByTitle") { |
|---|
| 156 | entryArrayDeDuped = entryArrayDeDuped.sort(sortByTitle); |
|---|
| 157 | } else { |
|---|
| 158 | entryArrayDeDuped = entryArrayDeDuped.sort(sortByDate); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | for (var i=0; i<entryArrayDeDuped.length; i++) { |
|---|
| 162 | field2.options[field2.options.length] = new Option(entryArrayDeDuped[i][1], entryArrayDeDuped[i][0]) |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | checkRememberEntries(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | function resetRelatedEntries() { |
|---|
| 169 | init(); |
|---|
| 170 | |
|---|
| 171 | rememberEntries.length = 0; // added 12 january to clear checked entries and allow a true reset - cjg |
|---|
| 172 | field2.selectedIndex = -1; // added 12 january to clear checked entries and allow a true reset - cjg |
|---|
| 173 | |
|---|
| 174 | // loop over the original entries array |
|---|
| 175 | for (var i=0; i<originalEntries.length; i++) { |
|---|
| 176 | // loop over the options in the Entries box |
|---|
| 177 | for (var j=0; j<field2.options.length; j++) { |
|---|
| 178 | if (originalEntries[i] == field2.options[j].value) { |
|---|
| 179 | field2.options[j].selected = true; |
|---|
| 180 | // break; |
|---|
| 181 | } else { |
|---|
| 182 | field2.options[j].selected = false; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | updateRememberEntries(); // added 12 january to clear checked entries and allow a true reset - cjg |
|---|
| 187 | } |
|---|