Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

js: Add support for page.reorderer callback

  • Loading branch information...
commit af0577d3105127a6c38b2e124a12108aaa5de84e 1 parent e394dea
Andreas Öman authored

Showing 1 changed file with 95 additions and 24 deletions. Show diff stats Hide diff stats

  1. +95 24 src/js/js_page.c
119 src/js/js_page.c
@@ -105,6 +105,7 @@ typedef struct js_model {
105 105 prop_sub_t *jm_eventsub;
106 106
107 107 jsval jm_paginator;
  108 + jsval jm_reorderer;
108 109
109 110 JSContext *jm_cx;
110 111
@@ -337,6 +338,7 @@ js_item_eventsub(void *opaque, prop_event_t event, ...)
337 338 ji->ji_eventsub = NULL;
338 339 ji->ji_model->jm_subs--;
339 340 JS_RemoveRoot(ji->ji_model->jm_cx, &ji->ji_this);
  341 + prop_tag_clear(ji->ji_root, ji->ji_model);
340 342 break;
341 343
342 344 case PROP_EXT_EVENT:
@@ -362,18 +364,6 @@ js_item_onEvent(JSContext *cx, JSObject *obj,
362 364 return JS_FALSE;
363 365 }
364 366
365   - if(ji->ji_eventsub == NULL) {
366   - ji->ji_eventsub =
367   - prop_subscribe(PROP_SUB_TRACK_DESTROY,
368   - PROP_TAG_CALLBACK, js_item_eventsub, ji,
369   - PROP_TAG_ROOT, ji->ji_root,
370   - PROP_TAG_COURIER, ji->ji_model->jm_pc,
371   - NULL);
372   - ji->ji_model->jm_subs++;
373   - ji->ji_this = OBJECT_TO_JSVAL(obj);
374   - JS_AddNamedRoot(cx, &ji->ji_this, "item_this");
375   - }
376   -
377 367 js_event_handler_create(cx, &ji->ji_event_handlers,
378 368 JSVAL_IS_STRING(argv[0]) ?
379 369 JS_GetStringBytes(JS_ValueToString(cx, argv[0])) :
@@ -409,7 +399,8 @@ js_item_moveBefore(JSContext *cx, JSObject *obj,
409 399 js_item_t *ji = JS_GetPrivate(cx, obj);
410 400 js_item_t *before;
411 401
412   - if(argc >= 1 && JSVAL_IS_OBJECT(argv[0]) &&
  402 + if(argc >= 1 && JSVAL_IS_OBJECT(argv[0]) &&
  403 + !JSVAL_IS_NULL(argv[0]) &&
413 404 JS_GetClass(cx, JSVAL_TO_OBJECT(argv[0])) == &item_class) {
414 405 before = JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
415 406 } else {
@@ -626,6 +617,17 @@ js_appendItem0(JSContext *cx, js_model_t *model, prop_t *parent,
626 617 LIST_INSERT_HEAD(&model->jm_items, ji, ji_link);
627 618 JS_SetPrivate(cx, robj, ji);
628 619 ji->ji_enable_set_property = 1;
  620 +
  621 + ji->ji_eventsub =
  622 + prop_subscribe(PROP_SUB_TRACK_DESTROY,
  623 + PROP_TAG_CALLBACK, js_item_eventsub, ji,
  624 + PROP_TAG_ROOT, ji->ji_root,
  625 + PROP_TAG_COURIER, model->jm_pc,
  626 + NULL);
  627 + model->jm_subs++;
  628 + ji->ji_this = OBJECT_TO_JSVAL(robj);
  629 + JS_AddNamedRoot(cx, &ji->ji_this, "item_this");
  630 + prop_tag_set(ji->ji_root, model, ji);
629 631 }
630 632 return JS_TRUE;
631 633 }
@@ -990,6 +992,25 @@ js_model_fill(JSContext *cx, js_model_t *jm)
990 992 return JSVAL_IS_BOOLEAN(result) && JSVAL_TO_BOOLEAN(result);
991 993 }
992 994
  995 +/**
  996 + *
  997 + */
  998 +static void
  999 +js_reorder(JSContext *cx, js_model_t *jm, js_item_t *ji, js_item_t *before)
  1000 +{
  1001 + void *mark;
  1002 + jsval *argv, result;
  1003 +
  1004 + if(!jm->jm_reorderer)
  1005 + return;
  1006 +
  1007 + argv = JS_PushArguments(cx, &mark, "vv", ji->ji_this,
  1008 + before ? before->ji_this : JSVAL_NULL);
  1009 +
  1010 + JS_CallFunctionValue(cx, NULL, jm->jm_reorderer, 2, argv, &result);
  1011 + JS_PopArguments(cx, mark);
  1012 +}
  1013 +
993 1014
994 1015 /**
995 1016 *
@@ -999,7 +1020,7 @@ js_model_nodesub(void *opaque, prop_event_t event, ...)
999 1020 {
1000 1021 js_model_t *jm = opaque;
1001 1022 va_list ap;
1002   -
  1023 + prop_t *p1, *p2;
1003 1024 va_start(ap, event);
1004 1025
1005 1026 switch(event) {
@@ -1007,12 +1028,23 @@ js_model_nodesub(void *opaque, prop_event_t event, ...)
1007 1028 break;
1008 1029
1009 1030 case PROP_DESTROYED:
1010   - JS_RemoveRoot(jm->jm_cx, &jm->jm_paginator);
  1031 + if(jm->jm_paginator)
  1032 + JS_RemoveRoot(jm->jm_cx, &jm->jm_paginator);
  1033 + if(jm->jm_reorderer)
  1034 + JS_RemoveRoot(jm->jm_cx, &jm->jm_reorderer);
  1035 +
1011 1036 prop_unsubscribe(jm->jm_nodesub);
1012 1037 jm->jm_nodesub = NULL;
1013 1038 jm->jm_subs--;
1014 1039 break;
1015 1040
  1041 + case PROP_REQ_MOVE_CHILD:
  1042 + p1 = va_arg(ap, prop_t *);
  1043 + p2 = va_arg(ap, prop_t *);
  1044 + js_reorder(jm->jm_cx, jm, prop_tag_get(p1, jm),
  1045 + p2 ? prop_tag_get(p2, jm) : NULL);
  1046 + break;
  1047 +
1016 1048 case PROP_WANT_MORE_CHILDS:
1017 1049 if(js_model_fill(jm->jm_cx, jm))
1018 1050 prop_have_more_childs(jm->jm_nodes);
@@ -1021,6 +1053,26 @@ js_model_nodesub(void *opaque, prop_event_t event, ...)
1021 1053 va_end(ap);
1022 1054 }
1023 1055
  1056 +
  1057 +/**
  1058 + *
  1059 + */
  1060 +static void
  1061 +install_nodesub(js_model_t *jm)
  1062 +{
  1063 + if(jm->jm_nodesub)
  1064 + return;
  1065 +
  1066 + jm->jm_subs++;
  1067 + jm->jm_nodesub =
  1068 + prop_subscribe(PROP_SUB_TRACK_DESTROY,
  1069 + PROP_TAG_CALLBACK, js_model_nodesub, jm,
  1070 + PROP_TAG_ROOT, jm->jm_nodes,
  1071 + PROP_TAG_COURIER, jm->jm_pc,
  1072 + NULL);
  1073 +}
  1074 +
  1075 +
1024 1076 /**
1025 1077 *
1026 1078 */
@@ -1035,16 +1087,32 @@ js_setPaginator(JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
1035 1087 return JS_FALSE;
1036 1088 }
1037 1089
1038   - jm->jm_subs++;
1039   - jm->jm_nodesub =
1040   - prop_subscribe(PROP_SUB_TRACK_DESTROY,
1041   - PROP_TAG_CALLBACK, js_model_nodesub, jm,
1042   - PROP_TAG_ROOT, jm->jm_nodes,
1043   - PROP_TAG_COURIER, jm->jm_pc,
1044   - NULL);
1045   -
  1090 + install_nodesub(jm);
  1091 + if(!jm->jm_paginator)
  1092 + JS_AddNamedRoot(cx, &jm->jm_paginator, "paginator");
1046 1093 jm->jm_paginator = *vp;
1047   - JS_AddNamedRoot(cx, &jm->jm_paginator, "paginator");
  1094 + return JS_TRUE;
  1095 +}
  1096 +
  1097 +
  1098 +/**
  1099 + *
  1100 + */
  1101 +static JSBool
  1102 +js_setReorderer(JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
  1103 +{
  1104 + js_model_t *jm = JS_GetPrivate(cx, obj);
  1105 +
  1106 + if(!JSVAL_IS_OBJECT(*vp) ||
  1107 + !JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(*vp))) {
  1108 + JS_ReportError(cx, "Argument is not a function");
  1109 + return JS_FALSE;
  1110 + }
  1111 +
  1112 + install_nodesub(jm);
  1113 + if(!jm->jm_reorderer)
  1114 + JS_AddNamedRoot(cx, &jm->jm_reorderer, "reorderer");
  1115 + jm->jm_reorderer = *vp;
1048 1116 return JS_TRUE;
1049 1117 }
1050 1118
@@ -1091,6 +1159,9 @@ make_model_object(JSContext *cx, js_model_t *jm, jsval *root)
1091 1159
1092 1160 JS_DefineProperty(cx, obj, "paginator", JSVAL_VOID,
1093 1161 NULL, js_setPaginator, JSPROP_PERMANENT);
  1162 +
  1163 + JS_DefineProperty(cx, obj, "reorderer", JSVAL_VOID,
  1164 + NULL, js_setReorderer, JSPROP_PERMANENT);
1094 1165 return obj;
1095 1166 }
1096 1167

0 comments on commit af0577d

Please sign in to comment.
Something went wrong with that request. Please try again.