diff --git a/www/common/metadata-manager.js b/www/common/metadata-manager.js index 928d7e0db..31f38252d 100644 --- a/www/common/metadata-manager.js +++ b/www/common/metadata-manager.js @@ -16,6 +16,7 @@ define([], function () { var dirty = true; var changeHandlers = []; var lazyChangeHandlers = []; + var titleChangeHandlers = []; var rememberedTitle; @@ -50,13 +51,14 @@ define([], function () { var lazyUserStr = JSON.stringify(metadataLazyObj.users[meta.user.netfluxId]); dirty = false; if (lazy || lazyUserStr !== JSON.stringify(meta.user)) { - metadataLazyObj.users = mdo; + metadataLazyObj = JSON.parse(JSON.stringify(metadataObj)); lazyChangeHandlers.forEach(function (f) { f(); }); } if (metadataObj.title !== rememberedTitle) { console.log("Title update\n" + metadataObj.title + '\n'); rememberedTitle = metadataObj.title; + titleChangeHandlers.forEach(function (f) { f(metadataObj.title); }); } changeHandlers.forEach(function (f) { f(); }); @@ -113,6 +115,7 @@ define([], function () { getMetadataLazy: function () { return metadataLazyObj; }, + onTitleChange: function (f) { titleChangeHandlers.push(f); }, onChange: function (f) { changeHandlers.push(f); }, onChangeLazy: function (f) { lazyChangeHandlers.push(f); }, isConnected : function () { diff --git a/www/common/sframe-chainpad-netflux-inner.js b/www/common/sframe-chainpad-netflux-inner.js index e5b921859..04659fe17 100644 --- a/www/common/sframe-chainpad-netflux-inner.js +++ b/www/common/sframe-chainpad-netflux-inner.js @@ -15,9 +15,8 @@ * along with this program. If not, see . */ define([ - '/common/metadata-manager.js', '/bower_components/chainpad/chainpad.dist.js' -], function (MetadataMgr) { +], function () { var ChainPad = window.ChainPad; var module = { exports: {} }; @@ -39,14 +38,13 @@ define([ var logLevel = typeof(config.logLevel) !== 'undefined'? config.logLevel : 1; var readOnly = config.readOnly || false; var sframeChan = config.sframeChan; + var metadataMgr = config.metadataMgr; config = undefined; var chainpad; var myID; var isReady = false; - var metadataMgr = MetadataMgr.create(sframeChan); - sframeChan.on('EV_RT_DISCONNECT', function () { isReady = false; onConnectionChange({ state: false }); diff --git a/www/common/sframe-common-title.js b/www/common/sframe-common-title.js index 9d611f9a2..545e80d51 100644 --- a/www/common/sframe-common-title.js +++ b/www/common/sframe-common-title.js @@ -50,19 +50,9 @@ define(['jquery'], function ($) { cb = cb || $.noop; if (newTitle === exp.title) { return; } // Change the title now, and set it back to the old value if there is an error - var oldTitle = exp.title; - Common.setPadTitleInDrive(newTitle, function (err, data) { - if (err) { - console.log("Couldn't set pad title"); - console.error(err); - updateLocalTitle(oldTitle); - return void cb(err); - } - metadataMgr.updateTitle(newTitle); - //onLocal(); - cb(null, data); - if (!$title) { return; } - }); + //var oldTitle = exp.title; + Common.updateTitle(newTitle, cb) + //cb(null, newTitle); }; // TODO not needed? diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index 2b9063e06..3459ea905 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -2,9 +2,10 @@ define([ '/bower_components/nthen/index.js', '/common/sframe-chainpad-netflux-inner.js', '/common/sframe-channel.js', - '/common/sframe-common-title.js' + '/common/sframe-common-title.js', + '/common/metadata-manager.js', -], function (nThen, CpNfInner, SFrameChannel, Title) { +], function (nThen, CpNfInner, SFrameChannel, Title, MetadataMgr) { // Chainpad Netflux Inner var funcs = {}; @@ -13,6 +14,7 @@ define([ funcs.startRealtime = function (options) { if (ctx.cpNfInner) { return ctx.cpNfInner; } options.sframeChan = ctx.sframeChan; + options.metadataMgr = ctx.metadataMgr; ctx.cpNfInner = CpNfInner.start(options); ctx.cpNfInner.metadataMgr.onChangeLazy(options.onLocal); return ctx.cpNfInner; @@ -23,10 +25,10 @@ define([ return ctx.cpNfInner.metadataMgr.getPrivateData().accountName; }; - funcs.setPadTitleInDrive = function (title, cb) { - ctx.sframeChan.query('Q_SET_PAD_TITLE_IN_DRIVE', title, function (err) { - if (cb) { cb(err, title); } - }); + var titleUpdated; + funcs.updateTitle = function (title, cb) { + ctx.metadataMgr.updateTitle(title); + titleUpdated = cb; }; // Title module @@ -49,6 +51,13 @@ define([ SFrameChannel.create(window.top, waitFor(function (sfc) { ctx.sframeChan = sfc; })); // CpNfInner.start() should be here.... }).nThen(function () { + ctx.metadataMgr = MetadataMgr.create(ctx.sframeChan); + ctx.metadataMgr.onTitleChange(function (title) { + ctx.sframeChan.query('Q_SET_PAD_TITLE_IN_DRIVE', title, function (err) { + if (err) { return; } + if (titleUpdated) { titleUpdated(undefined, title); } + }); + }); cb(funcs); }); } }; diff --git a/www/pad2/main.js b/www/pad2/main.js index 2f172261f..bf8fa8d97 100644 --- a/www/pad2/main.js +++ b/www/pad2/main.js @@ -725,6 +725,7 @@ define([ realtimeOptions.onError = onConnectError; var onLocal = realtimeOptions.onLocal = function () { + console.log('onlocal'); if (initializing) { return; } if (isHistoryMode) { return; } if (readOnly) { return; }