Browse files

Allow non-sequential ordering of customizable menu

  • Loading branch information...
1 parent fd9cc33 commit e86ae4f86a6990cb788249deb03038238392d11d @Trott committed May 10, 2012
View
7 config/frontpage.ini
@@ -36,9 +36,10 @@ header_main_text="Mobile Web Framework"
; - external :: on|off :: default off :: if on, indicates that the link is
; to a document that is not part of the current site
;
-; If you have a customizable home screen (which is the default), then it is
-; strongly recommended that you specify indexes for the *.*.default arrays
-; and that you do not reuse indexes of obsolete items.
+; If you have a customizable home screen (which is the default), specify
+; non-negative integer indexes for the *.*.default arrays. Do not reuse
+; indexes of obsolete items. The default will be the order that the items
+; appear in here, not necessarily the ascending sequence of indexes.
menu.name.default[0]="About"
menu.id.default[0]="about"
menu.url.default[0]="mwf/about.php"
View
2 root/assets/js/full/customizableMenu.js
@@ -1 +1 @@
-mwf.full.customizableMenu=function(a){var b=function(){var c=null;if(mwf.standard.preferences.isSupported()){var f=mwf.standard.preferences.get(a);if(f!==null){try{c=JSON.parse(f)}catch(d){c=null}}}return c};return{render:function(k,c,h){var e,g,d;var f=document.getElementById(k);if(f===null){return}if(!c){c=[]}if(!h){h=[]}var m="";var l=b();if(l===null){l={};l.items=[];for(var j in c){if(c.hasOwnProperty(j)){l.items.push({key:+j,on:1});m+=c[j]}}if(mwf.standard.preferences.isSupported()){mwf.standard.preferences.set(a,JSON.stringify(l))}}else{if(!l.hasOwnProperty("items")){l.items=[]}g=l.items;d=[];for(e=0;e<g.length;e++){if(g[e].hasOwnProperty("key")){d.push(g[e].key);if(g[e].hasOwnProperty("on")){if((g[e].on===1)&&(c.hasOwnProperty(g[e].key))){m+=c[g[e].key]}else{if((g[e].on===0)&&(h.hasOwnProperty(g[e].key))){m+=h[g[e].key]}}}}}for(e in c){if((c.hasOwnProperty(e)&&d.indexOf(+e)==-1)){m+=c[e];l.items.push({key:+e,on:1})}}if(mwf.standard.preferences.isSupported()){mwf.standard.preferences.set(a,JSON.stringify(l))}}f.innerHTML=m},enableItem:function(j,d){var e,h,c,g;var f=b();c=d?1:0;h={key:+j,on:c};if(f===null){f={}}if(!f.hasOwnProperty("items")){f.items=[]}g=false;for(e in f.items){if(f.items[e].hasOwnProperty("key")&&f.items[e].key===+j){f.items[e].on=c;g=true;break}}if(!g){f.items.push(h)}mwf.standard.preferences.set(a,JSON.stringify(f))},setItemPosition:function(e,c){var d=b()||{};if(!d.hasOwnProperty("items")){d.items=[]}d.items[c-1]={key:+e,on:1};mwf.standard.preferences.set(a,JSON.stringify(d))},reset:function(){mwf.standard.preferences.clear(a)}}};
+mwf.full.customizableMenu=function(c){if(!Array.prototype.filter){Array.prototype.filter=function(f){if(this==null){throw new TypeError()}var k=Object(this);var e=k.length>>>0;if(typeof f!="function"){throw new TypeError()}var j=[];var h=arguments[1];for(var g=0;g<e;g++){if(g in k){var l=k[g];if(f.call(h,l,g,k)){j.push(l)}}}return j}}var d=function(){var f=null,h;if(mwf.standard.preferences.isSupported()){h=mwf.standard.preferences.get(c);if(h!==null){try{f=JSON.parse(h)}catch(g){f=null}}}return f};var b=[];var a=[];return{render:function(h){var j,f,m,g;var l=document.getElementById(h);if(l===null){return}var e="";var k=d();if(k===null){k={};k.items=[];for(j=0;j<b.length;j++){k.items.push({key:b[j].key,on:1});e+=b[j].value}if(mwf.standard.preferences.isSupported()){mwf.standard.preferences.set(c,JSON.stringify(k))}}else{f=k.items||[];m=[];for(j=0;j<f.length;j++){if(f[j].hasOwnProperty("key")){m.push(f[j].key);if(f[j].hasOwnProperty("on")){if(f[j].on===1){g=b.filter(function(n,i,o){return n.key===this.key},f[j]);if(g.length>0){e+=g[0].value}}else{if(f[j].on===0){g=a.filter(function(n,i,o){return n.key===this.key},f[j]);if(g.length>0){e+=g[0].value}}}}}}for(j=0;j<b.length;j++){if(m.indexOf(b[j].key)==-1){e+=b[j].value;k.items.push({key:b[j].key,on:1})}}if(mwf.standard.preferences.isSupported()){mwf.standard.preferences.set(c,JSON.stringify(k))}}l.innerHTML=e},addItem:function(g,f,e){g=parseInt(g,10);b.push({key:g,value:f||""});a.push({key:g,value:e||""})},enableItem:function(l,f){l=parseInt(l,10);var g,k,e,j;var h=d();e=f?1:0;k={key:l,on:e};if(h===null){h={}}if(!h.hasOwnProperty("items")){h.items=[]}j=false;for(g in h.items){if(h.items[g].hasOwnProperty("key")&&h.items[g].key===l){h.items[g].on=e;j=true;break}}if(!j){h.items.push(k)}mwf.standard.preferences.set(c,JSON.stringify(h))},setItemPosition:function(g,e){g=parseInt(g,10);var f=d()||{};if(!f.hasOwnProperty("items")){f.items=[]}f.items[e-1]={key:g,on:1};mwf.standard.preferences.set(c,JSON.stringify(f))},reset:function(){mwf.standard.preferences.clear(c)}}};
View
157 root/assets/js/full/customizableMenu.src.js
@@ -14,96 +14,132 @@
* @requires mwf.standard.preferences
*
*/
+mwf.full.customizableMenu = function (prefsKey) {
+ //polyfill Array.filter() for IE 8 and earlier
+ //from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
+ if (!Array.prototype.filter)
+ {
+ Array.prototype.filter = function(fun /*, thisp */)
+ {
+ "use strict";
+
+ if (this == null)
+ throw new TypeError();
+
+ var t = Object(this);
+ var len = t.length >>> 0;
+ if (typeof fun != "function")
+ throw new TypeError();
+
+ var res = [];
+ var thisp = arguments[1];
+ for (var i = 0; i < len; i++)
+ {
+ if (i in t)
+ {
+ var val = t[i]; // in case fun mutates this
+ if (fun.call(thisp, val, i, t))
+ res.push(val);
+ }
+ }
+
+ return res;
+ };
+ }
+ //end polyfill for IE 8 and earlier
+
+ var getPrefsLists = function () {
+ var keys = null,
+ prefsValue;
-mwf.full.customizableMenu=function(prefsKey){
-
- var getPrefsLists = function() {
- var keys = null;
-
if (mwf.standard.preferences.isSupported()) {
- var prefsValue = mwf.standard.preferences.get(prefsKey);
- if (prefsValue!==null)
+ prefsValue = mwf.standard.preferences.get(prefsKey);
+ if (prefsValue !== null) {
try {
- keys = JSON.parse(prefsValue);
+ keys = JSON.parse(prefsValue);
} catch (e) {
- // String from user's preferences is not a valid JSON object.
+ // String from user's preferences is not a valid JSON object.
// Revert to default.
keys = null;
}
+ }
}
return keys;
}
-
+
+ var menuItems = [];
+ var disabledMenuItems = [];
+
return {
/**
* Renders the items from object or array menuItems (whose keys are
* specified in the preferences) into the DOM object with id targetId.
*
+ * @todo: menuItems should be an array of menuItem objects of the form {'key':key, 'value':value} so that order can be preserved
+ *
* @param targetId string
- * @param menuItems object|array
- * @param disabledMenuItems object|array
*
* @return undefined
*/
- render: function(targetId, menuItems, disabledItems){
- var i, items, processed;
+ render: function(targetId){
+ var i, items, processed, filtered;
var target = document.getElementById(targetId);
if (target === null) {
return;
}
-
- if (! menuItems) {
- menuItems = [];
- }
-
- if (! disabledItems) {
- disabledItems = [];
- }
var result = '';
var keys = getPrefsLists();
-
+
if (keys===null) {
keys={};
keys.items = [];
- for (var key in menuItems)
- if (menuItems.hasOwnProperty(key)) {
- keys.items.push({
- key:+key,
- on:1
- });
- result += menuItems[key];
- }
- if (mwf.standard.preferences.isSupported())
- mwf.standard.preferences.set(prefsKey,JSON.stringify(keys));
+ for (i=0; i<menuItems.length; i++) {
+ keys.items.push({
+ key:menuItems[i].key,
+ on:1
+ });
+ result += menuItems[i].value;
+ }
+ if (mwf.standard.preferences.isSupported()) {
+ mwf.standard.preferences.set(prefsKey, JSON.stringify(keys));
+ }
} else {
// Render items in the correct order
- if (! keys.hasOwnProperty('items')) {
- keys.items = [];
- }
- items = keys.items;
+ items = keys.items || [];
processed=[];
for (i=0; i<items.length; i++) {
if (items[i].hasOwnProperty('key')) {
processed.push(items[i].key);
+// @todo: refactor for DRY
if (items[i].hasOwnProperty('on')) {
- if ((items[i].on === 1) && (menuItems.hasOwnProperty(items[i].key))) {
- result += menuItems[items[i].key];
- } else if ((items[i].on === 0) && (disabledItems.hasOwnProperty(items[i].key))) {
- result += disabledItems[items[i].key];
+ if (items[i].on === 1) {
+ filtered = menuItems.filter(function(element, index, array){
+ return element.key === this.key;
+ }, items[i]);
+ if (filtered.length > 0) {
+ result += filtered[0].value;
+ }
+ } else if (items[i].on === 0) {
+ filtered = disabledMenuItems.filter(function(element, index, array){
+ return element.key === this.key;
+ }, items[i]);
+ if (filtered.length > 0) {
+ result += filtered[0].value;
+ }
}
}
}
}
- for (i in menuItems) {
- if ((menuItems.hasOwnProperty(i) && processed.indexOf(+i)==-1)) {
- result += menuItems[i];
+ for (i=0; i<menuItems.length; i++) {
+ if (processed.indexOf(menuItems[i].key)==-1) {
+ result += menuItems[i].value;
keys.items.push({
- key:+i,
+ key:menuItems[i].key,
on:1
});
}
@@ -116,6 +152,25 @@ mwf.full.customizableMenu=function(prefsKey){
}
target.innerHTML = result;
},
+
+ /**
+ * Add an item to the end of the list of menu possibilities.
+ *
+ * @param key string
+ * @param enabledMarkup string
+ * @param disabledMarkup string
+ */
+ addItem: function(key, enabledMarkup, disabledMarkup) {
+ key = parseInt(key, 10);
+ menuItems.push({
+ key: key,
+ value: enabledMarkup || ''
+ });
+ disabledMenuItems.push({
+ key: key,
+ value: disabledMarkup || ''
+ });
+ },
/**
* Enables or disables an item in the menu.
@@ -124,13 +179,14 @@ mwf.full.customizableMenu=function(prefsKey){
* @param enable boolean If true, enable item. Otherwise, disable.
*/
enableItem: function(itemId, enable) {
+ itemId = parseInt(itemId, 10);
var i, add, on, found;
var keys = getPrefsLists();
on = enable ? 1 : 0;
add = {
- key:+itemId,
+ key:itemId,
on:on
};
@@ -144,7 +200,7 @@ mwf.full.customizableMenu=function(prefsKey){
found = false;
for (i in keys.items) {
- if (keys.items[i].hasOwnProperty('key') && keys.items[i].key === +itemId) {
+ if (keys.items[i].hasOwnProperty('key') && keys.items[i].key === itemId) {
keys.items[i].on = on;
found = true;
break;
@@ -162,16 +218,17 @@ mwf.full.customizableMenu=function(prefsKey){
* overwriting anything that was already there..
*
* @param itemId integer
- * @param positioin integer
+ * @param position integer
*
*/
setItemPosition: function(itemId, position) {
+ itemId = parseInt(itemId, 10);
var keys = getPrefsLists() || {};
if (! keys.hasOwnProperty('items')) {
keys.items = [];
}
keys.items[position - 1] = {
- key:+itemId,
+ key:itemId,
on:1
};
mwf.standard.preferences.set(prefsKey,JSON.stringify(keys));
@@ -185,4 +242,4 @@ mwf.full.customizableMenu=function(prefsKey){
mwf.standard.preferences.clear(prefsKey);
}
}
-};
+};
View
11 root/assets/lib/decorator/site/menu.class.php
@@ -132,12 +132,11 @@ public function render($raw = false) {
}
if ($this->_home_screen && Classification::is_full() && Config::get('frontpage', 'customizable_home_screen')) {
- $js = 'mwf.full.customizableMenu("home_screen_layout").render("main_menu_list",' .
- json_encode(
- array_map(function($obj, $raw) {
- return $obj->render($raw);
- }, $this->_list, array_fill(0, count($this->_list), $raw))) . ');';
-
+ $js = 'mwf.cm = mwf.full.customizableMenu("home_screen_layout");';
+ foreach ($this->_list as $key=>$value) {
+ $js .= 'mwf.cm.addItem(' . json_encode($key) . ',' . json_encode($value->render($raw)) . ');';
+ }
+ $js .= 'mwf.cm.render("main_menu_list");';
$this->add_inner(HTML_Decorator::tag('ol')->set_param('id', 'main_menu_list'));
$this->add_inner(HTML_Decorator::tag('script', $js));
} else {
View
145 root/assets/test/js_unit/full/customizableMenu.js
@@ -27,19 +27,15 @@ module("full/customizableMenu.js", {
}
});
-test("mwf.full.customizableMenu render() no settings, object passed, returns everything", function()
+test("mwf.full.customizableMenu render() no settings, string keys, returns everything", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout',oldValue);
mwf.standard.preferences.clear('home_screen_layout');
var cm = mwf.full.customizableMenu('home_screen_layout');
- cm.render(
- "fake_main_menu",
- {
- "a":"<li><a href=\"foo\">Foo<\/a><\/li>",
- "b":"<li><a href=\"foo\">Foo<\/a><\/li>"
- }
- );
+ cm.addItem('a', '<li><a href="foo">Foo</a></li>', '');
+ cm.addItem('b', '<li><a href="foo">Foo</a></li>');
+ cm.render("fake_main_menu");
equal(document.getElementById('fake_main_menu').innerHTML,
'<li><a href="foo">Foo</a></li><li><a href="foo">Foo</a></li>',
@@ -51,17 +47,16 @@ test("mwf.full.customizableMenu render() no settings, object passed, returns eve
});
-test("mwf.full.customizableMenu render() no settings, array passed, returns everything", function()
+test("mwf.full.customizableMenu render() no settings, numeric keys, returns everything", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout',oldValue);
mwf.standard.preferences.clear('home_screen_layout');
var cm = mwf.full.customizableMenu('home_screen_layout');
-
- cm.render(
- "fake_main_menu",
- ["<li><a href=\"foo\">Foo<\/a><\/li>","<li><a href=\"foo\">Foo<\/a><\/li>"]
- );
+
+ cm.addItem(0, '<li><a href="foo">Foo</a></li>', '');
+ cm.addItem(1, '<li><a href="foo">Foo</a></li>', '');
+ cm.render("fake_main_menu");
equal(document.getElementById('fake_main_menu').innerHTML,
'<li><a href="foo">Foo</a></li><li><a href="foo">Foo</a></li>',
@@ -72,7 +67,7 @@ test("mwf.full.customizableMenu render() no settings, array passed, returns ever
}
});
-test("mwf.full.customizableMenu render() has settings, object passed", function()
+test("mwf.full.customizableMenu render() has settings, one item enabled two disabled", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout');
mwf.standard.preferences.set('home_screen_layout',JSON.stringify({
@@ -89,15 +84,10 @@ test("mwf.full.customizableMenu render() has settings, object passed", function(
}));
var cm = mwf.full.customizableMenu('home_screen_layout');
-
- cm.render(
- "fake_main_menu",
- {
- "1":"<li><a href=\"foo\">Foo<\/a><\/li>",
- "2":"<li><a href=\"bar\">Bar<\/a><\/li>",
- "3":"<li><a href=\"baz\">Baz<\/a><\/li>"
- }
- );
+ cm.addItem(1, '<li><a href="foo">Foo</a></li>', '');
+ cm.addItem(2, '<li><a href="bar">Bar</a></li>', '');
+ cm.addItem(3, '<li><a href="baz">Baz</a></li>', '');
+ cm.render("fake_main_menu");
equal(document.getElementById('fake_main_menu').innerHTML,
'<li><a href="bar">Bar</a></li>',
@@ -109,7 +99,7 @@ test("mwf.full.customizableMenu render() has settings, object passed", function(
});
-test("mwf.full.customizableMenu render() has settings, array passed", function()
+test("mwf.full.customizableMenu render() has settings, two items enabled, one disabled", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout');
mwf.standard.preferences.set('home_screen_layout',JSON.stringify({
@@ -126,11 +116,11 @@ test("mwf.full.customizableMenu render() has settings, array passed", function()
}));
var cm = mwf.full.customizableMenu('home_screen_layout');
+ cm.addItem(0, '<li><a href="foo">Foo</a></li>', '');
+ cm.addItem(1, '<li><a href="bar">Bar</a></li>', '');
+ cm.addItem(2, '<li><a href="whoa">Whoa</a></li>', '');
- cm.render(
- "fake_main_menu",
- ["<li><a href=\"foo\">Foo<\/a><\/li>","<li><a href=\"bar\">Bar<\/a><\/li>","<li><a href=\"whoa\">Whoa<\/a><\/li>"]
- );
+ cm.render("fake_main_menu");
equal(document.getElementById('fake_main_menu').innerHTML,
'<li><a href="foo">Foo</a></li><li><a href="whoa">Whoa</a></li>',
@@ -150,12 +140,12 @@ test("mwf.full.customizableMenu render() mangled settings, graceful handling", f
mwf.standard.preferences.set('home_screen_layout','["a]');
var cm = mwf.full.customizableMenu('home_screen_layout');
+ cm.addItem("a");
+ cm.addItem("b");
+ cm.addItem("c");
try {
- cm.render(
- "fake_main_menu",
- ["a","b","c"]
- );
+ cm.render("fake_main_menu");
} catch (e) {
result = false;
} finally {
@@ -168,7 +158,7 @@ test("mwf.full.customizableMenu render() mangled settings, graceful handling", f
});
-test("mwf.full.customizableMenu render() has settings, array passed", function()
+test("mwf.full.customizableMenu render() has settings, no disabled items sent", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout');
mwf.standard.preferences.set('home_screen_layout',JSON.stringify({
@@ -185,15 +175,11 @@ test("mwf.full.customizableMenu render() has settings, array passed", function()
}));
var cm = mwf.full.customizableMenu('home_screen_layout');
+ cm.addItem(0, '<li><a href="foo">Foo</a></li>');
+ cm.addItem(2, '<li>a href="whoa">Whoa</a></li>');
+ cm.addItem(3, '<li><a href="baz">Baz</a></li>');
- cm.render(
- "fake_main_menu",
- {
- "0":"<li><a href=\"foo\">Foo<\/a><\/li>",
- "2":"<li><a href=\"whoa\">Whoa<\/a><\/li>",
- "3":"<li><a href=\"baz\">Baz<\/a><\/li>"
- }
- );
+ cm.render("fake_main_menu");
equal(document.getElementById('fake_main_menu').innerHTML,
'<li><a href="foo">Foo</a></li><li><a href="baz">Baz</a></li>',
@@ -221,20 +207,11 @@ test("mwf.full.customizableMenu render() disabled items sent", function()
}));
var cm = mwf.full.customizableMenu('home_screen_layout');
+ cm.addItem('0', '<li><a href="foo">Foo</a></li>', 'foo disabled');
+ cm.addItem('1', '<li><a href="whoa">Whoa</a></li>', 'whoa totally disabled');
+ cm.addItem('2', '<li><a href="baz">Baz</a></li>', 'baz has left the building');
- cm.render(
- "fake_main_menu",
- {
- "0":"<li><a href=\"foo\">Foo<\/a><\/li>",
- "1":"<li><a href=\"whoa\">Whoa<\/a><\/li>",
- "2":"<li><a href=\"baz\">Baz<\/a><\/li>"
- },
- {
- "0":"foo disabled",
- "1":"whoa totally disabled",
- "2":"baz has left the building"
- }
- );
+ cm.render("fake_main_menu");
equal(document.getElementById('fake_main_menu').innerHTML,
'<li><a href="foo">Foo</a></li><li><a href="whoa">Whoa</a></li>baz has left the building',
@@ -689,23 +666,23 @@ test("mwf.full.customizableMenu enableItem() disable disabled item", function()
});
-test("mwf.full.customizableMenu render() should not have side effects on passed array", function()
+test("mwf.full.customizableMenu render() should not have side effects on passed values", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout');
mwf.standard.preferences.set('home_screen_layout',JSON.stringify({
items:[{
key:0,
on:0
}]
- }));
+ }));
var cm = mwf.full.customizableMenu('home_screen_layout');
var menuItems = ["foo", "bar", "baz"];
- cm.render(
- "fake_main_menu",
- menuItems
- );
+ cm.addItem(0, menuItems[0]);
+ cm.addItem(1, menuItems[1]);
+ cm.addItem(2, menuItems[2]);
+ cm.render("fake_main_menu");
deepEqual(menuItems, ["foo","bar","baz"], "render() should not have side effects on menuItems array");
@@ -851,11 +828,10 @@ test("mwf.full.customizableMenu render() no settings, object passed, sets settin
mwf.standard.preferences.clear('home_screen_layout');
var cm = mwf.full.customizableMenu('home_screen_layout');
+ cm.addItem(0,'foo');
+ cm.addItem(1,'bar');
- cm.render(
- "fake_main_menu",
- ["foo","bar"]
- );
+ cm.render("fake_main_menu");
var prefResults = JSON.parse(mwf.standard.preferences.get('home_screen_layout'));
@@ -890,11 +866,9 @@ test("mwf.full.customizableMenu render() some settings, missing items set", func
}));
var cm = mwf.full.customizableMenu('home_screen_layout');
-
- cm.render(
- "fake_main_menu",
- ["foo","bar"]
- );
+ cm.addItem(0, 'foo');
+ cm.addItem(1, 'bar');
+ cm.render("fake_main_menu");
var prefResults = JSON.parse(mwf.standard.preferences.get('home_screen_layout'));
@@ -918,34 +892,30 @@ test("mwf.full.customizableMenu render() some settings, missing items set", func
}
});
-test("mwf.full.customizableMenu render() no settings, object passed, sets settings casting strings to ints", function()
+test("mwf.full.customizableMenu render() no settings, sets settings, keys are numbers", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout',oldValue);
mwf.standard.preferences.clear('home_screen_layout');
var cm = mwf.full.customizableMenu('home_screen_layout');
+ cm.addItem(0,"foo");
+ cm.addItem(1,"bar");
- cm.render(
- "fake_main_menu",
- {
- "0":"foo",
- "1":"bar"
- }
- );
+ cm.render("fake_main_menu");
var prefResults = JSON.parse(mwf.standard.preferences.get('home_screen_layout'));
deepEqual(prefResults.items[0],{
key:0,
on:1
},
- 'render() should set prefs if they are not already set casting string to int');
+ 'render() should set first pref if not already set');
deepEqual(prefResults.items[1],{
key:1,
on:1
},
- 'render() should set prefs if they are not already set casting string to int');
+ 'render() should set second pref if not already set');
strictEqual(prefResults.items.length,2,
'render() should result in one pref setting per menu item');
@@ -955,7 +925,7 @@ test("mwf.full.customizableMenu render() no settings, object passed, sets settin
}
});
-test("mwf.full.customizableMenu render() some settings, missing items set casting string to int", function()
+test("mwf.full.customizableMenu render() some settings, missing items set, keys are strings", function()
{
var oldValue = mwf.standard.preferences.get('home_screen_layout',oldValue);
mwf.standard.preferences.set('home_screen_layout',JSON.stringify({
@@ -966,17 +936,12 @@ test("mwf.full.customizableMenu render() some settings, missing items set castin
}));
var cm = mwf.full.customizableMenu('home_screen_layout');
-
- cm.render(
- "fake_main_menu",
- {
- "0":"foo",
- "1":"bar"
- }
- );
+ cm.addItem("0","foo");
+ cm.addItem("1","bar");
+ cm.render("fake_main_menu");
var prefResults = JSON.parse(mwf.standard.preferences.get('home_screen_layout'));
-
+
deepEqual(prefResults.items[0],{
key:1,
on:1
View
15 root/customize_home_screen.php
@@ -42,6 +42,7 @@
->render();
if (Classification::is_full()) {
+ $js = "var cm = mwf.full.customizableMenu('home_screen_layout');";
$apps = Config::get('frontpage', 'menu.name.default');
$ids = Config::get('frontpage', 'menu.id.default');
@@ -51,16 +52,15 @@
$this_id = htmlspecialchars($ids[$key]);
$encoded_key = json_encode($key);
- $apps_rendered[$key] = '<label data-id="' . $encoded_key . '"><input id="' . $this_id . '" type="checkbox" checked>&nbsp;' .
+ $enabled_markup = '<label data-id="' . $encoded_key . '"><input id="' . $this_id . '" type="checkbox" checked>&nbsp;' .
htmlspecialchars($apps[$key]) .
'<span class="draggable-handle"></span></label>';
- $disabled_apps_rendered[$key] = '<label data-id="' . $encoded_key . '"><input id="' . $this_id . '" type="checkbox">&nbsp;' .
- htmlspecialchars($apps[$key]) .
+ $disabled_markup= '<label data-id="' . $encoded_key . '"><input id="' . $this_id . '" type="checkbox">&nbsp;' .
+ htmlspecialchars($apps[$key]) .
'<span class="draggable-handle"></span></label>';
- }
+ $js .= 'cm.addItem(' . json_encode($key) . ',' . json_encode($enabled_markup) . ',' . json_encode($disabled_markup) . ');';
- //@todo Move JS to external file, include with JS handler (perhaps part of customizableMenu, perhaps not)
- $js = 'var apps=' . json_encode($apps_rendered) . ';var disabledApps=' . json_encode($disabled_apps_rendered) . ';';
+ }
echo Site_Decorator::form('Customize Home Screen')
->set_padded()
@@ -81,13 +81,12 @@
echo HTML_Decorator::tag('script')
->add_inner($js .
- "var cm = mwf.full.customizableMenu('home_screen_layout');" .
"function saveMenu()" .
"{cm.reset();\$('#app_order').children().each(" .
" function(index,element) {cm.setItemPosition(element.getAttribute('data-id'),index+1);" .
"cm.enableItem(element.getAttribute('data-id'),this.querySelector('[type=checkbox]').checked)})}" .
"function renderMenu()" .
- "{cm.render('app_order',apps, disabledApps)}" .
+ "{cm.render('app_order')}" .
"renderMenu();" .
'$(function() { $( "#app_order" ).sortable({handle:".draggable-handle"}); $( "#app_order" ).disableSelection();});')
->render();
View
3 root/index.php
@@ -90,7 +90,6 @@
$menu->set_home_screen();
foreach ($menu_names as $key => $menu_name) {
-
$list_item_attributes = array();
if (isset($menu_classes[$key])) {
$list_item_attributes['class'] = $menu_classes[$key];
@@ -105,7 +104,7 @@
$link_attributes['rel'] = 'external';
}
- $menu->add_item($menu_name, $menu_urls[$key], $list_item_attributes, $link_attributes);
+ $menu->add_item($menu_name, $menu_urls[$key], $list_item_attributes, $link_attributes, $key);
}
echo $menu->render();
View
3 test/php_unit/root/assets/lib/decorator/site/Menu_Site_DecoratorTest.php
@@ -77,7 +77,8 @@ public function render_homeScreenAndFull_jsObject() {
$this->object->set_home_screen();
$this->object->add_item('Foo', 'http://example.com/', array(), array(), 'foo_index');
$this->object->add_item('Bar', 'http://musicroutes.com/', array(), array(), 'bar_index');
- $this->assertRegExp('/\bmwf\.full\.customizableMenu\(\"home_screen_layout"\)\.render\(/', $this->object->render());
+ $this->assertRegExp('/\bmwf\.full\.customizableMenu\(\"home_screen_layout"\)/', $this->object->render());
+ $this->assertRegExp('/\bmwf\.cm\.render\(\"main_menu_list\"\)\;/', $this->object->render());
}
}

0 comments on commit e86ae4f

Please sign in to comment.