client/js/space-editor.js
changeset 293 fba23fde14ba
parent 192 47aa37d3e750
equal deleted inserted replaced
292:f67047a16084 293:fba23fde14ba
     1 $(function() {
     1 $(function() {
     2     
     2 
     3     var originalData = {
     3     var originalData = {
     4         show_editor: false,
     4         show_editor: false,
     5         container: "preview"
     5         container: "preview"
     6     };
     6     };
     7    
     7 
     8     var userData = {
     8     var userData = {
     9         bins: [
     9         bins: [
    10             {
    10             {
    11                 type: "ResourceList",
    11                 type: "ResourceList",
    12                 title: "Resource List",
    12                 title: "Resource List",
    25                 type: "Wikipedia",
    25                 type: "Wikipedia",
    26                 lang: "en"
    26                 lang: "en"
    27             }
    27             }
    28         ]
    28         ]
    29     };
    29     };
    30     
    30 
    31     function getNormalizedHtml(jqsel) {
    31     function getNormalizedHtml(jqsel) {
    32         return jqsel.html().replace(/[\r\n]/gm,' ').replace(/\s{2,}/g,' ').replace(/(^\s+|\s+$)/g,'');
    32         return jqsel.html().replace(/[\r\n]/gm,' ').replace(/\s{2,}/g,' ').replace(/(^\s+|\s+$)/g,'');
    33     }
    33     }
    34     
    34 
    35     var searchEngineTemplate = getNormalizedHtml($(".search-engine-list"));
    35     var searchEngineTemplate = getNormalizedHtml($(".search-engine-list"));
    36     
    36 
    37     $(".search-engine-list").empty().sortable();
    37     $(".search-engine-list").empty().sortable();
    38     
    38 
    39     var resourceTemplate = getNormalizedHtml($(".resource-list"));
    39     var resourceTemplate = getNormalizedHtml($(".resource-list"));
    40     
    40 
    41     $(".resource-list").empty();
    41     $(".resource-list").empty();
    42     
    42 
    43     var binTemplate = getNormalizedHtml($(".bin-list"));
    43     var binTemplate = getNormalizedHtml($(".bin-list"));
    44     
    44 
    45     $(".bin-list").empty().sortable();
    45     $(".bin-list").empty().sortable();
    46     
    46 
    47     function addSearchEngine(data, editMode) {
    47     function addSearchEngine(data, editMode) {
    48         
    48 
    49         $(".item").removeClass("item-editing");
    49         $(".item").removeClass("item-editing");
    50         
    50 
    51         var element = $(searchEngineTemplate),
    51         var element = $(searchEngineTemplate),
    52             allfields = element.find(".edit-value"),
    52             allfields = element.find(".edit-value"),
    53             typefield = element.find(".search-engine-type .edit-value"),
    53             typefield = element.find(".search-engine-type .edit-value"),
    54             typedisplay = element.find(".search-engine-type .display-value"),
    54             typedisplay = element.find(".search-engine-type .display-value"),
    55             langsection = element.find(".search-engine-language"),
    55             langsection = element.find(".search-engine-language"),
    56             langfield = element.find(".search-engine-language .edit-value"),
    56             langfield = element.find(".search-engine-language .edit-value"),
    57             langdisplay = element.find(".search-engine-language .display-value");
    57             langdisplay = element.find(".search-engine-language .display-value");
    58         element.data("userdata",data);
    58         element.data("userdata",data);
    59         
    59 
    60         if (editMode) {
    60         if (editMode) {
    61             element.addClass("item-editing");
    61             element.addClass("item-editing");
    62         }
    62         }
    63         
    63 
    64         var onChange = function() {
    64         var onChange = function() {
    65             
    65 
    66             data.type = typefield.val();
    66             data.type = typefield.val();
    67             typedisplay.text(typefield.find("[value=" + data.type + "]").text());
    67             typedisplay.text(typefield.find("[value=" + data.type + "]").text());
    68             
    68 
    69             if (data.type === "Wikipedia") {
    69             if (data.type === "Wikipedia") {
    70                 data.lang = langfield.val() || "en";
    70                 data.lang = langfield.val() || "en";
    71                 langdisplay.text(langfield.find("[value=" + data.lang + "]").text());
    71                 langdisplay.text(langfield.find("[value=" + data.lang + "]").text());
    72                 langsection.show();
    72                 langsection.show();
    73             } else {
    73             } else {
    74                 delete data.lang;
    74                 delete data.lang;
    75                 langsection.hide();
    75                 langsection.hide();
    76             }
    76             }
    77         }
    77         };
    78         
    78 
    79         typefield.val(data.type);
    79         typefield.val(data.type);
    80         
    80 
    81         if (data.type === "Wikipedia") {
    81         if (data.type === "Wikipedia") {
    82             langfield.val(data.lang || "en");
    82             langfield.val(data.lang || "en");
    83         }
    83         }
    84         
    84 
    85         allfields.on("keyup paste change", onChange);
    85         allfields.on("keyup paste change", onChange);
    86         
    86 
    87         onChange();
    87         onChange();
    88         
    88 
    89         element.click(function() {
    89         element.click(function() {
    90             $(".item").removeClass("item-editing");
    90             $(".item").removeClass("item-editing");
    91             element.addClass("item-editing");
    91             element.addClass("item-editing");
    92             return false;
    92             return false;
    93         });
    93         });
    94         element.find(".remove-item").click(function() {
    94         element.find(".remove-item").click(function() {
    95             element.remove();
    95             element.remove();
    96             return false;
    96             return false;
    97         });
    97         });
    98         
    98 
    99         $(".search-engine-list").append(element);
    99         $(".search-engine-list").append(element);
   100     }
   100     }
   101     
   101 
   102     function addBin(data, editMode) {
   102     function addBin(data, editMode) {
   103         
   103 
   104         $(".item").removeClass("item-editing");
   104         $(".item").removeClass("item-editing");
   105         
   105 
   106         var element = $(binTemplate),
   106         var element = $(binTemplate),
   107             allfields = element.find(".edit-value"),
   107             allfields = element.find(".edit-value"),
   108             typefield = element.find(".bin-type .edit-value"),
   108             typefield = element.find(".bin-type .edit-value"),
   109             typedisplay = element.find(".bin-type .display-value"),
   109             typedisplay = element.find(".bin-type .display-value"),
   110             langsection = element.find(".bin-language"),
   110             langsection = element.find(".bin-language"),
   123             kwdisplay = kwsection.find(".display-value"),
   123             kwdisplay = kwsection.find(".display-value"),
   124             ressection = element.find(".bin-resources"),
   124             ressection = element.find(".bin-resources"),
   125             titlefield = element.find(".bin-title .edit-value"),
   125             titlefield = element.find(".bin-title .edit-value"),
   126             titledisplay = element.find(".bin-title .display-value"),
   126             titledisplay = element.find(".bin-title .display-value"),
   127             reslist = element.find(".resource-list");
   127             reslist = element.find(".resource-list");
   128         
   128 
   129         reslist.sortable({
   129         reslist.sortable({
   130             stop: updateListData
   130             stop: updateListData
   131         });
   131         });
   132         
   132 
   133         element.data("userdata",data);
   133         element.data("userdata",data);
   134         
   134 
   135         if (editMode) {
   135         if (editMode) {
   136             element.addClass("item-editing");
   136             element.addClass("item-editing");
   137         }
   137         }
   138         
   138 
   139         var onChange = function() {
   139         var onChange = function() {
   140             
   140 
   141             data.type = typefield.val();
   141             data.type = typefield.val();
   142             typedisplay.text(typefield.find("[value=" + data.type + "]").text());
   142             typedisplay.text(typefield.find("[value=" + data.type + "]").text());
   143             
   143 
   144             if (data.type === "Wikipedia") {
   144             if (data.type === "Wikipedia") {
   145                 data.lang = langfield.val() || "en";
   145                 data.lang = langfield.val() || "en";
   146                 langdisplay.text(langfield.find("[value=" + data.lang + "]").text());
   146                 langdisplay.text(langfield.find("[value=" + data.lang + "]").text());
   147                 langsection.show();
   147                 langsection.show();
   148             } else {
   148             } else {
   149                 delete data.lang;
   149                 delete data.lang;
   150                 langsection.hide();
   150                 langsection.hide();
   151             }
   151             }
   152             
   152 
   153             if (data.type === "Ldt") {
   153             if (data.type === "Ldt") {
   154                 ldtsection.show();
   154                 ldtsection.show();
   155                 data.ldt_type = ldttypefield.val() || "Results";
   155                 data.ldt_type = ldttypefield.val() || "Results";
   156                 ldttypedisplay.text(ldttypefield.find("[value=" + data.ldt_type + "]").text());
   156                 ldttypedisplay.text(ldttypefield.find("[value=" + data.ldt_type + "]").text());
   157                 
   157 
   158                 data.ldt_platform = ldtpffield.val() || ldtpffield.val("http://ldt.iri.centrepompidou.fr/").val();
   158                 data.ldt_platform = ldtpffield.val() || ldtpffield.val("http://ldt.iri.centrepompidou.fr/").val();
   159                 ldtpfdisplay.text(data.ldt_platform);
   159                 ldtpfdisplay.text(data.ldt_platform);
   160                 
   160 
   161                 if (data.ldt_type === "Project") {
   161                 if (data.ldt_type === "Project") {
   162                     data.project_id = ldtprojfield.val();
   162                     data.project_id = ldtprojfield.val();
   163                     ldtprojdisplay.text(data.ldt_project);
   163                     ldtprojdisplay.text(data.ldt_project);
   164                     ldtprojsection.show();
   164                     ldtprojsection.show();
   165                 } else {
   165                 } else {
   166                     delete data.ldt_project;
   166                     delete data.ldt_project;
   167                     ldtprojsection.hide();
   167                     ldtprojsection.hide();
   168                 }
   168                 }
   169                 
   169 
   170             } else {
   170             } else {
   171                 delete data.ldt_type;
   171                 delete data.ldt_type;
   172                 delete data.project_id;
   172                 delete data.project_id;
   173                 delete data.ldt_platform;
   173                 delete data.ldt_platform;
   174                 ldtsection.hide();
   174                 ldtsection.hide();
   175             }
   175             }
   176             
   176 
   177             if (data.type === "Wikipedia" || (data.type === "Ldt" && data.ldt_type === "Results")) {
   177             if (data.type === "Wikipedia" || (data.type === "Ldt" && data.ldt_type === "Results")) {
   178                 data.search = kwfield.val();
   178                 data.search = kwfield.val();
   179                 kwdisplay.text(data.search);
   179                 kwdisplay.text(data.search);
   180                 kwsection.show();
   180                 kwsection.show();
   181             } else {
   181             } else {
   182                 delete data.search;
   182                 delete data.search;
   183                 kwsection.hide();
   183                 kwsection.hide();
   184             }
   184             }
   185             
   185 
   186             if (data.type === "ResourceList") {
   186             if (data.type === "ResourceList") {
   187                 data.title = titlefield.val() || titlefield.val("Untitled Resource List").val();
   187                 data.title = titlefield.val() || titlefield.val("Untitled Resource List").val();
   188                 titledisplay.text(data.title);
   188                 titledisplay.text(data.title);
   189                 ressection.show();
   189                 ressection.show();
   190             } else {
   190             } else {
   191                 ressection.hide();
   191                 ressection.hide();
   192             }
   192             }
   193             
   193 
   194             updateListData();
   194             updateListData();
   195             
   195 
   196         }
   196         };
   197         
   197 
   198         var updateListData = function() {
   198         var updateListData = function() {
   199             if (data.type === "ResourceList") {
   199             if (data.type === "ResourceList") {
   200                 data.list = Array.prototype.slice.call(
   200                 data.list = Array.prototype.slice.call(
   201                     reslist.find(".resource").map(function(){return $(this).data("userdata");})
   201                     reslist.find(".resource").map(function(){return $(this).data("userdata");})
   202                 );
   202                 );
   203             } else {
   203             } else {
   204                 delete data.list;
   204                 delete data.list;
   205             }
   205             }
   206         }
   206         };
   207         
   207 
   208         var addResource = function(resdata, editmode) {
   208         var addResource = function(resdata, editmode) {
   209             
   209 
   210             var resel = $(resourceTemplate),
   210             var resel = $(resourceTemplate),
   211                 all_fields = resel.find(".edit-value"),
   211                 all_fields = resel.find(".edit-value"),
   212                 title_field = resel.find(".resource-title .edit-value"),
   212                 title_field = resel.find(".resource-title .edit-value"),
   213                 title_text = resel.find(".resource-title .display-value"),
   213                 title_text = resel.find(".resource-title .display-value"),
   214                 url_field = resel.find(".resource-url .edit-value"),
   214                 url_field = resel.find(".resource-url .edit-value"),
   215                 url_text = resel.find(".resource-url .display-value"),
   215                 url_text = resel.find(".resource-url .display-value"),
   216                 description_field = resel.find(".resource-description .edit-value"),
   216                 description_field = resel.find(".resource-description .edit-value"),
   217                 description_text = resel.find(".resource-description .display-value"),
   217                 description_text = resel.find(".resource-description .display-value"),
   218                 image_field = resel.find(".resource-image .edit-value"),
   218                 image_field = resel.find(".resource-image .edit-value"),
   219                 image_text = resel.find(".resource-image .display-value");
   219                 image_text = resel.find(".resource-image .display-value");
   220                 
   220 
   221             resel.data("userdata",resdata);
   221             resel.data("userdata",resdata);
   222             
   222 
   223             if (editmode) {
   223             if (editmode) {
   224                 resel.addClass("resource-editing");
   224                 resel.addClass("resource-editing");
   225             }
   225             }
   226             
   226 
   227             var onResourceChange = function() {
   227             var onResourceChange = function() {
   228                 
   228 
   229                 resdata.title = title_field.val();
   229                 resdata.title = title_field.val();
   230                 title_text.text(resdata.title);
   230                 title_text.text(resdata.title);
   231                 
   231 
   232                 resdata.description = description_field.val();
   232                 resdata.description = description_field.val();
   233                 description_text.text(resdata.description);
   233                 description_text.text(resdata.description);
   234                 
   234 
   235                 resdata.url = url_field.val();
   235                 resdata.url = url_field.val();
   236                 url_text.text(resdata.url);
   236                 url_text.text(resdata.url);
   237                 
   237 
   238                 resdata.image = image_field.val();
   238                 resdata.image = image_field.val();
   239                 image_text.text(resdata.image);
   239                 image_text.text(resdata.image);
   240                 
   240 
   241                 updateListData();
   241                 updateListData();
   242                 
   242 
   243             }
   243             };
   244             
   244 
   245             title_field.val( resdata.title || "" );
   245             title_field.val( resdata.title || "" );
   246             url_field.val( resdata.url || "" );
   246             url_field.val( resdata.url || "" );
   247             description_field.val( resdata.description || "" );
   247             description_field.val( resdata.description || "" );
   248             image_field.val( resdata.image || "" );
   248             image_field.val( resdata.image || "" );
   249             
   249 
   250             all_fields.on("keyup paste change", onResourceChange);
   250             all_fields.on("keyup paste change", onResourceChange);
   251             
   251 
   252             onResourceChange();
   252             onResourceChange();
   253             
   253 
   254             resel.find(".remove-item").click(function() {
   254             resel.find(".remove-item").click(function() {
   255                 resel.remove();
   255                 resel.remove();
   256                 updateListData();
   256                 updateListData();
   257                 return false;
   257                 return false;
   258             });
   258             });
   259             
   259 
   260             resel.click(function() {
   260             resel.click(function() {
   261                 $(".resource").removeClass("resource-editing");
   261                 $(".resource").removeClass("resource-editing");
   262                 resel.addClass("resource-editing");
   262                 resel.addClass("resource-editing");
   263                 return false;
   263                 return false;
   264             });
   264             });
   265             
   265 
   266             reslist.append(resel);
   266             reslist.append(resel);
   267         }
   267         };
   268     
   268 
   269         if (data.list) {
   269         if (data.list) {
   270             data.list.forEach(addResource);
   270             data.list.forEach(addResource);
   271         }
   271         }
   272         
   272 
   273         typefield.val(data.type);
   273         typefield.val(data.type);
   274         
   274 
   275         if (data.type === "Wikipedia") {
   275         if (data.type === "Wikipedia") {
   276             langfield.val(data.lang || "en");
   276             langfield.val(data.lang || "en");
   277         }
   277         }
   278             
   278 
   279         if (data.type === "Ldt") {
   279         if (data.type === "Ldt") {
   280             ldttypefield.val(data.ldt_type || "Results");
   280             ldttypefield.val(data.ldt_type || "Results");
   281             ldtpffield.val(data.ldt_platform || "http://ldt.iri.centrepompidou.fr/");
   281             ldtpffield.val(data.ldt_platform || "http://ldt.iri.centrepompidou.fr/");
   282             
   282 
   283             if (data.ldt_type === "Project") {
   283             if (data.ldt_type === "Project") {
   284                 ldtprojfield.val(data.ldt_project);
   284                 ldtprojfield.val(data.ldt_project);
   285             }
   285             }
   286             
   286 
   287         }
   287         }
   288         
   288 
   289         if (data.type === "Wikipedia" || (data.type === "Ldt" && data.ldt_type === "Results")) {
   289         if (data.type === "Wikipedia" || (data.type === "Ldt" && data.ldt_type === "Results")) {
   290             kwfield.val(data.search || "");
   290             kwfield.val(data.search || "");
   291         }
   291         }
   292         
   292 
   293         if (data.type === "ResourceList") {
   293         if (data.type === "ResourceList") {
   294             titlefield.val(data.title || "Untitled Resource List");
   294             titlefield.val(data.title || "Untitled Resource List");
   295         }
   295         }
   296         
   296 
   297         allfields.on("keyup paste change", onChange);
   297         allfields.on("keyup paste change", onChange);
   298         
   298 
   299         onChange();
   299         onChange();
   300         
   300 
   301         element.click(function() {
   301         element.click(function() {
   302             $(".item").removeClass("item-editing");
   302             $(".item").removeClass("item-editing");
   303             element.addClass("item-editing");
   303             element.addClass("item-editing");
   304             return false;
   304             return false;
   305         });
   305         });
   309         });
   309         });
   310         element.find(".add-resource").click(function() {
   310         element.find(".add-resource").click(function() {
   311             addResource({"title": "New Resource"},true);
   311             addResource({"title": "New Resource"},true);
   312             return false;
   312             return false;
   313         });
   313         });
   314         
   314 
   315         $(".bin-list").append(element);
   315         $(".bin-list").append(element);
   316         
   316 
   317     }
   317     }
   318     
   318 
   319     
   319 
   320     userData.search.forEach(function(data) {
   320     userData.search.forEach(function(data) {
   321         addSearchEngine(data,false);
   321         addSearchEngine(data,false);
   322     });
   322     });
   323     userData.bins.forEach(function(data) {
   323     userData.bins.forEach(function(data) {
   324         addBin(data,false);
   324         addBin(data,false);
   325     });
   325     });
   326     
   326 
   327     $(".add-search-engine").click(function() {
   327     $(".add-search-engine").click(function() {
   328         addSearchEngine({
   328         addSearchEngine({
   329             type: "Wikipedia",
   329             type: "Wikipedia",
   330             lang: "en"
   330             lang: "en"
   331         }, true);
   331         }, true);
   332         return false;
   332         return false;
   333     });
   333     });
   334     
   334 
   335     $(".add-bin").click(function() {
   335     $(".add-bin").click(function() {
   336         addBin({
   336         addBin({
   337             type: "Wikipedia",
   337             type: "Wikipedia",
   338             lang: "en",
   338             lang: "en",
   339             search: ""
   339             search: ""
   340         }, true);
   340         }, true);
   341         return false;
   341         return false;
   342     });
   342     });
   343     
   343 
   344     $(".update-preview").click(function() {
   344     $(".update-preview").click(function() {
   345         userData.search = Array.prototype.slice.call(
   345         userData.search = Array.prototype.slice.call(
   346             $(".search-engine").map(function(){return $(this).data("userdata");})
   346             $(".search-engine").map(function(){return $(this).data("userdata");})
   347         );
   347         );
   348         userData.bins = Array.prototype.slice.call(
   348         userData.bins = Array.prototype.slice.call(
   349             $(".bin").map(function(){return $(this).data("userdata");})
   349             $(".bin").map(function(){return $(this).data("userdata");})
   350         );
   350         );
   351         new Rkns.Renkan(_.extend({}, originalData, userData));
   351         new Rkns.Renkan(_.extend({}, originalData, userData));
   352         
   352 
   353     });
   353     });
   354    
   354 
   355     new Rkns.Renkan(_.extend({}, originalData, userData));
   355     new Rkns.Renkan(_.extend({}, originalData, userData));
   356    
   356 
   357 });
   357 });